Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***********************************************
- * Reaction Time Game For Arduino Nano *
- * by *
- * learnelectronics *
- * *
- * www.youtube.com/learnelectronics *
- * email: [email protected] *
- * *
- ***********************************************/
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #define OLED_RESET 4
- Adafruit_SSD1306 display(OLED_RESET);
- const int ledPin = 5; // LED connected to digital pin 5
- const int switchPin = 8; // Switch connected to digital pin 8
- const int minDelay = 1000; // Minimum delay before LED turns green (in milliseconds)
- const int maxDelay = 5000; // Maximum delay before LED turns green (in milliseconds)
- void setup() {
- pinMode(ledPin, OUTPUT);
- pinMode(switchPin, INPUT_PULLUP);
- Serial.begin(9600);
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
- display.display();
- delay(500);
- display.clearDisplay();
- // Seed the random number generator
- randomSeed(analogRead(0));
- // Wait for a moment before starting
- delay(2000);
- }
- void loop() {
- // Generate a random delay before the LED turns green
- int delayTime = random(minDelay, maxDelay);
- delay(delayTime);
- // Turn on the LED (green)
- digitalWrite(ledPin, HIGH);
- // Wait for the player to press the switch
- unsigned long startTime = millis();
- while (digitalRead(switchPin) == HIGH) {
- // Wait for the switch to be pressed
- }
- unsigned long endTime = millis();
- // Turn off the LED
- digitalWrite(ledPin, LOW);
- // Calculate and display the reaction time
- unsigned long reactionTime = endTime - startTime;
- Serial.print("Reaction Time: ");
- Serial.print(reactionTime);
- Serial.println(" ms");
- display.clearDisplay();
- display.setTextSize(1);
- display.setTextColor(WHITE);
- display.setCursor(0,0);
- display.clearDisplay();
- display.println("Reaction");
- display.print("Time: ");
- display.print(reactionTime);
- display.println("ms");
- display.display();
- // Wait for a moment before the next round
- delay(2000);
- display.clearDisplay();
- display.setTextSize(1);
- display.setTextColor(WHITE);
- display.setCursor(0,0);
- display.clearDisplay();
- display.println("Get Ready!");
- display.display();
- delay(1000);
- display.clearDisplay();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement