Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* REACTION TIME (with 2 leds) v1.1
- * 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
- */
- int switchPin = 6; // pin where the button will be connected
- int ledPin1 = 2 ; // LED that signals starting of the game
- int ledPin2 = 8 ; // LED that lights to test the reaction time
- // declare some variables:
- boolean lastButton = LOW;
- boolean currentButton = LOW;
- boolean Started = false;
- boolean timer = false;
- long startTime;
- long endTime;
- int randomTime;
- long beginTime;
- float elapsedTime;
- void setup()
- {
- // Setup button and LEDs:
- pinMode(switchPin, INPUT);
- pinMode(ledPin1, OUTPUT);
- pinMode(ledPin2, OUTPUT);
- // Begin serial communication
- Serial.begin(9600);
- }
- boolean debounce(boolean last)
- {
- boolean current = digitalRead(switchPin);
- if(last != current)
- {
- delay(5);
- current = digitalRead(switchPin);
- }
- return current;
- }
- void loop()
- {
- // see if button pressed
- currentButton = debounce(lastButton);
- if(lastButton == LOW && currentButton == HIGH)
- {
- if(Started==false){
- Started=true;
- randomTime = random(4,10);
- randomTime = randomTime*1000;
- Blink();
- beginTime=millis();
- }
- else{
- if((millis()-beginTime)>=randomTime){
- Stop();
- Started=false;
- timer=false;
- }
- else{
- Started=false;
- timer=false;
- Serial.println("You pressed the button too soon !");
- for(int i=0; i<3; i++){
- Blink();
- }
- }
- }
- }
- lastButton = currentButton;
- if(Started == true && (millis()-beginTime)>=randomTime && timer==false){
- Serial.println("Start");
- timer=true;
- Start();
- }
- }
- void Start(){
- startTime = millis();
- digitalWrite(ledPin1, HIGH);
- }
- void Blink(){
- digitalWrite(ledPin2, HIGH);
- delay(100);
- digitalWrite(ledPin2, LOW);
- delay(100);
- }
- void Stop(){
- endTime = millis();
- elapsedTime = (endTime - startTime)+5;
- elapsedTime = elapsedTime/1000;
- Serial.print("Time Seconds: ");
- Serial.println(elapsedTime);
- digitalWrite(ledPin1, LOW);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement