Advertisement
Guest User

Untitled

a guest
Jan 19th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. /* REACTION TIME (with 2 leds) v1.1
  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. int switchPin = 6;  // pin where the button will be connected
  7. int ledPin1 = 2 ;   // LED that signals starting of the game
  8. int ledPin2 = 8 ;   // LED that lights to test the reaction time
  9.  
  10. // declare some variables:
  11. boolean lastButton = LOW;
  12. boolean currentButton = LOW;
  13. boolean Started = false;
  14. boolean timer = false;
  15. long startTime;
  16. long endTime;
  17. int randomTime;
  18. long beginTime;
  19. float elapsedTime;
  20.  
  21.  
  22. void setup()
  23. {
  24.   // Setup button and LEDs:
  25.   pinMode(switchPin, INPUT);
  26.   pinMode(ledPin1, OUTPUT);
  27.   pinMode(ledPin2, OUTPUT);
  28.  
  29.   // Begin serial communication
  30.   Serial.begin(9600);
  31. }
  32. boolean debounce(boolean last)
  33. {
  34.   boolean current = digitalRead(switchPin);
  35.   if(last != current)
  36.   {
  37.     delay(5);
  38.     current = digitalRead(switchPin);
  39.   }
  40.   return current;
  41. }
  42.  
  43.  
  44. void loop()
  45. {
  46.   // see if button pressed
  47.   currentButton = debounce(lastButton);
  48.   if(lastButton == LOW && currentButton == HIGH)
  49.   {
  50.     if(Started==false){
  51.       Started=true;
  52.       randomTime = random(4,10);
  53.       randomTime = randomTime*1000;
  54.       Blink();
  55.       beginTime=millis();
  56.      
  57.     }
  58.     else{
  59.       if((millis()-beginTime)>=randomTime){
  60.           Stop();
  61.           Started=false;
  62.           timer=false;        
  63.       }
  64.      
  65.       else{
  66.         Started=false;
  67.         timer=false;
  68.         Serial.println("You pressed the button too soon !");
  69.         for(int i=0; i<3; i++){
  70.           Blink();
  71.         }
  72.       }
  73.     }
  74.   }
  75.      
  76.   lastButton = currentButton;
  77.  
  78.   if(Started == true && (millis()-beginTime)>=randomTime && timer==false){
  79.     Serial.println("Start");
  80.     timer=true;
  81.     Start();
  82.   }
  83. }
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90. void Start(){
  91.   startTime = millis();
  92.   digitalWrite(ledPin1, HIGH);
  93. }
  94.  
  95. void Blink(){
  96.   digitalWrite(ledPin2, HIGH);
  97.   delay(100);
  98.   digitalWrite(ledPin2, LOW);
  99.   delay(100);
  100. }
  101.  
  102. void Stop(){
  103.   endTime = millis();
  104.   elapsedTime = (endTime - startTime)+5;
  105.   elapsedTime = elapsedTime/1000;
  106.   Serial.print("Time Seconds: ");
  107.   Serial.println(elapsedTime);
  108.   digitalWrite(ledPin1, LOW);
  109.    
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement