Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <LiquidCrystal.h>
- #include <stdint.h>
- #define LED 9
- #define PUSH 8
- #define TIMEOUT 10000
- LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
- static int ms;
- static boolean reset;
- static String msRecord;
- void setup(){
- lcd.begin(16, 2);
- pinMode(LED,OUTPUT);
- pinMode(PUSH,INPUT);
- reset = true; //Stop timer acc
- /* Configure interrupt on Timer1 */
- cli();
- TCCR1A = 0; //Init timer
- TCCR1B = 0;
- OCR1A = 16000; //16MHz * 16000 = 1000us = 1ms
- TCCR1B |= (1 << WGM12); //Clear time on match
- TCCR1B |= 0x01; // Set CS10 and CS12 bits to 1
- TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
- sei();
- for(;;) {
- //Ready
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Ready!");
- digitalWrite(LED, 0);
- delay(random(2000,4000)); //Delay for 5 to 20 second, it is random
- //Turn on LED, start timer
- digitalWrite(LED,1);
- reset = false; //Enable timer acc
- //Wait until user push button, record
- while(!digitalRead(PUSH) && ms < TIMEOUT);
- msRecord = String(ms);
- digitalWrite(LED,0);
- reset = true; //Stop timer acc
- if (ms < TIMEOUT) {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Reaction time:");
- lcd.setCursor(2, 1);
- lcd.print( msRecord ); //Get digitals, transfer to ASCII
- lcd.print(" ms");
- }
- else {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Timeout!");
- }
- delay(1000);
- //Wait until user push button, next game
- while(!digitalRead(PUSH));
- }
- }
- ISR(TIMER1_COMPA_vect){ // This function runs once every time timer compare matches
- if (reset)
- ms = 0;
- else
- ms++;
- }
- void loop(){
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement