#include <LiquidCrystal.h>
// Initialize the library with the numbers of the interface pins
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const int buttonPin = 7;
const int ledPin = 8;
const int buzzerPin = 9;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Test Reaksi");
lcd.setCursor(0, 1);
lcd.print("Pencet utk mulai");
digitalWrite(ledPin, LOW);
digitalWrite(buzzerPin, LOW);
}
void loop() {
if (digitalRead(buttonPin) == HIGH) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Bersiaplah...");
// Wait for the button to be released to prevent immediate reaction
while(digitalRead(buttonPin) == HIGH) {}
// Random delay between 1 to 5 seconds
long randDelay = random(1000, 5000);
delay(randDelay);
// Signal start
digitalWrite(ledPin, HIGH);
digitalWrite(buzzerPin, HIGH);
unsigned long startTime = millis();
// Wait for the user to press the button
while (digitalRead(buttonPin) == LOW) {}
unsigned long endTime = millis();
digitalWrite(ledPin, LOW);
digitalWrite(buzzerPin, LOW);
unsigned long reactionTime = endTime - startTime;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Delay: ");
lcd.setCursor(0, 1);
lcd.print(reactionTime);
lcd.print(" ms");
delay(3000); // Display result for 3 seconds
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Pencet utk mulai");
lcd.setCursor(0, 1);
lcd.print("lagi");
}
}