-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReactionTimer.ino
More file actions
37 lines (31 loc) · 953 Bytes
/
ReactionTimer.ino
File metadata and controls
37 lines (31 loc) · 953 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const int ledPin = 13;
const int buttonPin = 2;
unsigned long startTime;
unsigned long reactionTime;
bool waitingForPress = false;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // Internal pull-up activated
Serial.begin(9600);
randomSeed(analogRead(0)); // Seed random delay
}
void loop() {
Serial.println("Get ready...");
delay(2000); // Wait before starting
int randomDelay = random(2000, 5000); // 2–5 seconds
delay(randomDelay);
digitalWrite(ledPin, HIGH); // LED ON = press button now
startTime = millis();
waitingForPress = true;
while (waitingForPress) {
if (digitalRead(buttonPin) == LOW) { // Button pressed
reactionTime = millis() - startTime;
digitalWrite(ledPin, LOW);
Serial.print("Your reaction time: ");
Serial.print(reactionTime);
Serial.println(" ms");
waitingForPress = false;
delay(2000); // Pause before next round
}
}
}