Created
December 16, 2017 21:56
-
-
Save paul-em/f4eb699ccd60918bc45e7fceec7aad91 to your computer and use it in GitHub Desktop.
Looping Louie Advanced
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define POT_PIN 1 | |
#define DC_MOTOR_PIN 3 | |
#define BUTTON_PIN 2 | |
#define LED_PIN 13 | |
int speed = 0; | |
int minSpeed = 60; | |
int maxSpeed = 255; | |
int val = 0; | |
int prevButtonState = 0; | |
int buttonState = 0; | |
int skip = 0; | |
bool started = false; | |
bool crazy = false; | |
void setup() { | |
// put your setup code here, to run once | |
pinMode(DC_MOTOR_PIN, OUTPUT); | |
pinMode(LED_BUILTIN, OUTPUT); | |
pinMode(BUTTON_PIN, INPUT); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
val = analogRead(POT_PIN); | |
if(started) { | |
speed = minSpeed + (maxSpeed - minSpeed) * (val / 1023.0); | |
} | |
if(crazy && random(10) > 5) { | |
speed = speed * (random(100) / 100.0); | |
if(random(10) > 8) { | |
skip = 3; | |
} | |
} | |
/* Run motor */ | |
if(skip > 0) { | |
analogWrite( DC_MOTOR_PIN, 0 ); | |
skip -= 1; | |
} else { | |
analogWrite( DC_MOTOR_PIN, speed ); | |
} | |
buttonState = digitalRead(BUTTON_PIN); | |
if (buttonState == HIGH && prevButtonState == LOW) { | |
if(!started) { | |
started = true; | |
} else { | |
crazy = !crazy; | |
} | |
} | |
prevButtonState = buttonState; | |
if(crazy) { | |
digitalWrite(LED_BUILTIN, HIGH); | |
} else { | |
digitalWrite(LED_BUILTIN, LOW); | |
} | |
delay(100); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment