Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2016
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  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) && gameStarted == true) {
  40. Stop();
  41.  
  42. //resets game:
  43. gameStarted = false;
  44. timerFlag = false;
  45. currentButton = LOW;
  46. lastButton = LOW;
  47.  
  48.  
  49. Blink(2);
  50. }
  51. // read the pushbutton input pin in the current loop:
  52. currentButton = buttonPressed(lastButton);
  53.  
  54. // if there's a change from low to high (comparing last and current loop) then the following is true
  55. if (currentButton == HIGH && lastButton == LOW) { // outer IF
  56.  
  57. if (gameStarted == false) { // middle IF. starts the game
  58. gameStarted = true;
  59. randomSeconds = random(2, 5) * 1000; // generates a random number of seconds between 3 and 8
  60. Blink(1);
  61. beginTime = millis();
  62. } else {
  63.  
  64. if ((millis() - beginTime) >= randomSeconds) {
  65. Stop();
  66. gameStarted = false;
  67. timerFlag = false;
  68.  
  69. } else if ((millis() - beginTime) < randomSeconds ) {
  70. gameStarted = false;
  71. timerFlag = false;
  72. Serial.println("1010"); // signal code for early response
  73. Blink(3);
  74. }
  75.  
  76. }
  77. } // end outer if
  78.  
  79. // save the current state as the last state,
  80. //for next time through the loop
  81. lastButton = currentButton;
  82.  
  83.  
  84.  
  85.  
  86. // If true, starts the response time timer and lights up the LED
  87. if (gameStarted == true && (millis() - beginTime) >= randomSeconds && timerFlag == false) {
  88. timerFlag = true;
  89. Serial.println("9090"); // signal code for start sound
  90. Start();
  91.  
  92. }
  93.  
  94. } // end void loop
  95.  
  96. //===========================================================================================
  97.  
  98. boolean buttonPressed(boolean last) { //button debouncing function
  99. boolean current = digitalRead(switchPin);
  100. if (last != current) {
  101. delay(5);
  102. current = digitalRead(switchPin);
  103. }
  104. return current;
  105. }
  106.  
  107. void Start() {
  108.  
  109. startTime = millis();
  110. Light(ledPin1, "on");
  111.  
  112. }
  113.  
  114. void Stop() {
  115. if ( (millis() - beginTime) <= maxTimer ) {
  116. endTime = millis();
  117. elapsedTime = (endTime - startTime);
  118. float elapsedSeconds = elapsedTime / 1000.0;
  119. Serial.println(elapsedSeconds);
  120. }
  121. else Serial.println("Timed out");
  122. Light(ledPin1, "off");
  123.  
  124. }
  125.  
  126. void Blink(int repetitions) {
  127. for (int i = 0; i < repetitions; i++) {
  128. digitalWrite(ledPin2, HIGH);
  129. delay(100);
  130. digitalWrite(ledPin2, LOW);
  131. delay(100);
  132. }
  133. }
  134.  
  135. void Light(int lednumber, String onOff) {
  136. if (onOff == "on") {
  137. digitalWrite(lednumber, HIGH);
  138. } else if (onOff == "off") {
  139. digitalWrite(lednumber, LOW);
  140. }
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement