Problems with beginners LED reaction game

The Project:
Hi, i try to work on my first project (not easy for me :sweat_smile: )
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 :laughing:

In the Arduino IDE, use CTRL T to format your code then copy the complete sketch.
Use the </> button from the ‘reply menu’ to attach the copied sketch.


Never ever use delay() in your sketches.

Well you can if you fully understand why you shouldn’t.

Read this

1 Like

FYI

This doesn't have the appearance of a tutorial

(Where's the "report to moderator" control?)

It's the flag.

As if by magic it has been moved here

Here, try it like this..

#include <timeObj.h>
#include <mechButton.h>


#define SHORT_MS     1000/3.0    // 1/3 seconds
#define LONG_MS      2000        // 2 seconds
#define BTN_PIN      2           // Button pin, hook this from pin to ground. No resistors.
#define LED_PIN_0    3           // LED pins. Just a serving suggestion. Ues whatever pins you like.
#define LED_PIN_1    4
#define LED_PIN_2    5
#define LED_PIN_3    6
#define LED_PIN_4    7

timeObj     LEDTimer(SHORT_MS);  // A timing object for timing the LEDs
mechButton  theButton(BTN_PIN);  // A button object for deboiuncing the button.
int         pinList[5];          // Our orded pin list.
int         pin;                 // What pin are we lighting now?


void setup(void) {

   pinList[0]  = LED_PIN_0;            // Stuff the pin number into the list.
   pinList[1]  = LED_PIN_1;
   pinList[2]  = LED_PIN_2;
   pinList[3]  = LED_PIN_3;
   pinList[4]  = LED_PIN_4;
   pin = 0;                            // We start at index zero.
   for (int i=0;i<5;i++) {             // For all the pins..
      pinMode(pinList[i],OUTPUT);      // Make them output pins.
   }
   theButton.setCallback(btnClicked);  // Set the callback for the button.
}


// This is the callback that the button will call when clicked.
void btnClicked(void) {

   if (!theButton.trueFalse()) {       // If the button has been grounded..
      LEDTimer.setTime(LONG_MS,true);  // We bunp up the timer and reset it.
   }
}


void loop() {

   idle();                             // idle() lets the button do its thing.
   if (LEDTimer.ding()) {              // If the timer has expired..
      LEDTimer.setTime(SHORT_MS,true); // Reset the timer (short time, just in case)
      digitalWrite(pinList[pin],LOW);  // Shut off the current LED.
      pin++;                           // Bump up the LED index
      if (pin>=5) {                    // If we've gone beyond 5 on the index..
         pin = 0;                      // Reset it back down to zero.
      }
      digitalWrite(pinList[pin],HIGH); // Turn on the new LED.
   }
}

You'll need to grab LC_baseTools from the library manager to compile it.

Good luck!

-jim lee

1 Like

What flag?

I donno' what they're going on about either.

-jim lee

The ‘Report to moderator’ button has been replaced with the ‘Flag’ button.

This flag
image

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
https://forum.arduino.cc/index.php?topic=712198.0

Can you please post a circuit diagram please?
How have you got your button wired?
If you have the button between the input pin and 5V, do you have a 10K resistor between the input pin and gnd.
The 10K ensures the input goes LOW when the button is not pushed.

Tom... :grinning: :+1: :coffee: :australia:

Hello
My standard recipe:
As a basis, study the IPO model and then take a bag of data structures, timers and a small finite state machine and stir everything together to the desired functionality. And don't forget the seasoning. :nerd_face:

Did i post it somewhere wrong?? I had no idea

No. Arduino recently changed the forum software, and the old-timers here are a bit on edge because the change sucks.

Read the post of how to get the most from the forum. You need to put your code into code tags so that it formats correctly in the forum. We also want to see a schematic. A scan of a hand-drawn schematic is fine, but please, no Fritzing crap. (Fritzing pictures are NOT schematics).

Your question may have been lost in the noise, so try restating it when you post the code. Tell us what is happening, and what you want to happen.

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.