Created
January 26, 2017 14:09
-
-
Save intepra/12efa8267e3cee6bd5a655ce9cd988a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define BUZZER_PIN 12 // пин с пищалкой | |
#define ledPin 13 // светодиод | |
#define yLedPin 11 // пин с пищалкой | |
#define buttonPin 3 // кнопка | |
bool state = 0; | |
long startTime = 0; | |
int reactionTime = 0; | |
void setup() | |
{ | |
pinMode(BUZZER_PIN, OUTPUT); | |
pinMode(ledPin, OUTPUT); | |
pinMode(buttonPin, INPUT_PULLUP); | |
pinMode(yLedPin, OUTPUT); | |
Serial.begin(9600); | |
} | |
void loop() | |
{ | |
// даём сигнал «пли!», выждав случайное время от 2 до 7 сек | |
delay(random(2000, 7000)); | |
digitalWrite(yLedPin, HIGH); | |
startTime = millis(); | |
tone(BUZZER_PIN, 3000, 250); // 3 килогерца, 250 миллисекунд | |
state = 1; | |
while (state == 1){ | |
// если игрок номер «player» нажал кнопку... | |
if (!digitalRead(buttonPin)) { | |
// ...включаем его светодиод и сигнал победы на 1 сек | |
reactionTime = millis() - startTime; | |
Serial.println(reactionTime); | |
digitalWrite(yLedPin, LOW); | |
digitalWrite(ledPin, HIGH); | |
tone(BUZZER_PIN, 4000, 1000); | |
delay(1000); | |
digitalWrite(ledPin, LOW); | |
state = 0; | |
//break; // Есть победитель! Выходим (англ. break) из цикла | |
} | |
if (millis() - startTime > 5000){ | |
digitalWrite(yLedPin, LOW); | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment