Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* REACTION TIME (with 2 leds) v2.1 beta
- Luis Andrés Gonzalez
- Reaction time original version from http://www.instructables.com/id/Arduino-Reaction-Time-Tester/?ALLSTEPS
- Send data to processing via the Serial Port original from By Elaine Laguerta http://url/of/online/tutorial.cc
- */
- const int switchPin = 6; // pin where the button will be connected
- const int ledPin1 = 2 ; // Left LED
- const int ledPin2 = 8 ; // Middle LED
- const int ledPin3 = 12; // Right LED
- // declare some variables:
- boolean lastButton = LOW;
- boolean currentButton = LOW;
- boolean gameStarted = false; // true if game has started
- boolean timerFlag = false; // true if timer is running
- long startTime;
- long endTime;
- int randomSeconds;
- long beginTime;
- float elapsedTime;
- int maxTimer = 5 * 1000;
- float totalTime;
- void setup() {
- // Setup button and LEDs:
- pinMode(switchPin, INPUT);
- pinMode(ledPin1, OUTPUT);
- pinMode(ledPin2, OUTPUT);
- pinMode(ledPin3, OUTPUT);
- // Begin serial communication
- Serial.begin(9600);
- }
- void loop() {
- // Check https://www.arduino.cc/en/Tutorial/StateChangeDetection to understand the following code.
- if ((millis() - beginTime) > (randomSeconds + maxTimer) && gameStarted == true) {
- Stop();
- //resets game:
- gameStarted = false;
- timerFlag = false;
- currentButton = LOW;
- lastButton = LOW;
- Blink(2);
- }
- // read the pushbutton input pin in the current loop:
- currentButton = buttonPressed(lastButton);
- // if there's a change from low to high (comparing last and current loop) then the following is true
- if (currentButton == HIGH && lastButton == LOW) { // outer IF
- if (gameStarted == false) { // middle IF. starts the game
- gameStarted = true;
- randomSeconds = random(2, 5) * 1000; // generates a random number of seconds between 3 and 8
- Blink(1);
- beginTime = millis();
- } else {
- if ((millis() - beginTime) >= randomSeconds) {
- Stop();
- gameStarted = false;
- timerFlag = false;
- } else if ((millis() - beginTime) < randomSeconds ) {
- gameStarted = false;
- timerFlag = false;
- Serial.println("1010"); // signal code for early response
- Blink(3);
- }
- }
- } // end outer if
- // save the current state as the last state,
- //for next time through the loop
- lastButton = currentButton;
- // If true, starts the response time timer and lights up the LED
- if (gameStarted == true && (millis() - beginTime) >= randomSeconds && timerFlag == false) {
- timerFlag = true;
- Serial.println("9090"); // signal code for start sound
- Start();
- }
- } // end void loop
- //===========================================================================================
- boolean buttonPressed(boolean last) { //button debouncing function
- boolean current = digitalRead(switchPin);
- if (last != current) {
- delay(5);
- current = digitalRead(switchPin);
- }
- return current;
- }
- void Start() {
- startTime = millis();
- Light(ledPin1, "on");
- }
- void Stop() {
- if ( (millis() - beginTime) <= maxTimer ) {
- endTime = millis();
- elapsedTime = (endTime - startTime);
- float elapsedSeconds = elapsedTime / 1000.0;
- Serial.println(elapsedSeconds);
- }
- else Serial.println("Timed out");
- Light(ledPin1, "off");
- }
- void Blink(int repetitions) {
- for (int i = 0; i < repetitions; i++) {
- digitalWrite(ledPin2, HIGH);
- delay(100);
- digitalWrite(ledPin2, LOW);
- delay(100);
- }
- }
- void Light(int lednumber, String onOff) {
- if (onOff == "on") {
- digitalWrite(lednumber, HIGH);
- } else if (onOff == "off") {
- digitalWrite(lednumber, LOW);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement