Sourcecode Problem with a reactiongame

Hey there,

i could really use some help. I´m working on a projekt, there i don´t continue at the sourcecode.
I want to build a reactgame.
There are three LEDs (Red, Yellow and Green) and three buttons. One LED should be switched on and than you have to push the right button. I have done the random delay, when the LED is switch on. Also what i have done this that the LED is random selected to switch on. In my Opinion, my circuit is working very well. On Request i can take a picture.

My Problem at the moment is that the LEDs don´t turn on by my If-clauses, but they turn on if I say them in the code. My other Problem is that i don´t know how to calculate the reaction time in milliseconds.

I really need some help. I had speak to my Teachers and they also have no solution to slove the problem.
I hope you can help :confused:

Sourcecode Reaction.txt (2.99 KB)

Sourcecode Reactiontime + Interrupt.txt (3.25 KB)

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Thanks, that way we can all see your sketch… Tom… :slight_smile:

#include <LiquidCrystal.h>

LiquidCrystal lcd( 0, 1, 6, 7, 9, 10);

const int led_R = 11;                        
const int button_R = 2;                      
      int button_R_status = 0;               

const int led_GE = 12;                       
const int button_GE = 3;                     
      int button_GE_status = 0;              

const int led_GR = 13;                       
const int button_GR = 4;                      
      int button_GR_status = 0;              

      long randDelay = random(1000, 5000);
      int  randLed = random (11,14);
      long reactiontime = 0;
      long starttime = 0;
      long endtime = 0;
    //  int  fortentimes = 0;
      long counter = 1000;



void clearAll()              
{
    digitalWrite(led_R, LOW);
    digitalWrite(led_GE, LOW);
    digitalWrite(led_GR, LOW);
}

void setup()
{  
  lcd.begin(16,1);
  lcd.print("Hello, World");
  Serial.begin(9600);

  Serial.println ("begin ");

  randomSeed(analogRead(A0));
  
  pinMode(led_R, OUTPUT);
  pinMode(led_GE, OUTPUT);
  pinMode(led_GR, OUTPUT);

  pinMode(button_R, INPUT);
  pinMode(button_GE, INPUT);
  pinMode(button_GR, INPUT);
}
 

void loop()
{ 
  button_R_status=digitalRead(button_R);
  button_GE_status=digitalRead(button_GE);
  button_GR_status= digitalRead(button_GR);


   counter = counter+1;
  
 // clearAll();
  
  //digitalWrite(randLed,HIGH);

  Serial.println(counter);
  Serial.println(randLed);
  Serial.println(randDelay);
  Serial.println(button_R_status);
  Serial.println(button_GE_status);
  Serial.println(button_GR_status);
  Serial.println(starttime);

  if (counter == randDelay) 
      {
        digitalWrite(randLed, HIGH); 
        starttime = counter;
 
      }



      
        if  ( randLed == 11 && button_GE_status == HIGH || randLed == 11 && button_GR_status == HIGH  || 
              randLed == 12 && button_R_status == HIGH  || randLed == 12 && button_GR_status == HIGH  ||
              randLed == 13 && button_R_status == HIGH  || randLed == 13 && button_GE_status == HIGH )
   
                {
                  digitalWrite(led_R, LOW);
                  digitalWrite(led_GE, LOW);
                  digitalWrite(led_GR, LOW);

                  Serial.println("Leider der falscher Knopf!");
                  counter = 0;
                  starttime = 0;
                  endtime = 0;
                  reactiontime = 0;
                }

  
        if (randLed == 11 && button_R_status == HIGH)
                {
                  digitalWrite(randLed ,LOW);
                  endtime = counter;
                }

        if (randLed == 12 && button_GE_status == HIGH )
                {
                  digitalWrite(randLed ,LOW);
                  endtime = counter;
                }

        if (randLed == 13 && button_GR_status == HIGH)
                {
                  digitalWrite(randLed ,LOW);
                  endtime = counter;  
                }

 }
#include <LiquidCrystal.h>

LiquidCrystal lcd( 0, 1, 6, 7, 9, 10);

const int led_R = 11;                        
const int button_R = 1;                      
      int button_R_status = 0;               

const int led_GE = 12;                       
const int button_GE = 3;                     
      int button_GE_status = 0;              

const int led_GR = 13;                      
const int button_GR = 4;                     
      int button_GR_status = 0;              

      long randDelay = 0;
      int  randLed = 0;
      long reactiontime = 0;
      long starttime = 0;
      long endtime = 0;
      int  fortentimes = 0;
      long counter = 1000;
      int ip = 2;           //the interrupt Pin
      int pin_ip_status = 0;


void clearAll()               //Eine Methode zum ausschalten aller LEDs
{
    digitalWrite(led_R, LOW);
    digitalWrite(led_GE, LOW);
    digitalWrite(led_GR, LOW);
}

void setup()
{  
  lcd.begin(16,1);
  lcd.print("Hello, World");
  Serial.begin(9600);
  
  attachInterrupt(digitalPinToInterrupt 2,buttonQuery, HIGH);
  
  Serial.println ("Start einer neuen Nummer");

  randomSeed(analogRead(A0));

  pinMode(ip, INPUT);
  pinMode(led_R, OUTPUT);
  pinMode(led_GE, OUTPUT);
  pinMode(led_GR, OUTPUT);

  pinMode(button_R, INPUT);
  pinMode(button_GE, INPUT);
  pinMode(button_GR, INPUT);
}
 

void loop()
{ 
  for( fortentimes; fortentimes <= 10; fortentimes++)
  {
  button_R_status=digitalRead(button_R);
  button_GE_status=digitalRead(button_GE);
  button_GR_status= digitalRead(button_GR);
  pin_ip_status= digitalRead(ip);

  clearAll();

  randDelay = random(1000, 5000);
  randLed = random (11,14);
  
  //digitalWrite(randLed,HIGH);

  Serial.println(randLed);
  Serial.println(randDelay);
  Serial.println(button_R_status);
  Serial.println(button_GE_status);
  Serial.println(button_GR_status);

      delay(randDelay);
      digitalWrite(randLed, HIGH);

      digitalWrite(ip, LOW);
       

 }

  void buttonQuery()
  { 
    if  ( randLed == 11 && button_GE_status == HIGH || randLed == 11 && button_GR_status == HIGH  || 
              randLed == 12 && button_R_status == HIGH  || randLed == 12 && button_GR_status == HIGH  ||
              randLed == 13 && button_R_status == HIGH  || randLed == 13 && button_GE_status == HIGH )
   
                {
                  digitalWrite(led_R, LOW);
                  digitalWrite(led_GE, LOW);
                  digitalWrite(led_GR, LOW);

                  Serial.println("Leider der falscher Knopf!");
                  counter = 0;
                  starttime = 0;
                  endtime = 0;
                  reactiontime = 0;
                }

  
        if (randLed == 11 && button_R_status == HIGH)
                {
                  digitalWrite(randLed ,LOW);
                  endtime = counter;
                }

        if (randLed == 12 && button_GE_status == HIGH )
                {
                  digitalWrite(randLed ,LOW);
                  endtime = counter;
                }

        if (randLed == 13 && button_GR_status == HIGH)
                {
                  digitalWrite(randLed ,LOW);
                  endtime = counter;  
                }
  
  }
}

You know when we say "don't put serial I/O in an interrupt" ? Well, we really mean it.

P3t3rs0hn: I really need some help.

Which Arduino board do you use?

With most Arduino boards, which have Serial on pin-0 and pin-1, this is a problem:

  LiquidCrystal lcd( 0, 1, 6, 7, 9, 10);
...
  Serial.begin(9600);

pin-0 and pin-1 can only be used for a LCD if it is a board where "Serial" is NOT on pin-0/pin-1.

I'm fairly certain that second code doesn't compile.

jurs:
Which Arduino board do you use?

I´m using a Arduino UNO.
I just need some help with the coding that the LEDs turn and stay on and than the button was pressed the LED turn off. Also after 10 runs should there be a display which show the average reaction time.

Here you go i drawed the basic circuit.

P3t3rs0hn: I´m using a Arduino UNO. ... Here you go i drawed the basic circuit.

With an Arduino UNO pin-0 and pin-1 are assigned to "Serial".

So if you are using "Serial" in your sketch, you cannot use pin-1 as a digital button input at the same time.

When using "Serial" on an UNO, DO NOT USE PIN-0/PIN-1 FOR DIFFERENT HARDWARE!