3 LED Blinking when button pushed down

Hi,

I have an assignment which requires me to have 3 LEDS blinking, when a button is pushed downwards. And no light to be on when the button is not pushed. I've got the 3 blinking lights to flash when the button is pushed. But the problem is that there needs to be only one light on at a time. The lights are currently having 2 lights on at the same time.

I would like some input on my code and ways I could modify it to achieve just 1 blinking light on at a time when the button is pushed.

Thanks

const int button =2;
int state=0;

void setup() {
// put your setup code here, to run once:

pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(button, INPUT);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
}

void loop() {
// put your main code here, to run repeatedly:

state= digitalRead(button);
digitalWrite(5, HIGH);
delay(200);
digitalWrite(5, HIGH);
delay(200);
if(state==1){
digitalWrite(5, HIGH);
}
else{
digitalWrite(5, LOW);
}
state= digitalRead(button);
digitalWrite(6, HIGH);
delay(200);
digitalWrite(6, HIGH);
delay(200);
if(state==1){
digitalWrite(6, HIGH);
}
else{
digitalWrite(6, LOW);
}
state= digitalRead(button);
digitalWrite(7, HIGH);
delay(200);
digitalWrite(7, HIGH);
delay(200);
if(state==1){
digitalWrite(7, HIGH);
}
else{
digitalWrite(7, LOW);
}
}

I would do something like this:

int tick = 0;
const int button = 2;

void setup() {
    pinMode(5, OUTPUT);
    pinMode(6, OUTPUT);
    pinMode(7, OUTPUT);
    pinMode(button, INPUT);
}

void loop() {
    state = digitalRead(button);
    tick = Math.mod(tick+1, 1500); //increase tick, but keep it under 1500

    if (state) {

        //Turn on lights based on how high tick is out of 1500
        if (tick < 500) {
            digitalWrite(5, HIGH); //only turn on light A
            digitalWrite(6, LOW);
            digitalWrite(7, LOW);
        } else if (tick < 1000) {
            digitalWrite(5, LOW); //only turn on light B
            digitalWrite(6, HIGH);
            digitalWrite(7, LOW);
        } else {
            digitalWrite(5, LOW); //only turn on light C
            digitalWrite(6, LOW);
            digitalWrite(7, HIGH);
        }
    } else {
        digitalWrite(5, LOW); //turn off all lights
        digitalWrite(6, LOW);
        digitalWrite(7, LOW);
    }


}

I haven't tested it yet, but I think this is right. You can change the numbers (in the tick line, and the if statements) to change the length of the blink. Doing this will also allow you to run your main loop smoothly, whereas your original scripts has many delays before it gets back to the top. Hope this helps!

I tried to run the code, but I got an error at the state= digitalRead(button); part. I'm unsure on how to fix this, because to me the code looks fine.

In your case, it had better to use the output library for blinking multiple LED without using delay

purnez:
I tried to run the code, but I got an error at the state= digitalRead(button); part. I'm unsure on how to fix this, because to me the code looks fine.

I found it. It's because I never defined state in my code. Just define state as an int, and it should work.