- Sets
Test your reaction!
Reaction test with Arduino and our bricks
We’ll present our reaction game to you today that we did with our bricks and the Arduino Nano. Below, you’ll find the according code and you can watch the YouTube video here.
Have fun programming and testing your reaction!
Once you’ve connected the battery you are in the main menu. To get to the first level you need to shortly press the button. The yellow onboard LED will lighten up for a certain amount of time in which you need to keep pressing the button. If you successfully do this, the yellow LED brick will lighten up and you will enter the next level.
The time you have to press the button becomes shorter and shorter. After three failed attempts, that means, when the red LED lightens up, you’ll get back to the main menu. Once you’ve managed the last level, both LEDs will lighten up.
Here is the code for the Arduino:
/*
Authors:Michael Steidl, Maria Hoffmann
Date: 04.06.2016
Licence: GPL v2
Reaction game
*/
#define GREENLED 2
#define REDLED 3
#define SWITCH 4
int GELBELED = 13;
int reactionTime = 500;
int subTime = 50;
int winningTime = 150;
int errors = 0;
boolean game = false;
void menu();
void ingame();
void resetAllValues();
void setup()
{
pinMode(GREENLED, OUTPUT);
pinMode(REDLED, OUTPUT);
pinMode(SWITCH, INPUT_PULLUP);
pinMode(GELBELED, OUTPUT);
}
void resetAllValues()
{
int reactionTime = 500;
int subTime = 50;
int winningTime = 150;
int errors = 0;
}
void menu()
{
if(digitalRead(SWITCH)==LOW)
{
game = true;
}
digitalWrite(GREENLED, HIGH);
digitalWrite(REDLED, HIGH);
delay(200);
if(digitalRead(SWITCH)==LOW)
{
game = true;
}
digitalWrite(GREENLED, LOW);
digitalWrite(REDLED, LOW);
delay(200);
}
void ingame()
{
delay(3000);
digitalWrite(GELBELED,HIGH);
delay(reactionTime);
digitalWrite(GELBELED,LOW);
if(digitalRead(SWITCH)==LOW) //gedrückt
{
//Decrease Reaction Time
reactionTime = reactionTime – subTime;
if(reactionTime <= winningTime || reactionTime <= 0)
{
game = false;
resetAllValues();
}
digitalWrite(GREENLED,HIGH);
delay(1500);
digitalWrite(GREENLED,LOW);
}
else
{
errors++;
if(errors >= 3)
{
game = false;
resetAllValues();
}
digitalWrite(REDLED,HIGH);
delay(1500);
digitalWrite(REDLED,LOW);
}
}
void loop()
{
if(game == false)
{
menu();
}
else
{
ingame();
}
}