Skip to content

Instantly share code, notes, and snippets.

@paul-em
Created December 16, 2017 21:56
Show Gist options
  • Save paul-em/f4eb699ccd60918bc45e7fceec7aad91 to your computer and use it in GitHub Desktop.
Save paul-em/f4eb699ccd60918bc45e7fceec7aad91 to your computer and use it in GitHub Desktop.
Looping Louie Advanced
#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