Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 #include "TextLCD.h" 00003 #define freqB 0.002024 00004 //min and max for the start of the round 00005 #define MAX 10 00006 #define MIN 3 00007 //min and max for the time between the rounds 00008 #define MIN2 5 00009 #define MAX2 10 00010 00011 /* 00012 analog value pin used to generate random numbers 00013 buzzer used to announce the start of the round 00014 lcd used to show the game results 00015 startButton - button used to start a new game 00016 button1 - button used in the game by player 1 00017 button2 - button used in the game by player 2 00018 debounce - timer used for the buttons debouncing 00019 t - timer used to measure a round's time 00020 battle - timer used to measure the players' reaction time 00021 winnner - variable which is used to decide which is the winner of a round 00022 win1 - variable which holds the number of rounds won by player 1 00023 win2 - variable which holds the number of rounds won by player 2 00024 t1 - the reaction time of player 1 in a round 00025 t2 - the reaction time of player 2 in a round 00026 avg1 - average reaction time of player 1 00027 avg2 - average reaction time of player 2 00028 */ 00029 AnalogIn analog_value(A0); 00030 DigitalOut buzzer(D11); 00031 TextLCD lcd(D2,D3,D4,D5,D6,D7); 00032 Timer debounce, t, battle; 00033 InterruptIn startButton(D8); 00034 InterruptIn button1(D9); 00035 InterruptIn button2(D10); 00036 void start(void); 00037 void resetGame(void); 00038 void press1(void); 00039 void press2(void); 00040 void playB(void); 00041 volatile int winner = 0, win1 = 0, win2 = 0; 00042 float t1, t2, avg1, avg2; 00043 00044 int main() { 00045 debounce.start(); 00046 00047 //set the buttons to pull up resistance mode 00048 startButton.mode(PullUp); 00049 button1.mode(PullUp); 00050 button2.mode(PullUp); 00051 00052 //set the function for each interrupt button 00053 startButton.rise(&resetGame); 00054 button1.rise(&press1); 00055 button2.rise(&press2); 00056 } 00057 00058 void resetGame(){ 00059 if(debounce.read_ms() > 50){ 00060 //reset the stats for players at the start of a game 00061 avg1 = 0; 00062 avg2 = 0; 00063 win1 = 0; 00064 win2 = 0; 00065 start(); 00066 } 00067 debounce.reset(); 00068 } 00069 00070 void start() { 00071 lcd.cls(); 00072 float f = analog_value.read(); 00073 srand(f*1000); 00074 t.reset(); //reset the round timer 00075 t.start(); 00076 float r = (rand() % (MAX + 1 - MIN)) + MIN; //generating the random number using the analog pin 00077 wait(r); 00078 battle.reset(); //reset the timer for the reaction times 00079 battle.start(); 00080 winner = 0; 00081 playB(); //buzzer plays the B note 00082 } 00083 00084 void press1() { 00085 if(debounce.read_ms() > 50){ 00086 t1 = battle.read(); //read the reaction time 00087 avg1 += t1; //add it to the average reaction time sum 00088 if(winner == 0) //if player 1 is the first to press the button, increment the winner variable 00089 winner++; 00090 else { //if not, that means that the button has been already pressed, which means that player 2 won the round 00091 win2++; 00092 //print the reaction times 00093 lcd.cls(); 00094 lcd.printf("Loser: %f\n", t1); 00095 lcd.printf("Winner: %f\n", t2); 00096 wait(1); 00097 lcd.cls(); 00098 lcd.printf("Player 2 wins!\n"); 00099 wait(1); 00100 lcd.printf("Diff: %f\n", t1-t2); 00101 if((win1 + win2) < 3){ //if the game is not over yet(the players did not play 3 rounds yet), start a new round 00102 float f = analog_value.read(); 00103 srand(f*1000); 00104 float r = (rand() % (MAX + 1 - MIN)) + MIN; 00105 wait(r); 00106 start(); //starts a new round 00107 } 00108 else if(win1 > win2){ //the game is over, player 1 won more rounds 00109 wait(1); 00110 lcd.cls(); 00111 lcd.printf("P1 is the winner!\n"); 00112 wait(2); 00113 lcd.cls(); 00114 lcd.printf("Avg P1: %f\n", avg1/3); 00115 wait(1); 00116 lcd.printf("Avg P2: %f\n", avg2/3); 00117 } 00118 else { //the game is over, player 2 won more rounds 00119 wait(1); 00120 lcd.cls(); 00121 lcd.printf("P2 is the winner!\n"); 00122 wait(2); 00123 lcd.cls(); 00124 lcd.printf("Avg P1: %f\n", avg1/3); 00125 wait(1); 00126 lcd.printf("Avg P2: %f\n", avg2/3); 00127 } 00128 } 00129 } 00130 debounce.reset(); 00131 } 00132 00133 void press2() { 00134 if(debounce.read_ms() > 50){ 00135 t2 = battle.read(); //read the reaction time 00136 avg2 += t2; //add it to the average reaction time sum 00137 if(winner == 0) //if player 2 is the first to press the button, increment the winner variable 00138 winner++; 00139 else { //if not, that means that the button has been already pressed, which means that player 1 won the round 00140 win1++; 00141 //print the reaction times 00142 lcd.cls(); 00143 lcd.printf("Loser: %f\n", t2); 00144 lcd.printf("Winner: %f\n", t1); 00145 wait(1); 00146 lcd.cls(); 00147 lcd.printf("Player 1 wins!\n"); 00148 wait(1); 00149 lcd.printf("Diff: %f\n", t2-t1); 00150 if((win1 + win2) < 3){ //if the game is not over yet(the players did not play 3 rounds yet), start a new round 00151 float f = analog_value.read(); 00152 srand(f*1000); 00153 float r = (rand() % (MAX + 1 - MIN)) + MIN; 00154 wait(r); 00155 start(); //starts a new round 00156 } 00157 else if(win1 > win2){ //the game is over, player 1 won more rounds 00158 wait(1); 00159 lcd.cls(); 00160 lcd.printf("P1 is the winner1"); 00161 wait(2); 00162 lcd.cls(); 00163 lcd.printf("Avg P1: %f\n", avg1/3); 00164 wait(1); 00165 lcd.printf("Avg P2: %f\n", avg2/3); 00166 } 00167 else { //the game is over, player 2 won more rounds 00168 wait(1); 00169 lcd.cls(); 00170 lcd.printf("P2 is the winner!"); 00171 wait(2); 00172 lcd.cls(); 00173 lcd.printf("Avg P1: %f\n", avg1/3); 00174 wait(1); 00175 lcd.printf("Avg P2: %f\n", avg2/3); 00176 } 00177 } 00178 } 00179 debounce.reset(); 00180 } 00181 00182 //function used to play the B note 00183 void playB(){ 00184 for(int i = 0; i < 100; i++){ 00185 wait(freqB/2); 00186 buzzer.write(1); 00187 wait(freqB/2); 00188 buzzer.write(0); 00189 } 00190 }
Generated on Mon Aug 22 2022 17:20:41 by
1.7.2