Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2016
266
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* REACTION TIME (with 2 leds) v2.1 beta
  2.     Luis Andrés Gonzalez
  3.    Reaction time original version from http://www.instructables.com/id/Arduino-Reaction-Time-Tester/?ALLSTEPS
  4.    Send data to processing via the Serial Port original from By Elaine Laguerta http://url/of/online/tutorial.cc
  5. */
  6. const int switchPin = 6;  // pin where the button will be connected
  7. const int ledPin1 = 2 ;   // Left LED
  8. const int ledPin2 = 8 ;   // Middle LED
  9. const int ledPin3 = 12;   // Right LED
  10.  
  11. // declare some variables:
  12. boolean lastButton = LOW;
  13. boolean currentButton = LOW;
  14. boolean gameStarted = false;  // true if game has started
  15. boolean timerFlag = false;    // true if timer is running
  16. long startTime;
  17. long endTime;
  18. int randomSeconds;
  19. long beginTime;
  20. float elapsedTime;
  21. int maxTimer = 5 * 1000;
  22. float totalTime;
  23.  
  24.  
  25.  
  26. void setup() {
  27.   // Setup button and LEDs:
  28.   pinMode(switchPin, INPUT);
  29.   pinMode(ledPin1, OUTPUT);
  30.   pinMode(ledPin2, OUTPUT);
  31.   pinMode(ledPin3, OUTPUT);
  32.  
  33.   // Begin serial communication
  34.   Serial.begin(9600);
  35. }
  36.  
  37. void loop() {
  38.   // Check https://www.arduino.cc/en/Tutorial/StateChangeDetection to understand the following code.
  39.   if ((millis() - beginTime) > (randomSeconds + maxTimer)) {
  40.     Stop();
  41.    
  42.     //resets game:
  43.     gameStarted = false;
  44.     timerFlag = false;
  45.     currentButton = LOW;
  46.     lastButton = LOW;
  47.    
  48.    
  49.     Blink(2);
  50.     Serial.println("message");
  51.  
  52.   }
  53.   // read the pushbutton input pin in the current loop:
  54.   currentButton = buttonPressed(lastButton);
  55.  
  56.   // if there's a change from low to high (comparing last and current loop) then the following is true
  57.   if (currentButton == HIGH && lastButton == LOW) {   // outer IF
  58.  
  59.     if (gameStarted == false) {                       // middle IF. starts the game
  60.       gameStarted = true;
  61.       randomSeconds = random(2, 5) * 1000; // generates a random number of seconds between 3 and 8
  62.       Blink(1);
  63.       Serial.println("9090"); // signal code for start sound
  64.       beginTime = millis();
  65.     } else {
  66.  
  67.       if ((millis() - beginTime) >= randomSeconds) {
  68.         Stop();
  69.         gameStarted = false;
  70.         timerFlag = false;
  71.  
  72.       } else if ((millis() - beginTime) < randomSeconds ) {
  73.         gameStarted = false;
  74.         timerFlag = false;
  75.         Serial.println("1010"); // signal code for early response
  76.         Blink(3);
  77.       }
  78.  
  79.     }
  80.   }                             // end outer if
  81.  
  82.   // save the current state as the last state,
  83.   //for next time through the loop
  84.   lastButton = currentButton;
  85.  
  86.  
  87.  
  88.  
  89.   // If true, starts the response time timer and lights up the LED
  90.   if (gameStarted == true && (millis() - beginTime) >= randomSeconds && timerFlag == false) {
  91.     timerFlag = true;
  92.  
  93.     Start();
  94.  
  95.   }
  96.  
  97. } // end void loop
  98.  
  99. //===========================================================================================
  100.  
  101. boolean buttonPressed(boolean last) {       //button debouncing function
  102.   boolean current = digitalRead(switchPin);
  103.   if (last != current) {
  104.     delay(5);
  105.     current = digitalRead(switchPin);
  106.   }
  107.   return current;
  108. }
  109.  
  110. void Start() {
  111.  
  112.   startTime = millis();
  113.   Light(ledPin1, "on");
  114.  
  115. }
  116.  
  117. void Stop() {
  118.   if ( (millis() - beginTime) <= maxTimer ) {
  119.     endTime = millis();
  120.     elapsedTime = (endTime - startTime) + 5;
  121.     float elapsedSeconds = elapsedTime / 1000;
  122.     Serial.println(elapsedSeconds);
  123.   }
  124.   else Serial.println("Timed out");
  125.   Light(ledPin1, "off");
  126.  
  127. }
  128.  
  129. void Blink(int repetitions) {
  130.   for (int i = 0; i < repetitions; i++) {
  131.     digitalWrite(ledPin2, HIGH);
  132.     delay(100);
  133.     digitalWrite(ledPin2, LOW);
  134.     delay(100);
  135.   }
  136. }
  137.  
  138. void Light(int lednumber, String onOff) {
  139.   if (onOff == "on") {
  140.     digitalWrite(lednumber, HIGH);
  141.   } else if (onOff == "off") {
  142.     digitalWrite(lednumber, LOW);
  143.   }
  144.  
  145. }
Advertisement
RAW Paste Data Copied
Advertisement