Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.47 KB | None | 0 0
  1. /*program runs a reaction test
  2. */
  3. #define POT1 0
  4. #define BUTTON1 10
  5. #define BUTTON2 11
  6. #define LED1 5
  7. #define BUZZER 3                      //refers to what digital pin these items are connected to
  8. #define LATCH 7
  9. #define CLOCK 8
  10. #define DATA 4
  11. #include <EEPROM.h>
  12.  
  13.  
  14. const byte three = B10110000;
  15. const byte four = B10011001;
  16. const byte five= B10010010;           //declares the binary of these numbers
  17. const byte six = B10000010;
  18. const byte seven = B11111000;
  19. const byte eight =B10000000;
  20. const byte nine = B10010000;
  21.  
  22.   int i;
  23.   int goes;
  24.   int randomnum;
  25.   float reactiontime;
  26.   float startTime;                   //these are declared variables that will be used in the program
  27.   float stopTime;
  28.   float average;
  29.   float total;
  30.   float num;
  31.   float bestaveragesession;
  32.   int bestaverage;
  33.   word savebest;
  34.   word best_L;
  35.   word best_H;
  36.  
  37.   void setup()
  38.   {
  39.     Serial.begin(9600);
  40.     pinMode(BUTTON1, INPUT);
  41.     pinMode(BUTTON2, INPUT);
  42.     pinMode(LED1, OUTPUT);               //initializes the digital pins to either input or output
  43.     pinMode(LATCH, OUTPUT);
  44.     pinMode(CLOCK, OUTPUT);
  45.     pinMode(DATA, OUTPUT);
  46.     pinMode(POT1, INPUT);
  47.     pinMode(BUZZER, OUTPUT);
  48.  
  49.   }
  50.   void loop()
  51.   {
  52.    savebest = word(EEPROM.read(1),EEPROM.read(0));
  53.    bestaverage = int(savebest);
  54.    goes = selectgoes();            //calls function that lets the user choose the amount of goes they would like
  55.    i = 0;
  56.    total = 0;                        //resets the variables to these numbers
  57.    while (i < goes)            //until the desired number of goes is reached
  58.    {
  59.      
  60.      reactiontime = reactiontimer();//calls the reaction timer
  61.      Serial.print("Your reaction time is: ");
  62.      Serial.print(reactiontime );          //prints out the reaction time
  63.       Serial.print(" milliseconds");
  64.      Serial.println();
  65.     total = total + reactiontime;          //works out the total
  66.      i = i + 1;                          //adds one onto i
  67.    }
  68.    
  69.   average = total / goes;            //works out average time
  70.     tone(BUZZER,1000, 500);
  71.     delay(500);
  72.     tone(BUZZER,2000, 500);                    //series of beeps to tell the user they are done
  73.     delay(500);
  74.     tone(BUZZER,3000, 500);
  75.     Serial.print("Your total reaction times were: ");            //prints the total
  76.     Serial.print(total);
  77.      Serial.print(" milliseconds");
  78.     Serial.println();
  79.     Serial.print("Your average reaction time is: ");              //prints the average
  80.     Serial.print(average);
  81.      Serial.print(" milliseconds");
  82.     Serial.println();
  83.     if (average < bestaveragesession || bestaveragesession == 0 )
  84.     {
  85.        bestaveragesession = average;        //works out if the new average is better than the current best in the session
  86.     }
  87.    
  88.       if (average <  bestaverage || bestaverage == 0 || bestaverage == 0.00)
  89.       {
  90.           bestaverage = int(average);      
  91.           savebest = word(bestaverage);
  92.           best_L = lowByte(savebest);
  93.           best_H = highByte(savebest);
  94.           EEPROM.write(0,best_L);
  95.           EEPROM.write(1,best_H);
  96.       }
  97.    
  98.     savebest = word(EEPROM.read(1),EEPROM.read(0));
  99.     bestaverage = int(savebest);
  100.     Serial.print("The best average reaction time this session is: ");
  101.     Serial.print(bestaveragesession);    //prints out the best average
  102.      Serial.print(" milliseconds");
  103.     Serial.println();
  104.     Serial.print("The best average reaction time ever is: ");
  105.     Serial.print(bestaverage);      //prints out the best ever
  106.     Serial.print(" milliseconds");
  107.     Serial.println();
  108.     Serial.println();
  109.    
  110.   }
  111.    
  112.   float reactiontimer()
  113.   {
  114.     randomnum = random(3000,7000);    //picks a random number from 3 to 7
  115.     delay(randomnum);      //delays for the period of the random number
  116.     startTime = millis();      //declares when the start time is in milliseconds
  117.     while (digitalRead(BUTTON1) == HIGH)    //until the button is pressed the light will stay on
  118.     {
  119.     digitalWrite(LED1, HIGH);
  120.     }
  121.     digitalWrite(LED1, LOW);// the lkight will switch off when the button is pressed
  122.     stopTime = millis();//declares the end time in milliseconds
  123.     reactiontime = stopTime - startTime; //calculates time between
  124.    // reactiontime = reactiontime/1000;// converts milliseconds to seconds
  125.    
  126.     return reactiontime;//returns the reaction time
  127.   }
  128.   float selectgoes()
  129.  {
  130.    while (digitalRead(BUTTON2) == HIGH) //loops round until the button is pressed
  131.    {
  132.     if (analogRead(POT1) < 146) // if the frequency of the potentiometer is in the fields shown then it will perform the next tasks
  133.     {
  134.           digitalWrite(LATCH, LOW);
  135.           shiftOut(DATA, CLOCK, MSBFIRST, three);//changes the 7 segment display to show a 3
  136.           digitalWrite(LATCH,HIGH);
  137.           goes = 3;
  138.     }
  139.          
  140.        if (analogRead(POT1) > 146 && analogRead(POT1) < 293)
  141.     {
  142.           digitalWrite(LATCH, LOW);
  143.           shiftOut(DATA, CLOCK, MSBFIRST, four);//changes the 7 segment display to show a 4
  144.           digitalWrite(LATCH,HIGH);
  145.           goes = 4;
  146.     }
  147.        if (analogRead(POT1) > 293 && analogRead(POT1) < 438)
  148.     {
  149.           digitalWrite(LATCH, LOW);
  150.           shiftOut(DATA, CLOCK, MSBFIRST, five);//changes the 7 segment display to show a 5
  151.           digitalWrite(LATCH,HIGH);
  152.           goes = 5;
  153.     }
  154.         if (analogRead(POT1) > 438 && analogRead(POT1) < 584)
  155.     {
  156.           digitalWrite(LATCH, LOW);
  157.           shiftOut(DATA, CLOCK, MSBFIRST, six);//changes the 7 segment display to show a 6
  158.           digitalWrite(LATCH,HIGH);
  159.           goes = 6;
  160.     }
  161.          if (analogRead(POT1) > 584 && analogRead(POT1) < 730)
  162.     {
  163.           digitalWrite(LATCH, LOW);
  164.           shiftOut(DATA, CLOCK, MSBFIRST, seven);//changes the 7 segment display to show a 7
  165.           digitalWrite(LATCH,HIGH);
  166.           goes = 7;
  167.     }
  168.          if (analogRead(POT1) > 730 && analogRead(POT1) < 876)
  169.     {
  170.           digitalWrite(LATCH, LOW);
  171.           shiftOut(DATA, CLOCK, MSBFIRST, eight);//changes the 7 segment display to show a 8
  172.           digitalWrite(LATCH,HIGH);
  173.           goes = 8;
  174.     }
  175.     if (analogRead(POT1) > 876 && analogRead(POT1) < 1023)
  176.     {
  177.           digitalWrite(LATCH, LOW);
  178.           shiftOut(DATA, CLOCK, MSBFIRST, nine);//changes the 7 segment display to show a 9
  179.           digitalWrite(LATCH,HIGH);
  180.           goes = 9;
  181.     }
  182.    }
  183.  
  184.    tone(BUZZER,1000, 1000);//a one second beep is played to tell the user the game has started
  185.    
  186.    return goes;
  187.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement