// ---------------------------------------------------- // Filename : ReactoMeter.ino // Author : Sven Maerivoet & Martin Evans et al. // Last modified : 25/07/2015 // Target : C++ (Arduino UNO) // // All rights reserved. // ---------------------------------------------------- /* Copyright 2015 Sven Maerivoet Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // retain state variable during interrupts volatile int fState = LOW; volatile float fTime; volatile float fStartTime; // constants const int kLedArray[] = {8, 9, 10, 11, 12}; const int kStopLED = 6; const int kGoLED = 7; const int kMinRand = 500; const int kMaxRand = 1000; const int kLEDDelay = 100; const int kBounceDelay = 50; // ------------- // Setup routine // ------------- void setup() { for (int i = 0; i < 5; ++i) { pinMode(kLedArray[i],OUTPUT); } pinMode(kStopLED,OUTPUT); pinMode(kGoLED,OUTPUT); randomSeed((unsigned long) analogRead[0]); attachInterrupt(0,interruptLEDOnOff,RISING); Serial.begin(9600); } // ---------------------- // Loop execution routine // ---------------------- void loop() { // are we in the starting state if (fState == 0) { digitalWrite(kStopLED,HIGH); digitalWrite(kGoLED,LOW); for (int i = 0; i < 5; ++i) { digitalWrite(kLedArray[i],LOW); } } // was the start button pressed? if (fState == 1) { // random start int startDelay = random(kMinRand,kMaxRand); delay(startDelay); fStartTime = millis(); digitalWrite(kStopLED,LOW); digitalWrite(kGoLED,HIGH); for (int i = 0; i < 5; ++i) { digitalWrite(kLedArray[i],HIGH); delay(kLEDDelay); if (fState == 2) { fTime = (fTime - fStartTime) / 1000; Serial.print("Reaction time: "); Serial.print(fTime); Serial.println(" seconds"); delay(1000); break; } } } } // ----------------- // Interrupt routine // ----------------- void interruptLEDOnOff() { static unsigned long previousMillis = 0; unsigned long currentMillis = millis(); if ((currentMillis - previousMillis) < kBounceDelay) { } else { ++fState; if (fState == 3) { fState = 0; } previousMillis = currentMillis; } }