I am trying to modify the bike light project found at the bottom of this page: Arduino Tutorial - Lesson 5
I want to do two things: 1) I want to add a second push button switch to change the direction of the currently running pattern. For example, if the lights go right to left, pushing the button would make them go from left to right, pushing the button again would set them back to the original direction. 2) I want this to remember what light pattern was displayed when it is turned off and then back on again.
I changed some of the patterns from the original code and will add more which I know how to do, but I need help with the two things above. Would I need a buttonState2 in there for the pattern reverse switch? Any help is appreciated. Thank you. Here's where I'm at:
/*
* Bike light with pattern reverse button
*/
int potPin = 0; // input pin for the pot
int timer = 50;
// you can adjust the numbers below to set the max and min for the speed adjustment
int x = 10; // sets the max speed (0 = fast) the lower the number the faster it can go
int y = 300; // sets the min speed (100 = slow) the higher the number the slower it can go
int switchPin = 2; // pattern select switch is connected to pin 2
int switchPin2 = 3; // pattern reverse switch is connected to pin 3
int led1Pin = 11;
int led2Pin = 10;
int led3Pin = 9;
int led4Pin = 8;
int led5Pin = 7;
int val; // variable for reading the pin status
int val2; // variable for reading the delayed status
int buttonState; // variable to hold the button state
int lightMode = 0; // What mode is the light in?
void setup() {
pinMode(switchPin, INPUT); // Set the switch pin as input
pinMode(switchPin2, INPUT); // set the 2nd switch as input
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
pinMode(led4Pin, OUTPUT);
pinMode(led5Pin, OUTPUT);
Serial.begin(9600); // Set up serial communication at 9600bps
buttonState = digitalRead(switchPin); // read the initial state
}
void loop(){
val = digitalRead(switchPin); // read input value and store it in val
delay(10); // 10 milliseconds is a good amount of time
timer = analogRead(0); // reads the pot and sets the timer to the value
timer = map(timer, 0, 1023, x, y); // maps the timer value to the max and min values set above with x and y
val2 = digitalRead(switchPin); // read the input again to check for bounces
if (val == val2) { // make sure we got 2 consistant readings!
if (val != buttonState) { // the button state has changed!
if (val == LOW) { // check if the button is pressed
if (lightMode == 0) { // if its off
lightMode = 1; // alternate blinking!
} else {
if (lightMode == 1) { // if its alternate blinking
lightMode = 2; // wave then come back!
} else {
if (lightMode == 2) { // if its waving then coming back
lightMode = 3; // make it wave!
} else {
if (lightMode == 3) { // if its waving,
lightMode = 0; // turn light off!
}
}
}
}
}
}
buttonState = val; // save the new state in our variable
}
// Now do whatever the lightMode indicates
if (lightMode == 0) { // all-off
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
digitalWrite(led4Pin, LOW);
digitalWrite(led5Pin, LOW);
}
if (lightMode == 1) { // alternate blinking
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, HIGH);
digitalWrite(led4Pin, LOW);
digitalWrite(led5Pin, HIGH);
delay(timer);
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, HIGH);
digitalWrite(led3Pin, LOW);
digitalWrite(led4Pin, HIGH);
digitalWrite(led5Pin, LOW);
delay(timer);
}
if (lightMode == 2) { // "wave" then come back
digitalWrite(led1Pin, HIGH);
delay(timer);
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, HIGH);
delay(timer);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, HIGH);
delay(timer);
digitalWrite(led3Pin, LOW);
digitalWrite(led4Pin, HIGH);
delay(timer);
digitalWrite(led4Pin, LOW);
digitalWrite(led5Pin, HIGH);
delay(timer);
digitalWrite(led5Pin, LOW);
digitalWrite(led4Pin, HIGH);
delay(timer);
digitalWrite(led4Pin, LOW);
digitalWrite(led3Pin, HIGH);
delay(timer);
digitalWrite(led3Pin, LOW);
digitalWrite(led2Pin, HIGH);
delay(timer);
digitalWrite(led2Pin, LOW);
digitalWrite(led1Pin, HIGH);
}
if (lightMode == 3) { // "wave"
digitalWrite(led5Pin, LOW);
digitalWrite(led1Pin, HIGH);
delay(timer);
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, HIGH);
delay(timer);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, HIGH);
delay(timer);
digitalWrite(led3Pin, LOW);
digitalWrite(led4Pin, HIGH);
delay(timer);
digitalWrite(led4Pin, LOW);
digitalWrite(led5Pin, HIGH);
delay(timer);
digitalWrite(led5Pin, LOW);
}
}