Hi all,
I'm doing a project that requires an LED chaser, which has variable speed (via potentiometer) and can have it's direction switched by one or two (ideally two but currently programmed for one) momentary switches. I've never coded before so I've been mixing and matching pre-existing code to get this far, but not sure why the button isn't reversing the direction of the chaser. I'm sure it's just a few simple lines of code I'm missing so if anyone could give me some pointers or even tell me what to add verbatim I'd be grateful and and you will have saved me a huge migraine.
int buttonPin = 13; // the number of the pushbutton pin
int ledNum = 8; // define the number of the LEDs
int ledPin[] = {2, 3, 4, 5, 6, 7, 8, 9}; // create array for LED pins
int delayTime; // define a variable for the value of delay
int potPin = A0; // define the potentiometer pin
int initial = 0; //hold current initial
int oldstate = 0; //hold last initial
int buttonstate = 0; // variable for reading the pushbutton status
void setup() {
for (int x = 0; x < ledNum; x++) { // setting all LEDs as OUTPUT
pinMode(ledPin[x], OUTPUT);
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input
}
}
void loop() {
//debouncing routline to read button
buttonstate = digitalRead(buttonPin); //state the initial of button
if(buttonstate == HIGH){ //check if it has been pressed
buttonstate = digitalRead(buttonPin);//state button again
if(buttonstate == LOW){ //if it is 0 considered one press
initial = oldstate + 1; //increase initial by 1
}
}else{ //check if it has been NOT pressed
delay(100);
}
switch (initial){ //react to button press a initial
case 1:
for (int i = 0; i < 8; i++) {
delayTime = analogRead(potPin); //getting the time delay from the potentiometer
digitalWrite(ledPin[i], HIGH); //turn on LEDs
delay(delayTime); //time inverval
digitalWrite(ledPin[i], LOW); //turn off LEDs
oldstate = initial; //set oldstate initial as current initial
}
case 2:
for (int i = 7; i >= 0; i--) {
delayTime = analogRead(potPin); //getting the time delay from the potentiometer
digitalWrite(ledPin[i], HIGH); //turn on LEDs
delay(delayTime); //time interval
digitalWrite(ledPin[i], LOW); //turn off leds
oldstate = initial;}
} }