The Project:
Hi, i try to work on my first project (not easy for me )
I want to create a reaction game, where one LED goes on for 1/3 seconds.
After that the LED next to it goes on, while the first goes off.
So there is always one led on.
This keeps going on through all 5 LEDs. Then it starts over again.
If you use the button, the LED, that is on at that moment, will glow for 2 and 1/3 seconds, before the programm continues with the next one.
The goal is, to use the button right on the middle one (the green LED)
My Problem:
It all seems to work right, but sometimes the LED doesn´t stop, when i use the button.
It just keeps going like i didn´t use the buttom. But it works for most of the time
The Code:
int led1 = 11; // first red LED
int led2 = 10; // second red LED
int ledg = 9; // green middle LED
int led3 = 6; // third red LED
int led4 = 5; // forth red LED
int tasterPin = 2; // this is the button, that the player uses
int tasterStatus = 0;
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(ledg, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(tasterPin, INPUT);
}
void loop() {
tasterStatus = digitalRead(tasterPin);
digitalWrite(led1, HIGH); //first Led goeas on
if(tasterStatus == HIGH){ // tests if i use the button
delay(2000); // if i use the button, than it will be on 2 seconds longer
}
delay(300);
digitalWrite(led1, LOW); // now the led goes off
tasterStatus = digitalRead(tasterPin);
digitalWrite(led2, HIGH); //the second led goes on
if(tasterStatus == HIGH){
delay(2000);
}
delay(300);
digitalWrite(led2, LOW);
tasterStatus = digitalRead(tasterPin);
digitalWrite(ledg, HIGH); //green led goes on
if(tasterStatus == HIGH){
delay(2000);
}
delay(300);
digitalWrite(ledg, LOW);
tasterStatus = digitalRead(tasterPin);
digitalWrite(led3, HIGH); //third Led goes on
if(tasterStatus == HIGH){
delay(2000);
}
delay(300);
digitalWrite(led3, LOW);
tasterStatus = digitalRead(tasterPin);
digitalWrite(led4, HIGH); //forth Led goes on
if(tasterStatus == HIGH){
delay(2000);
}
delay(300);
digitalWrite(led4, LOW);
}
I wanted to upload a picture of my build up as well, but it doesn´t work for new members.
I really hope that you can help me with this, so i can continue working on my first try out project. And sorry if my english is not perfect.. I tried my best