learnelectronics

Reaction Time Game

Apr 4th, 2023
3,073
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 2.22 KB | Gaming | 0 0
  1. /************************************
  2.  *        Reaction Time Game        *
  3.  *                                  *
  4.  *      learnelectronics 3/3/23     *
  5.  *       [email protected]      *
  6.  * www.youtube.com/learnelectronics *
  7.  *                                  *
  8.  ***********************************/
  9.  
  10. #include <LCD_I2C.h>                                            //I2C LCD Driver
  11. LCD_I2C lcd(0x27, 16, 2);                                       //setup lcd at hex addy 0x27, 16 columns, 2 rows
  12.  
  13. #define tacswitch 2                                             //tacswitch =2
  14. #define redled 8                                                //redled =8
  15. #define yelled 9                                                //yelled =9
  16. #define greled 10                                               //greled =10
  17.  
  18.   long starttime = millis();                                    //start counter
  19.   float redrand = random(100, 2000);                            // red led on for random time up to 2 seconds
  20.   float yelrand = random(100, 3000);                            // yel led on for random time up to 3 sec
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31. void setup() {
  32.  
  33.   Serial.begin(115200);
  34.  
  35.   pinMode(redled, OUTPUT);
  36.   pinMode(yelled, OUTPUT);
  37.   pinMode(greled, OUTPUT);
  38.   pinMode(tacswitch, INPUT_PULLUP);
  39.   digitalWrite(redled, LOW);
  40.   digitalWrite(yelled, LOW);
  41.   digitalWrite(greled, LOW);
  42.  
  43.   lcd.begin();
  44.   lcd.backlight();
  45.   lcd.print("Reaction Time");
  46.   lcd.setCursor(0,1);
  47.   lcd.print("   Game by LE");
  48.   delay(1000);
  49.   lcd.clear();
  50.   //lcd.noBacklight();
  51.  
  52.  
  53.  
  54.  
  55.   digitalWrite(redled,HIGH);
  56.   delay(redrand);
  57.   digitalWrite(redled,LOW);
  58.  
  59.   digitalWrite(yelled,HIGH);
  60.   delay(yelrand);
  61.   digitalWrite(yelled,LOW);
  62.  
  63.  
  64. }
  65.  
  66.  
  67. void loop() {
  68.  
  69.  
  70.   delay(100);
  71.  
  72.   digitalWrite(greled, HIGH);
  73.  
  74.   if (digitalRead(2) == 0){
  75.    
  76.   long stoptime = millis();
  77.   long mytime = (stoptime - starttime-1100-redrand-yelrand);
  78.   lcd.setCursor(0,0);
  79.   lcd.print("Your time is:");
  80.   lcd.setCursor(0,1);
  81.   lcd.print(mytime);
  82.   lcd.print(" mSec:");
  83.  
  84.  
  85. Serial.print("start - ");
  86. Serial.println(starttime);
  87. Serial.print("stop - ");
  88. Serial.println(stoptime);
  89. Serial.print("mytime - ");
  90. Serial.print(mytime);
  91.   }
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment