Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*program runs a reaction test
- */
- #define POT1 0
- #define BUTTON1 10
- #define BUTTON2 11
- #define LED1 5
- #define BUZZER 3 //refers to what digital pin these items are connected to
- #define LATCH 7
- #define CLOCK 8
- #define DATA 4
- #include <EEPROM.h>
- const byte three = B10110000;
- const byte four = B10011001;
- const byte five= B10010010; //declares the binary of these numbers
- const byte six = B10000010;
- const byte seven = B11111000;
- const byte eight =B10000000;
- const byte nine = B10010000;
- int i;
- int goes;
- int randomnum;
- float reactiontime;
- float startTime; //these are declared variables that will be used in the program
- float stopTime;
- float average;
- float total;
- float num;
- float bestaveragesession;
- int bestaverage;
- word savebest;
- word best_L;
- word best_H;
- void setup()
- {
- Serial.begin(9600);
- pinMode(BUTTON1, INPUT);
- pinMode(BUTTON2, INPUT);
- pinMode(LED1, OUTPUT); //initializes the digital pins to either input or output
- pinMode(LATCH, OUTPUT);
- pinMode(CLOCK, OUTPUT);
- pinMode(DATA, OUTPUT);
- pinMode(POT1, INPUT);
- pinMode(BUZZER, OUTPUT);
- }
- void loop()
- {
- savebest = word(EEPROM.read(1),EEPROM.read(0));
- bestaverage = int(savebest);
- goes = selectgoes(); //calls function that lets the user choose the amount of goes they would like
- i = 0;
- total = 0; //resets the variables to these numbers
- while (i < goes) //until the desired number of goes is reached
- {
- reactiontime = reactiontimer();//calls the reaction timer
- Serial.print("Your reaction time is: ");
- Serial.print(reactiontime ); //prints out the reaction time
- Serial.print(" milliseconds");
- Serial.println();
- total = total + reactiontime; //works out the total
- i = i + 1; //adds one onto i
- }
- average = total / goes; //works out average time
- tone(BUZZER,1000, 500);
- delay(500);
- tone(BUZZER,2000, 500); //series of beeps to tell the user they are done
- delay(500);
- tone(BUZZER,3000, 500);
- Serial.print("Your total reaction times were: "); //prints the total
- Serial.print(total);
- Serial.print(" milliseconds");
- Serial.println();
- Serial.print("Your average reaction time is: "); //prints the average
- Serial.print(average);
- Serial.print(" milliseconds");
- Serial.println();
- if (average < bestaveragesession || bestaveragesession == 0 )
- {
- bestaveragesession = average; //works out if the new average is better than the current best in the session
- }
- if (average < bestaverage || bestaverage == 0 || bestaverage == 0.00)
- {
- bestaverage = int(average);
- savebest = word(bestaverage);
- best_L = lowByte(savebest);
- best_H = highByte(savebest);
- EEPROM.write(0,best_L);
- EEPROM.write(1,best_H);
- }
- savebest = word(EEPROM.read(1),EEPROM.read(0));
- bestaverage = int(savebest);
- Serial.print("The best average reaction time this session is: ");
- Serial.print(bestaveragesession); //prints out the best average
- Serial.print(" milliseconds");
- Serial.println();
- Serial.print("The best average reaction time ever is: ");
- Serial.print(bestaverage); //prints out the best ever
- Serial.print(" milliseconds");
- Serial.println();
- Serial.println();
- }
- float reactiontimer()
- {
- randomnum = random(3000,7000); //picks a random number from 3 to 7
- delay(randomnum); //delays for the period of the random number
- startTime = millis(); //declares when the start time is in milliseconds
- while (digitalRead(BUTTON1) == HIGH) //until the button is pressed the light will stay on
- {
- digitalWrite(LED1, HIGH);
- }
- digitalWrite(LED1, LOW);// the lkight will switch off when the button is pressed
- stopTime = millis();//declares the end time in milliseconds
- reactiontime = stopTime - startTime; //calculates time between
- // reactiontime = reactiontime/1000;// converts milliseconds to seconds
- return reactiontime;//returns the reaction time
- }
- float selectgoes()
- {
- while (digitalRead(BUTTON2) == HIGH) //loops round until the button is pressed
- {
- if (analogRead(POT1) < 146) // if the frequency of the potentiometer is in the fields shown then it will perform the next tasks
- {
- digitalWrite(LATCH, LOW);
- shiftOut(DATA, CLOCK, MSBFIRST, three);//changes the 7 segment display to show a 3
- digitalWrite(LATCH,HIGH);
- goes = 3;
- }
- if (analogRead(POT1) > 146 && analogRead(POT1) < 293)
- {
- digitalWrite(LATCH, LOW);
- shiftOut(DATA, CLOCK, MSBFIRST, four);//changes the 7 segment display to show a 4
- digitalWrite(LATCH,HIGH);
- goes = 4;
- }
- if (analogRead(POT1) > 293 && analogRead(POT1) < 438)
- {
- digitalWrite(LATCH, LOW);
- shiftOut(DATA, CLOCK, MSBFIRST, five);//changes the 7 segment display to show a 5
- digitalWrite(LATCH,HIGH);
- goes = 5;
- }
- if (analogRead(POT1) > 438 && analogRead(POT1) < 584)
- {
- digitalWrite(LATCH, LOW);
- shiftOut(DATA, CLOCK, MSBFIRST, six);//changes the 7 segment display to show a 6
- digitalWrite(LATCH,HIGH);
- goes = 6;
- }
- if (analogRead(POT1) > 584 && analogRead(POT1) < 730)
- {
- digitalWrite(LATCH, LOW);
- shiftOut(DATA, CLOCK, MSBFIRST, seven);//changes the 7 segment display to show a 7
- digitalWrite(LATCH,HIGH);
- goes = 7;
- }
- if (analogRead(POT1) > 730 && analogRead(POT1) < 876)
- {
- digitalWrite(LATCH, LOW);
- shiftOut(DATA, CLOCK, MSBFIRST, eight);//changes the 7 segment display to show a 8
- digitalWrite(LATCH,HIGH);
- goes = 8;
- }
- if (analogRead(POT1) > 876 && analogRead(POT1) < 1023)
- {
- digitalWrite(LATCH, LOW);
- shiftOut(DATA, CLOCK, MSBFIRST, nine);//changes the 7 segment display to show a 9
- digitalWrite(LATCH,HIGH);
- goes = 9;
- }
- }
- tone(BUZZER,1000, 1000);//a one second beep is played to tell the user the game has started
- return goes;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement