Interactive Lights

Hi,

I would like leds to light from left to right and then right to left in the continuous loop when I press the button.When I press the button again, lights should go off completely. I wrote the code below, it works when I press the button first time. However, when I press the button again, leds don't stop.
Any idea what's wrong with my code below.

byte ledPin [] = {2,3,4};
byte inputPin = 13;
byte lastState;
byte currentState;
byte ledState = LOW;
byte counter = 0;
byte ledDirection = 1;
byte currentLed = 0;

void setup() {

for (int i = 0; i<3; i++)
pinMode (ledPin*, OUTPUT);*

  • pinMode (inputPin, INPUT);*
    }
    void loop() {

  • currentState = digitalRead(inputPin);*

  • if (currentState == HIGH){*

  • counter++ ;*

  • }*

  • if (counter % 2 != 0)*

  • {*

  • for(int i = 0; i<3; i++)*

  • {*
    _ digitalWrite(ledPin*,LOW);_
    _
    }_
    _
    digitalWrite(ledPin[currentLed],HIGH);_
    _
    delay (100);_
    _
    currentLed += ledDirection;_
    _
    }_
    _
    if(currentLed == 2)_
    _
    ledDirection = -1;_
    _
    if (currentLed == 0)_
    _
    ledDirection = 1;*_

* if (counter % 2 == 0)*
* {*
* for(int i = 0; i<3; i++)*
* {*
_ digitalWrite(ledPin*,LOW);
}
}
}*_

Welcome to the Forum. Please read the two posts by Nick Gammon about the guidelines for posting to the Forum, especially using code tags for source code listings.

In your present code, how is the statement

    pinMode (ledPin, OUTPUT);

supposed to affect ledPin elements if no array subscript is given? Also, why is inputPin even in the for loop?

A coupe of things:

Why do you have a loop in the setup function?

 for (int i = 0; i<3; i++)

I do not see an "IF" , or "ELSE" statement for the condition "currentState==LOW"

It may be a good idea to monitor the value of "counter" throughout your loop to get your answer.

@Yellow GTM: I can understand the for loop in setup() if it were written:

 for (int i = 0; i<3; i++)
    pinMode (ledPin[i], OUTPUT);

but he left the array subscripts out and does it again in loop(), yet uses a subscript in one of his calls to digitalWrite(). Perhaps there's a syntactic shortcut I don't know about...

No shortcut, just the forum software doing its thing with [i], hence the subsequent italics.

AWOL:
No shortcut, just the forum software doing its thing with [i], hence the subsequent italics.

Ahh, gotta love those code tags, or lack thereof!