daily pastebin goal
0%
SHARE
TWEET

Untitled

a guest Mar 23rd, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. #include <LiquidCrystal.h>
  4. #include <stdint.h>
  5.  
  6. #define LED 9
  7. #define PUSH 8
  8. #define TIMEOUT 10000
  9.  
  10. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  11.  
  12. static int ms;
  13. static boolean reset;
  14. static String msRecord;
  15.  
  16. void setup(){
  17.   lcd.begin(16, 2);
  18.   pinMode(LED,OUTPUT);
  19.   pinMode(PUSH,INPUT);
  20.  
  21.   reset = true; //Stop timer acc
  22.  
  23.   /* Configure interrupt on Timer1 */
  24.   cli();
  25.   TCCR1A = 0;     //Init timer
  26.   TCCR1B = 0;
  27.   OCR1A = 16000;  //16MHz * 16000 = 1000us = 1ms
  28.   TCCR1B |= (1 << WGM12); //Clear time on match
  29.   TCCR1B |= 0x01;  // Set CS10 and CS12 bits to 1
  30.   TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
  31.   sei();
  32.  
  33.   for(;;) {
  34.     //Ready
  35.     lcd.clear();
  36.     lcd.setCursor(0, 0);
  37.     lcd.print("Ready!");
  38.     digitalWrite(LED, 0);
  39.     delay(random(2000,4000)); //Delay for 5 to 20 second, it is random
  40.    
  41.     //Turn on LED, start timer
  42.     digitalWrite(LED,1);
  43.     reset = false; //Enable timer acc
  44.  
  45.     //Wait until user push button, record
  46.     while(!digitalRead(PUSH) && ms < TIMEOUT);
  47.     msRecord = String(ms);
  48.     digitalWrite(LED,0);
  49.     reset = true; //Stop timer acc
  50.  
  51.     if (ms < TIMEOUT) {
  52.       lcd.clear();
  53.       lcd.setCursor(0, 0);
  54.       lcd.print("Reaction time:");
  55.       lcd.setCursor(2, 1);
  56.       lcd.print( msRecord ); //Get digitals, transfer to ASCII
  57.       lcd.print(" ms");
  58.     }
  59.     else {
  60.       lcd.clear();
  61.       lcd.setCursor(0, 0);
  62.       lcd.print("Timeout!");
  63.     }
  64.     delay(1000);
  65.    
  66.     //Wait until user push button, next game
  67.     while(!digitalRead(PUSH));
  68.   }
  69. }
  70.  
  71. ISR(TIMER1_COMPA_vect){ // This function runs once every time timer compare matches
  72.   if (reset)
  73.     ms = 0;
  74.   else
  75.     ms++;
  76. }
  77.  
  78.  
  79. void loop(){
  80.  
  81.  
  82. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top