Why isn't my code doing what it should do?

Hey guys!
I'm new to Arduino programming, so this is probably a noob question.
Basically, I'm trying to make a "Reaction Timer".
the LED's will keep flashing from right to left and left to right. when you click the button, the LED that is currently on is supposed to stay on and the next LED shouldn't go on (basically if you read the program you will understand). What i don;t understand is why isn't my program working. (I'm pretty sure its an error in the programming).

int switchState = 0;
int redLED1;
int redLED2;
int greenLED;
int redLED3;
int redLED4;

void setup(){
  pinMode(2, INPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
}

void loop(){
  switchState = digitalRead(2);
  redLED1 = digitalRead(3);
  redLED2 = digitalRead(4);
  greenLED = digitalRead(5);
  redLED3 = digitalRead(6);
  redLED4 = digitalRead(7);
  if (switchState == LOW){
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    delay(100);
    digitalWrite(3, LOW);
    digitalWrite(4, HIGH);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    delay(100);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, HIGH);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    delay(100);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    digitalWrite(7, LOW);
    delay(100);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, HIGH);
    delay(100);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    digitalWrite(7, LOW);
    delay(100);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, HIGH);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    delay(100);
    digitalWrite(3, LOW);
    digitalWrite(4, HIGH);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
  }
  if(switchState == HIGH){
    if(redLED1 == HIGH){
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    }
    if(redLED2 == HIGH){
    digitalWrite(3, LOW);
    digitalWrite(4, HIGH);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    }
    if(greenLED == HIGH){
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, HIGH);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    }
    if(redLED3 == HIGH){
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    digitalWrite(7, LOW);
    }
    if(redLED4 == HIGH){
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, HIGH);
    }
  }
}

Could it be that immediately you exit "loop()", it gets called again and everything that you just did gets undone?

him i don't think so because if i leave the button pressed, when it gets to "redLED2" then that LED stays on until i un-press the button. I'm really confused of whats going on.

i don't think so

I do.

Ok then what should i do? im a newb at this XD

Yup i just added a delay so i don't think thats it. The delay didn't fix the problem.

look up switch debouncing, each press of a switch is lots of small presses, which could leave the variable in an unpredicted state.

Another problem might be that since there is quite a bit going on the processer may miss your button push. instead you could use an interrupt, and an interrupt handler to update the switchState variable

and you're trying to read the state of an OUTPUT.

Personally i would write to the variables when you write to the LED's rather than reading the OUTPUT.

Some serial prints of the variables states may also help you with debugging

HackExpert:
Hey guys!
I'm new to Arduino programming, so this is probably a noob question.
Basically, I'm trying to make a "Reaction Timer".
the LED's will keep flashing from right to left and left to right. when you click the button, the LED that is currently on is supposed to stay on and the next LED shouldn't go on (basically if you read the program you will understand). What i don;t understand is why isn't my program working. (I'm pretty sure its an error in the programming)

this is a good time to learn about functions, the mills() function and arrays...

something like this:

const int ledPin[5] = {3,4,5,6,7};
int index = 1;
int myDirection = 1;
int lastSwitchState;
boolean flashing = true;
//
void setup()
{
  Serial.begin(115200);
  for (int i = 0; i < 5; i++)
  {
    pinMode(ledPin[i], OUTPUT);
  }
  pinMode(2, INPUT_PULLUP);
}
//
void loop()
{
  int switchState = digitalRead(2);
  if (switchState == LOW && lastSwitchState == HIGH)
  {
    flashing = !flashing;
  }
  lastSwitchState = switchState;
  if (flashing)
  {
    blinkLed();
  }
  
}
void blinkLed()
{
  static unsigned long lastUpdate;
  if (millis() - lastUpdate >= 100UL)
  {
    index += myDirection;
    Serial.println(index);
    if (index <=0 || index >= 4)
    {
      myDirection = -myDirection;
    }
    for (int i = 0; i < 5; i++)
    {
      digitalWrite(ledPin[i], (i == index? HIGH : LOW));
    }
    lastUpdate = millis();
  }
}

HackExpert:
What i don;t understand is why isn't my program working.

Ahhh, the eternal question except without the question mark!

Your code spends the greatest amount of run time not doing anything including detecting button presses.

In fact, the whole time the leds are blinking I don't see a single pin read between any of them. So how is it supposed to detect the button press?

Perhaps you should try Bulldog's code and tracing out how that works?

BulldogLowell:

HackExpert:
Hey guys!
I'm new to Arduino programming, so this is probably a noob question.
Basically, I'm trying to make a "Reaction Timer".
the LED's will keep flashing from right to left and left to right. when you click the button, the LED that is currently on is supposed to stay on and the next LED shouldn't go on (basically if you read the program you will understand). What i don;t understand is why isn't my program working. (I'm pretty sure its an error in the programming)

this is a good time to learn about functions, the mills() function and arrays...

something like this:

const int ledPin[5] = {3,4,5,6,7};

int index = 1;
int myDirection = 1;
int lastSwitchState;
boolean flashing = true;
//
void setup()
{
  Serial.begin(115200);
  for (int i = 0; i < 5; i++)
  {
    pinMode(ledPin[i], OUTPUT);
  }
  pinMode(2, INPUT_PULLUP);
}
//
void loop()
{
  int switchState = digitalRead(2);
  if (switchState == LOW && lastSwitchState == HIGH)
  {
    flashing = !flashing;
  }
  lastSwitchState = switchState;
  if (flashing)
  {
    blinkLed();
  }
 
}
void blinkLed()
{
  static unsigned long lastUpdate;
  if (millis() - lastUpdate >= 100UL)
  {
    index += myDirection;
    Serial.println(index);
    if (index <=0 || index >= 4)
    {
      myDirection = -myDirection;
    }
    for (int i = 0; i < 5; i++)
    {
      digitalWrite(ledPin[i], (i == index? HIGH : LOW));
    }
    lastUpdate = millis();
  }
}

wow thanks dude :smiley:
I really just started programming so no wonder my program is so basic XD
but anyway thanks for the help