Need help with timer game making and random led blinking.

Hello I’m currently working on making a timer game where there is 4 leds and 4 buttons,randomely 1 of the led is suppose to blink and you will have 120 ms to click the button opposite to it, then another random led with blink and you have 120 ms to click the button and so on… if you fail to click the button the game resets and ends and you will have to click the start button again. So far I got an idea on how the coding is gunna work but some guidance on this will be much appreciated!
Code:
int LED1 = 9;
int LED2 = 10;
int LED3 = 11;
int LED4 = 12;
int BUTTON1 = 1;
int BUTTON2 = 2;
int BUTTON3 = 3;
int BUTTON4 = 5;
int Start = 6;
long randNumber;

void setup(){
Serial.begin(9600);
randomSeed(analogRead(0));
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);
pinMode(LED3,OUTPUT);
pinMode(LED4,OUTPUT);
pinMode(BUTTON1,INPUT);
pinMode(BUTTON2,INPUT);
pinMode(BUTTON3,INPUT);
pinMode(BUTTON4,INPUT);
pinMode(Start,INPUT);

}

void loop(){
randNumber = random(1,4);
reset:
if(digitalRead(Start) == HIGH){
gamestart:
Serial.println(randNumber);

if((randNumber) == 1){
digitalWrite(LED1,LOW);
delay(120);
if(digitalRead(BUTTON1) == HIGH){
goto gamestart;
}else{
goto reset;
}

if((randNumber) == 2){
digitalWrite(LED2,LOW);
delay(120);
if(digitalRead(BUTTON2) == HIGH){
goto gamestart;
}else{
goto reset;
}

if((randNumber) == 3){
digitalWrite(LED3,LOW);
delay(120);
if(digitalRead(BUTTON3) == HIGH){
goto gamestart;
}else{
goto reset;
}

if((randNumber) == 4){
digitalWrite(LED4,LOW);
delay(120);
if(digitalRead(BUTTON4) == HIGH){
goto gamestart;
}else{
goto reset;
}

}else{
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,HIGH);
digitalWrite(LED4,HIGH);
}
}
}

delay(120);

if(digitalRead(BUTTON4) == HIGH){

This will wait 120ms and then check if the button is high. What if it is pressed and released in that time?

Look at the example sketch "Blink without delay". Then think about how you can repeatedly inspect the button state while waiting out the 120ms.

And don't write "120" directly in your code. You are going to want to change it. Make it a variable so the 'advanced' version of the game can change the timing to make it harder.

MorganS:
This will wait 120ms and then check if the button is high. What if it is pressed and released in that time?

Look at the example sketch "Blink without delay". Then think about how you can repeatedly inspect the button state while waiting out the 120ms.

And don't write "120" directly in your code. You are going to want to change it. Make it a variable so the 'advanced' version of the game can change the timing to make it harder.

delay(120);
if(digitalRead(BUTTON4) == HIGH){
Well you basically have 120 ms to click button 4 before if not the game will reset and you will have to press the start button again. In the advanced version of the game the delay will depend on a potentiometer but for now I really need help on how to get this basic game set up.

Alright, first try to write it without GOTOs. You will need another loop something like this...

//after start button was pressed...
boolean UserIsWinning = true;
while(UserIsWinning) {
//do stuff...

   //Oops, user missed the time - game lost
   UserIsWinning = false;

//...
}

Then when they lose, it falls out of this while() loop and goes back to the top of the main loop which waits for the start button to be pressed again. No gotos required.

Sorry I'm kinda new to programming and stuff, do you think you can edit and my code for me as I tried to put it in mine but it got messed up.

Btw here's a slightly updated version that is starting to work but problem is I have to keep pressing start button to get it to generate a random number and choose an Led to light up, also after 120 ms the Led stays off and waits for me to click the button.

If you can please fix my coding I would be so great full :c

int LED1 = 8;
int LED2 = 10;
int LED3 = 11;
int LED4 = 12;
int BUTTON1 = 7;
int BUTTON2 = 2;
int BUTTON3 = 3;
int BUTTON4 = 5;
int Start = 6;
long randNumber;

void setup(){
Serial.begin(9600);
randomSeed(analogRead(0));
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);
pinMode(LED3,OUTPUT);
pinMode(LED4,OUTPUT);
pinMode(BUTTON1,INPUT);
pinMode(BUTTON2,INPUT);
pinMode(BUTTON3,INPUT);
pinMode(BUTTON4,INPUT);
pinMode(Start,INPUT);

}

void loop(){
randNumber = random(1,4);
reset:
if(digitalRead(Start) == LOW){

gamestart:
Serial.println(randNumber);

if((randNumber) == 1){
digitalWrite(LED1,LOW);
delay(120);
if(digitalRead(BUTTON1) == HIGH){
goto gamestart;
}else{
goto reset;
}
}

if((randNumber) == 2){
digitalWrite(LED2,LOW);
delay(120);
if(digitalRead(BUTTON2) == HIGH){
goto gamestart;
}else{
goto reset;
}
}

if((randNumber) == 3){
digitalWrite(LED3,LOW);
delay(120);
if(digitalRead(BUTTON3) == HIGH){
goto gamestart;
}else{
goto reset;
}
}

if((randNumber) == 4){
digitalWrite(LED4,LOW);
delay(120);
if(digitalRead(BUTTON4) == HIGH){
goto gamestart;
}else{
goto reset;
}
}
}
}else{
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,HIGH);
digitalWrite(LED4,HIGH);
}

First a number of remarks:

  • The use of goto is seldom necessary and should be avoided
  • pin 1 is for serial communication ad should not be used for other purposes
  • Using the delay() function is asking for trouble
  • It would be far better to use arrays for buttons and leds, that way you would not need to duplicate code

I would have solved the problem using a state machine, see attached state diagram. In this state machine i have also handled cheats (pressing any button before start) and false presses. I believe these are important for the gameplay.
Once the state diagram is finished (and not before that) you can convert it to code. One way of doing that is using using my State machine library to facilitate the coding resulting in something like this:

#include <SM.h>
SM game(gameStart);
const int noControls = 4;
const byte led[noControls] = {9, 10, 11, 12};
const byte btn[noControls] = {2, 3, 4, 5};//pin 0 and 1 are for serial
const byte startBtn = 6;
const unsigned long limit = 120;//reaction time limit
byte currentCtrl;
enum btnState {noPress, correct, fault};

void setup(){
    for(byte i = 0; i<noControls; i++) pinMode(led[i], OUTPUT);
    randomSeed(analogRead(0));
}//setup()

void loop(){
  EXEC(game);
}//loop()

State gameStart(){
  if(digitalRead(startBtn)){
    currentCtrl = random(noControls);//determine control number
    ledCtrl(1<<currentCtrl);//light correct led
    game.Set(randomLed);//change state
  }//if(stertBtn)
  for(byte i = 0; i<noControls; i++) if(digitalRead(btn[i])){//check if user cheats
    ledCtrl(15);//all leds on
    game.Set(miss);//change state
    break;//no need to check further
  }//if(cheat)
}//gameStart()

State randomLed(){
  if(game.Timeout(limit)){//check for Timeout
    ledCtrl(15);//all leds on
    game.Set(miss);//change state
  }//if(Timeout)
  btnState pressed = noPress;
  for(byte i = 0; i<noControls; i++){//scan all buttons
    if(digitalRead(btn[i])){//check individual button
      if(i == currentCtrl){//check validity of btn
        pressed = correct;//we have a hit
      }else{//incorrect btn
        pressed = fault;//false press
        break;//abort scan
      }//()
    }//if(btn[i])
  }//for(i)
  switch(pressed){//act on press
    case correct:
      currentCtrl = 1;//reuse currentCtrl for display
      ledCtrl(currentCtrl);
      game.Set(success);//change state
      break;
    case fault:
      ledCtrl(15);//all leds on
      game.Set(miss);//change state
      break;
  }//switch(pressed)
}//randomLed()

State miss(){
  if(game.Timeout(1000)){
    ledCtrl(0);//all leds off
    game.Set(gameStart);
  }//()
}//miss()

State success(){
  if(game.Timeout(300)){//wait 300ms
    currentCtrl <<= 1;//shift 1 bit
      if(currentCtrl<(1<<noControls)){//more ledsd to show?
        ledCtrl(currentCtrl);
        game.Set(success);
      }else{//back to start
        ledCtrl(0);//all leds off
        game.Set(gameStart);//change state
      }
  }//if(wait 300 ms)()
}//success()

inline void ledCtrl(byte ctrl){
  for(byte i = 0; i<noControls; i++) digitalWrite(led[i], !!(ctrl&(1<<i)));
}//ledCtrl()

And please use code tags when posting code

reaction game state diagram.png

Thanks for the code but when I compile I get this error
error: 'State' does not name a type

Have you downloaded and installed the library?

Okay I installed the library but when I click start button nothing happens but all the 4 leds are on :c

Yes, that will happen if you have a reaction time of 120ms as you originally specified… I recommend you increase it.
All 4 leds lit means as miss that is you cheated byt by pressing any button before the start, you pressed the wrong button after start or you did not press in time. Remember, start button is connected to pin 6 since you should not use pins 0 and 1.

Still nothings happening when I change delay to 1200+ ms, and my arduino is stupid because sometimes the leds light up when digitalWrite(led, LOW); so I think the code for the led should reverse aswell :/.

#include <SM.h>
SM game(gameStart);
const int noControls = 4;
const byte led[noControls] = {8, 10, 11, 12};
const byte btn[noControls] = {2, 5, 3, 7};//pin 0 and 1 are for serial
const byte startBtn = 6;
const unsigned long limit = 1200;//reaction time limit
byte currentCtrl;
enum btnState {noPress, correct, fault};

Heres a picture.
http://s1122.photobucket.com/user/Dennis_Pratap/media/DSC00920_zpsfffb9ce4.jpg.html

Have you connected the leds and the switches correctly?
Here i have commented out the timeout and added some serial debug information.

#include <SM.h>
SM game(gameStart);
const int noControls = 4;
const byte led[noControls] = {9, 10, 11, 12};
const byte btn[noControls] = {2, 3, 4, 5};//pin 0 and 1 are for serial
const byte startBtn = 6;
const unsigned long limit = 120;//reaction time limit
byte currentCtrl;
enum btnState {noPress, correct, fault};

void setup(){
  Serial.begin(115200); 
  for(byte i = 0; i<noControls; i++) pinMode(led[i], OUTPUT);
  randomSeed(analogRead(0));
}//setup()

void loop(){
  EXEC(game);
}//loop()

State gameStart(){
  if(digitalRead(startBtn)){
    currentCtrl = random(noControls);//determine control number
    ledCtrl(1<<currentCtrl);//light correct led
    Serial.print("Started with:");
    Serial.println(currentCtrl);
    game.Set(randomLed);//change state
  }//if(stertBtn)
  for(byte i = 0; i<noControls; i++) if(digitalRead(btn[i])){//check if user cheats
    ledCtrl(15);//all leds on
    Serial.println("Cheat!");
    game.Set(miss);//change state
    break;//no need to check further
  }//if(cheat)
}//gameStart()

State randomLed(){
//  if(game.Timeout(limit)){//check for Timeout
//    ledCtrl(15);//all leds on
//    Serial.println("Timeout");
//    game.Set(miss);//change state
//  }//if(Timeout)
  btnState pressed = noPress;
  for(byte i = 0; i<noControls; i++){//scan all buttons
    if(digitalRead(btn[i])){//check individual button
      if(i == currentCtrl){//check validity of btn
        pressed = correct;//we have a hit
      }else{//incorrect btn
        pressed = fault;//false press
        break;//abort scan
      }//()
    }//if(btn[i])
  }//for(i)
  switch(pressed){//act on press
    case correct:
      currentCtrl = 1;//reuse currentCtrl for display
      ledCtrl(currentCtrl);
      Serial.println("Hit");
      game.Set(success);//change state
      break;//case correct
    case fault:
      ledCtrl(15);//all leds on
      Serial.println("Wrong button");
      game.Set(miss);//change state
      break;//case fault
  }//switch(pressed)
}//randomLed()

State miss(){
  if(game.Timeout(1000)){
    ledCtrl(0);//all leds off
    Serial.println("Back to start from miss");
    game.Set(gameStart);
  }//()
}//miss()

State success(){
  if(game.Timeout(300)){//wait 300ms
    currentCtrl <<= 1;//shift 1 bit
      if(currentCtrl<(1<<noControls)){//more ledsd to show?
        ledCtrl(currentCtrl);
        game.Set(success);
      }else{//back to start
        ledCtrl(0);//all leds off
        Serial.println("back to start from success");
        game.Set(gameStart);//change state
      }
  }//if(wait 300 ms)()
}//success()

inline void ledCtrl(byte ctrl){
  for(byte i = 0; i<noControls; i++) digitalWrite(led[i], !!(ctrl&(1<<i)));
}//ledCtrl()

I have asked you before to put your code in code tags. And please put links in url tags as well. There are button above the text entry field for that.

Terribly sorry, I think I have them correctly wired and stuff but maby your view of the wiring is different to mine.
Also note I cant use pin 4.

Heres sketch of my breadboard using Fritzing.
http://s1122.photobucket.com/user/Dennis_Pratap/media/Timer_zpsa6337af4.jpg.html

I'm not sure there is anything wrong with the wiring but you can change something or if you could make me a sketch that works with your code that would be much appreciated!.

I thank you for your time and help!

Post the fritzing file as an attachment instead, that would be easier.
What printout from the serial monitor do you get when you run the last program?

Each second the serial monitor displays random gibberish like h9FB ÇM’É9FB˜a etc..
also the built in led between pin 13 and gnd blinks every second as well.
The 1st code you have me showed nothing but the last one you gave was something like this:

ñ:i“„"’É9FB i1Ö:6“€)p¦ "	)q¦ e—îFǘ+e’‰:i“„eR19FG˜:ižÖ:6“„˜b¦°FG˜eVÄM9FF˜eVÄM:i“„:ižÖeFÌM9FG˜+ž`â:6“€e—æeVÄ	*6“€)q¦ e–î:i“„h1Ö+e’‰9q·i1Ö+e’‰FG˜9FF˜’‰+e’‰:ižÖh1Ö)q® "	+žñ:i“„h1Ö+e’‰)q® i1Ö*6“€

have you got the setting for the serial monitor right?

Ohhhhh... my bad I didn't set it to 115200baud
now this one last problem.
I'm not pressing anything

Started with:1
Cheat!
Back to start from miss
Started with:1
Cheat!
Back to start from miss
Started with:3
Cheat!
Back to start from miss
Started with:3
Cheat!
Back to start from miss
Started with:0
Cheat!
Back to start from miss
Started with:0
Cheat!
Back to start from miss
Started with:2
Cheat!
Back to start from miss
Started with:0
Cheat!
Back to start from miss
Started with:3
Cheat!
Back to start from miss
Started with:0
Cheat!
Back to start from miss
Started with:0
Cheat!
Back to start from miss
Started with:0
Cheat!
Back to start from miss
Started with:1
Cheat!
Back to start from miss
Started with:3
Cheat!
Back to start from miss
Started with:1
Cheat!
Back to start from miss
Started with:1
Cheat!
Back to start from miss
Started with:0
Cheat!
Back to start from miss
Started with:1
Cheat!
Back to start from miss
Started with:0
Cheat!
Back to start from miss
Started with:1
Cheat!
Back to start from miss
Started with:2
Cheat!
Back to start from miss
Started with:0
Cheat!
Back to start from miss
Started with:2
Cheat!
Back to start from miss
Started with:3
Cheat!
Back to start from miss
Started with:3
Cheat!
Back to start from miss
Started with:2
Cheat!
Back to start from miss
Started with:0
Cheat!
Back to start from miss
Started with:3
Cheat!
Back to start from miss
Started with:0
Cheat!
Back to start from miss
Started with:0
Cheat!
Back to start from miss
Started with:3
Cheat!
Back to start from miss
Started with:0
Cheat!

I'm not touching the switches but I think I know what's wrong, I think it's because the switches are programmed or wired in reverse thats why it keeps doing that. When I hold 4 buttons down the led's seem to work ok.
Thx for your help.

Yes, it seems so. You could either change the wiring or you could put a ! (not) before every digitalRead()

sketch_dec24a.ino: In function 'void ledCtrl(byte)':
sketch_dec24a:92: error: 'a' was not declared in this scope

Idk how to change them since it's a different style of coding that I'm not used to :confused: can you show how it's supposed be?

Dude really, when I put ! (not) before digital read it doesnt work, please help me out :c