- 关于我们
测评一下你的反应能力
用Arduino和Bricks做一次反应能力测评
今天,我们将给你展示一款测试反应能力的游戏,是将Bricks和Arduino Nano一起搭配起来玩的。以下是相应的代码,你也可以在YouTube 上观看视频:
祝您开心玩编程的同时,还能测评一下自己的反应能力
接上电池以后,你将会出现在主菜单。简单的按下开关,你就能进入第一级。接下来板子上的黄色LED会被点亮一段时间,这时你需要不停的按压开关。如果你成功的做到了这点,那么带有黄色LED的brick就会被点亮,你就可以进入到下一级了。按压开关所剩的时间越来越少,3次尝试失败以后,红色LED亮起,你将退回到主菜单。如果你成功进入了最后一级,两个LED将会同时亮起。
以下是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();
}
}