Advertisement
learnelectronics

Reaction Time Game ][

Aug 22nd, 2023
1,057
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 2.47 KB | Gaming | 0 0
  1. /***********************************************
  2.  *     Reaction Time Game For Arduino Nano     *
  3.  *                       by                    *
  4.  *                learnelectronics             *
  5.  *                                             *
  6.  *        www.youtube.com/learnelectronics     *
  7.  *        email: [email protected]         *
  8.  *                                             *
  9.  ***********************************************/
  10.  
  11.  
  12.  
  13.  
  14. #include <Wire.h>
  15. #include <Adafruit_GFX.h>
  16. #include <Adafruit_SSD1306.h>
  17.  
  18. #define OLED_RESET 4
  19. Adafruit_SSD1306 display(OLED_RESET);
  20.  
  21. const int ledPin = 5;   // LED connected to digital pin 5
  22. const int switchPin = 8;  // Switch connected to digital pin 8
  23. const int minDelay = 1000;  // Minimum delay before LED turns green (in milliseconds)
  24. const int maxDelay = 5000;  // Maximum delay before LED turns green (in milliseconds)
  25.  
  26. void setup() {
  27.   pinMode(ledPin, OUTPUT);
  28.   pinMode(switchPin, INPUT_PULLUP);
  29.  
  30.   Serial.begin(9600);
  31.  
  32.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  33.   display.display();
  34.   delay(500);
  35.   display.clearDisplay();
  36.  
  37.   // Seed the random number generator
  38.   randomSeed(analogRead(0));
  39.  
  40.   // Wait for a moment before starting
  41.   delay(2000);
  42. }
  43.  
  44. void loop() {
  45.   // Generate a random delay before the LED turns green
  46.   int delayTime = random(minDelay, maxDelay);
  47.   delay(delayTime);
  48.  
  49.   // Turn on the LED (green)
  50.   digitalWrite(ledPin, HIGH);
  51.  
  52.   // Wait for the player to press the switch
  53.   unsigned long startTime = millis();
  54.   while (digitalRead(switchPin) == HIGH) {
  55.     // Wait for the switch to be pressed
  56.   }
  57.   unsigned long endTime = millis();
  58.  
  59.   // Turn off the LED
  60.   digitalWrite(ledPin, LOW);
  61.  
  62.   // Calculate and display the reaction time
  63.   unsigned long reactionTime = endTime - startTime;
  64.   Serial.print("Reaction Time: ");
  65.   Serial.print(reactionTime);
  66.   Serial.println(" ms");
  67.  
  68.   display.clearDisplay();
  69.   display.setTextSize(1);
  70.   display.setTextColor(WHITE);
  71.   display.setCursor(0,0);
  72.   display.clearDisplay();
  73.   display.println("Reaction");
  74.   display.print("Time: ");
  75.   display.print(reactionTime);
  76.   display.println("ms");
  77.   display.display();
  78.  
  79.   // Wait for a moment before the next round
  80.   delay(2000);
  81.  
  82.   display.clearDisplay();
  83.   display.setTextSize(1);
  84.   display.setTextColor(WHITE);
  85.   display.setCursor(0,0);
  86.   display.clearDisplay();
  87.   display.println("Get Ready!");
  88.  
  89.   display.display();
  90.   delay(1000);
  91.   display.clearDisplay();
  92.  
  93. }
  94.  
Tags: time ReAction
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement