I am new to Arduino and I have written a code that makes 3 LEDs blink while the push button is off so the light are blinking from left to right (1,0,0)(0,1,0)(0,0,1) and then repeat from left to right. Now I am trying to modify this code so that whenever the system receives a High from the switch it reverse the order so it should start blinking from right to left and keeps blinking that way until I press the push button and then return to left to right order. I hope my issue is clear. I only know how to use if & else.
this is the initial code ( from left to right only ):
int switchState = 0;
int switchst = LOW;
void setup(){
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(2,INPUT);}
void loop(){
switchState = digitalRead(2);
if (switchState == switchst){
digitalWrite(3,~switchst);
digitalWrite(4,switchst);
digitalWrite(5,switchst);
delay(200);
digitalWrite(3,switchst);
digitalWrite(4,~switchst);
digitalWrite(5,switchst);
delay(200);
digitalWrite(3,switchst);
digitalWrite(4,switchst);
digitalWrite(5,~switchst);
delay(200);}
else {
digitalWrite(3,switchst);
digitalWrite(4,switchst);
digitalWrite(5,switchst);}}
this is the modified one: (if I press the push button it reverse the order but it only blinks for once and then stops)
int switchState;
int switchst = LOW;
int d=LOW;
void setup(){
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(2,INPUT);}
void loop(){
switchState = digitalRead(2);
if (switchState == HIGH){
d= ~d;
if(d==LOW){
digitalWrite(3,~switchst);
digitalWrite(4,switchst);
digitalWrite(5,switchst);
delay(200);
digitalWrite(3,switchst);
digitalWrite(4,~switchst);
digitalWrite(5,switchst);
delay(200);
digitalWrite(3,switchst);
digitalWrite(4,switchst);
digitalWrite(5,~switchst);
delay(200);}
else {
digitalWrite(3,switchst);
digitalWrite(4,switchst);
digitalWrite(5,~switchst);
delay(200);
digitalWrite(3,switchst);
digitalWrite(4,~switchst);
digitalWrite(5,switchst);
delay(200);
digitalWrite(3,~switchst);
digitalWrite(4,switchst);
digitalWrite(5,switchst);
delay(200);}}}
