Dragos Andrei Brasoveanu
/
Nucleo_pwm23
3rd year projects
main.cpp@0:4608832d38d5, 2019-11-04 (annotated)
- Committer:
- andreib10
- Date:
- Mon Nov 04 23:19:26 2019 +0000
- Revision:
- 0:4608832d38d5
all projects
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andreib10 | 0:4608832d38d5 | 1 | #include "mbed.h" |
andreib10 | 0:4608832d38d5 | 2 | #include "TextLCD.h" |
andreib10 | 0:4608832d38d5 | 3 | #define freqB 0.002024 |
andreib10 | 0:4608832d38d5 | 4 | //min and max for the start of the round |
andreib10 | 0:4608832d38d5 | 5 | #define MAX 10 |
andreib10 | 0:4608832d38d5 | 6 | #define MIN 3 |
andreib10 | 0:4608832d38d5 | 7 | //min and max for the time between the rounds |
andreib10 | 0:4608832d38d5 | 8 | #define MIN2 5 |
andreib10 | 0:4608832d38d5 | 9 | #define MAX2 10 |
andreib10 | 0:4608832d38d5 | 10 | |
andreib10 | 0:4608832d38d5 | 11 | /* |
andreib10 | 0:4608832d38d5 | 12 | analog value pin used to generate random numbers |
andreib10 | 0:4608832d38d5 | 13 | buzzer used to announce the start of the round |
andreib10 | 0:4608832d38d5 | 14 | lcd used to show the game results |
andreib10 | 0:4608832d38d5 | 15 | startButton - button used to start a new game |
andreib10 | 0:4608832d38d5 | 16 | button1 - button used in the game by player 1 |
andreib10 | 0:4608832d38d5 | 17 | button2 - button used in the game by player 2 |
andreib10 | 0:4608832d38d5 | 18 | debounce - timer used for the buttons debouncing |
andreib10 | 0:4608832d38d5 | 19 | t - timer used to measure a round's time |
andreib10 | 0:4608832d38d5 | 20 | battle - timer used to measure the players' reaction time |
andreib10 | 0:4608832d38d5 | 21 | winnner - variable which is used to decide which is the winner of a round |
andreib10 | 0:4608832d38d5 | 22 | win1 - variable which holds the number of rounds won by player 1 |
andreib10 | 0:4608832d38d5 | 23 | win2 - variable which holds the number of rounds won by player 2 |
andreib10 | 0:4608832d38d5 | 24 | t1 - the reaction time of player 1 in a round |
andreib10 | 0:4608832d38d5 | 25 | t2 - the reaction time of player 2 in a round |
andreib10 | 0:4608832d38d5 | 26 | avg1 - average reaction time of player 1 |
andreib10 | 0:4608832d38d5 | 27 | avg2 - average reaction time of player 2 |
andreib10 | 0:4608832d38d5 | 28 | */ |
andreib10 | 0:4608832d38d5 | 29 | AnalogIn analog_value(A0); |
andreib10 | 0:4608832d38d5 | 30 | DigitalOut buzzer(D11); |
andreib10 | 0:4608832d38d5 | 31 | TextLCD lcd(D2,D3,D4,D5,D6,D7); |
andreib10 | 0:4608832d38d5 | 32 | Timer debounce, t, battle; |
andreib10 | 0:4608832d38d5 | 33 | InterruptIn startButton(D8); |
andreib10 | 0:4608832d38d5 | 34 | InterruptIn button1(D9); |
andreib10 | 0:4608832d38d5 | 35 | InterruptIn button2(D10); |
andreib10 | 0:4608832d38d5 | 36 | void start(void); |
andreib10 | 0:4608832d38d5 | 37 | void resetGame(void); |
andreib10 | 0:4608832d38d5 | 38 | void press1(void); |
andreib10 | 0:4608832d38d5 | 39 | void press2(void); |
andreib10 | 0:4608832d38d5 | 40 | void playB(void); |
andreib10 | 0:4608832d38d5 | 41 | volatile int winner = 0, win1 = 0, win2 = 0; |
andreib10 | 0:4608832d38d5 | 42 | float t1, t2, avg1, avg2; |
andreib10 | 0:4608832d38d5 | 43 | |
andreib10 | 0:4608832d38d5 | 44 | int main() { |
andreib10 | 0:4608832d38d5 | 45 | debounce.start(); |
andreib10 | 0:4608832d38d5 | 46 | |
andreib10 | 0:4608832d38d5 | 47 | //set the buttons to pull up resistance mode |
andreib10 | 0:4608832d38d5 | 48 | startButton.mode(PullUp); |
andreib10 | 0:4608832d38d5 | 49 | button1.mode(PullUp); |
andreib10 | 0:4608832d38d5 | 50 | button2.mode(PullUp); |
andreib10 | 0:4608832d38d5 | 51 | |
andreib10 | 0:4608832d38d5 | 52 | //set the function for each interrupt button |
andreib10 | 0:4608832d38d5 | 53 | startButton.rise(&resetGame); |
andreib10 | 0:4608832d38d5 | 54 | button1.rise(&press1); |
andreib10 | 0:4608832d38d5 | 55 | button2.rise(&press2); |
andreib10 | 0:4608832d38d5 | 56 | } |
andreib10 | 0:4608832d38d5 | 57 | |
andreib10 | 0:4608832d38d5 | 58 | void resetGame(){ |
andreib10 | 0:4608832d38d5 | 59 | if(debounce.read_ms() > 50){ |
andreib10 | 0:4608832d38d5 | 60 | //reset the stats for players at the start of a game |
andreib10 | 0:4608832d38d5 | 61 | avg1 = 0; |
andreib10 | 0:4608832d38d5 | 62 | avg2 = 0; |
andreib10 | 0:4608832d38d5 | 63 | win1 = 0; |
andreib10 | 0:4608832d38d5 | 64 | win2 = 0; |
andreib10 | 0:4608832d38d5 | 65 | start(); |
andreib10 | 0:4608832d38d5 | 66 | } |
andreib10 | 0:4608832d38d5 | 67 | debounce.reset(); |
andreib10 | 0:4608832d38d5 | 68 | } |
andreib10 | 0:4608832d38d5 | 69 | |
andreib10 | 0:4608832d38d5 | 70 | void start() { |
andreib10 | 0:4608832d38d5 | 71 | lcd.cls(); |
andreib10 | 0:4608832d38d5 | 72 | float f = analog_value.read(); |
andreib10 | 0:4608832d38d5 | 73 | srand(f*1000); |
andreib10 | 0:4608832d38d5 | 74 | t.reset(); //reset the round timer |
andreib10 | 0:4608832d38d5 | 75 | t.start(); |
andreib10 | 0:4608832d38d5 | 76 | float r = (rand() % (MAX + 1 - MIN)) + MIN; //generating the random number using the analog pin |
andreib10 | 0:4608832d38d5 | 77 | wait(r); |
andreib10 | 0:4608832d38d5 | 78 | battle.reset(); //reset the timer for the reaction times |
andreib10 | 0:4608832d38d5 | 79 | battle.start(); |
andreib10 | 0:4608832d38d5 | 80 | winner = 0; |
andreib10 | 0:4608832d38d5 | 81 | playB(); //buzzer plays the B note |
andreib10 | 0:4608832d38d5 | 82 | } |
andreib10 | 0:4608832d38d5 | 83 | |
andreib10 | 0:4608832d38d5 | 84 | void press1() { |
andreib10 | 0:4608832d38d5 | 85 | if(debounce.read_ms() > 50){ |
andreib10 | 0:4608832d38d5 | 86 | t1 = battle.read(); //read the reaction time |
andreib10 | 0:4608832d38d5 | 87 | avg1 += t1; //add it to the average reaction time sum |
andreib10 | 0:4608832d38d5 | 88 | if(winner == 0) //if player 1 is the first to press the button, increment the winner variable |
andreib10 | 0:4608832d38d5 | 89 | winner++; |
andreib10 | 0:4608832d38d5 | 90 | else { //if not, that means that the button has been already pressed, which means that player 2 won the round |
andreib10 | 0:4608832d38d5 | 91 | win2++; |
andreib10 | 0:4608832d38d5 | 92 | //print the reaction times |
andreib10 | 0:4608832d38d5 | 93 | lcd.cls(); |
andreib10 | 0:4608832d38d5 | 94 | lcd.printf("Loser: %f\n", t1); |
andreib10 | 0:4608832d38d5 | 95 | lcd.printf("Winner: %f\n", t2); |
andreib10 | 0:4608832d38d5 | 96 | wait(1); |
andreib10 | 0:4608832d38d5 | 97 | lcd.cls(); |
andreib10 | 0:4608832d38d5 | 98 | lcd.printf("Player 2 wins!\n"); |
andreib10 | 0:4608832d38d5 | 99 | wait(1); |
andreib10 | 0:4608832d38d5 | 100 | lcd.printf("Diff: %f\n", t1-t2); |
andreib10 | 0:4608832d38d5 | 101 | if((win1 + win2) < 3){ //if the game is not over yet(the players did not play 3 rounds yet), start a new round |
andreib10 | 0:4608832d38d5 | 102 | float f = analog_value.read(); |
andreib10 | 0:4608832d38d5 | 103 | srand(f*1000); |
andreib10 | 0:4608832d38d5 | 104 | float r = (rand() % (MAX + 1 - MIN)) + MIN; |
andreib10 | 0:4608832d38d5 | 105 | wait(r); |
andreib10 | 0:4608832d38d5 | 106 | start(); //starts a new round |
andreib10 | 0:4608832d38d5 | 107 | } |
andreib10 | 0:4608832d38d5 | 108 | else if(win1 > win2){ //the game is over, player 1 won more rounds |
andreib10 | 0:4608832d38d5 | 109 | wait(1); |
andreib10 | 0:4608832d38d5 | 110 | lcd.cls(); |
andreib10 | 0:4608832d38d5 | 111 | lcd.printf("P1 is the winner!\n"); |
andreib10 | 0:4608832d38d5 | 112 | wait(2); |
andreib10 | 0:4608832d38d5 | 113 | lcd.cls(); |
andreib10 | 0:4608832d38d5 | 114 | lcd.printf("Avg P1: %f\n", avg1/3); |
andreib10 | 0:4608832d38d5 | 115 | wait(1); |
andreib10 | 0:4608832d38d5 | 116 | lcd.printf("Avg P2: %f\n", avg2/3); |
andreib10 | 0:4608832d38d5 | 117 | } |
andreib10 | 0:4608832d38d5 | 118 | else { //the game is over, player 2 won more rounds |
andreib10 | 0:4608832d38d5 | 119 | wait(1); |
andreib10 | 0:4608832d38d5 | 120 | lcd.cls(); |
andreib10 | 0:4608832d38d5 | 121 | lcd.printf("P2 is the winner!\n"); |
andreib10 | 0:4608832d38d5 | 122 | wait(2); |
andreib10 | 0:4608832d38d5 | 123 | lcd.cls(); |
andreib10 | 0:4608832d38d5 | 124 | lcd.printf("Avg P1: %f\n", avg1/3); |
andreib10 | 0:4608832d38d5 | 125 | wait(1); |
andreib10 | 0:4608832d38d5 | 126 | lcd.printf("Avg P2: %f\n", avg2/3); |
andreib10 | 0:4608832d38d5 | 127 | } |
andreib10 | 0:4608832d38d5 | 128 | } |
andreib10 | 0:4608832d38d5 | 129 | } |
andreib10 | 0:4608832d38d5 | 130 | debounce.reset(); |
andreib10 | 0:4608832d38d5 | 131 | } |
andreib10 | 0:4608832d38d5 | 132 | |
andreib10 | 0:4608832d38d5 | 133 | void press2() { |
andreib10 | 0:4608832d38d5 | 134 | if(debounce.read_ms() > 50){ |
andreib10 | 0:4608832d38d5 | 135 | t2 = battle.read(); //read the reaction time |
andreib10 | 0:4608832d38d5 | 136 | avg2 += t2; //add it to the average reaction time sum |
andreib10 | 0:4608832d38d5 | 137 | if(winner == 0) //if player 2 is the first to press the button, increment the winner variable |
andreib10 | 0:4608832d38d5 | 138 | winner++; |
andreib10 | 0:4608832d38d5 | 139 | else { //if not, that means that the button has been already pressed, which means that player 1 won the round |
andreib10 | 0:4608832d38d5 | 140 | win1++; |
andreib10 | 0:4608832d38d5 | 141 | //print the reaction times |
andreib10 | 0:4608832d38d5 | 142 | lcd.cls(); |
andreib10 | 0:4608832d38d5 | 143 | lcd.printf("Loser: %f\n", t2); |
andreib10 | 0:4608832d38d5 | 144 | lcd.printf("Winner: %f\n", t1); |
andreib10 | 0:4608832d38d5 | 145 | wait(1); |
andreib10 | 0:4608832d38d5 | 146 | lcd.cls(); |
andreib10 | 0:4608832d38d5 | 147 | lcd.printf("Player 1 wins!\n"); |
andreib10 | 0:4608832d38d5 | 148 | wait(1); |
andreib10 | 0:4608832d38d5 | 149 | lcd.printf("Diff: %f\n", t2-t1); |
andreib10 | 0:4608832d38d5 | 150 | if((win1 + win2) < 3){ //if the game is not over yet(the players did not play 3 rounds yet), start a new round |
andreib10 | 0:4608832d38d5 | 151 | float f = analog_value.read(); |
andreib10 | 0:4608832d38d5 | 152 | srand(f*1000); |
andreib10 | 0:4608832d38d5 | 153 | float r = (rand() % (MAX + 1 - MIN)) + MIN; |
andreib10 | 0:4608832d38d5 | 154 | wait(r); |
andreib10 | 0:4608832d38d5 | 155 | start(); //starts a new round |
andreib10 | 0:4608832d38d5 | 156 | } |
andreib10 | 0:4608832d38d5 | 157 | else if(win1 > win2){ //the game is over, player 1 won more rounds |
andreib10 | 0:4608832d38d5 | 158 | wait(1); |
andreib10 | 0:4608832d38d5 | 159 | lcd.cls(); |
andreib10 | 0:4608832d38d5 | 160 | lcd.printf("P1 is the winner1"); |
andreib10 | 0:4608832d38d5 | 161 | wait(2); |
andreib10 | 0:4608832d38d5 | 162 | lcd.cls(); |
andreib10 | 0:4608832d38d5 | 163 | lcd.printf("Avg P1: %f\n", avg1/3); |
andreib10 | 0:4608832d38d5 | 164 | wait(1); |
andreib10 | 0:4608832d38d5 | 165 | lcd.printf("Avg P2: %f\n", avg2/3); |
andreib10 | 0:4608832d38d5 | 166 | } |
andreib10 | 0:4608832d38d5 | 167 | else { //the game is over, player 2 won more rounds |
andreib10 | 0:4608832d38d5 | 168 | wait(1); |
andreib10 | 0:4608832d38d5 | 169 | lcd.cls(); |
andreib10 | 0:4608832d38d5 | 170 | lcd.printf("P2 is the winner!"); |
andreib10 | 0:4608832d38d5 | 171 | wait(2); |
andreib10 | 0:4608832d38d5 | 172 | lcd.cls(); |
andreib10 | 0:4608832d38d5 | 173 | lcd.printf("Avg P1: %f\n", avg1/3); |
andreib10 | 0:4608832d38d5 | 174 | wait(1); |
andreib10 | 0:4608832d38d5 | 175 | lcd.printf("Avg P2: %f\n", avg2/3); |
andreib10 | 0:4608832d38d5 | 176 | } |
andreib10 | 0:4608832d38d5 | 177 | } |
andreib10 | 0:4608832d38d5 | 178 | } |
andreib10 | 0:4608832d38d5 | 179 | debounce.reset(); |
andreib10 | 0:4608832d38d5 | 180 | } |
andreib10 | 0:4608832d38d5 | 181 | |
andreib10 | 0:4608832d38d5 | 182 | //function used to play the B note |
andreib10 | 0:4608832d38d5 | 183 | void playB(){ |
andreib10 | 0:4608832d38d5 | 184 | for(int i = 0; i < 100; i++){ |
andreib10 | 0:4608832d38d5 | 185 | wait(freqB/2); |
andreib10 | 0:4608832d38d5 | 186 | buzzer.write(1); |
andreib10 | 0:4608832d38d5 | 187 | wait(freqB/2); |
andreib10 | 0:4608832d38d5 | 188 | buzzer.write(0); |
andreib10 | 0:4608832d38d5 | 189 | } |
andreib10 | 0:4608832d38d5 | 190 | } |