Dragos Andrei Brasoveanu
/
Nucleo_pwm23
3rd year projects
Diff: main.cpp
- Revision:
- 0:4608832d38d5
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Nov 04 23:19:26 2019 +0000 @@ -0,0 +1,190 @@ +#include "mbed.h" +#include "TextLCD.h" +#define freqB 0.002024 +//min and max for the start of the round +#define MAX 10 +#define MIN 3 +//min and max for the time between the rounds +#define MIN2 5 +#define MAX2 10 + +/* +analog value pin used to generate random numbers +buzzer used to announce the start of the round +lcd used to show the game results +startButton - button used to start a new game +button1 - button used in the game by player 1 +button2 - button used in the game by player 2 +debounce - timer used for the buttons debouncing +t - timer used to measure a round's time +battle - timer used to measure the players' reaction time +winnner - variable which is used to decide which is the winner of a round +win1 - variable which holds the number of rounds won by player 1 +win2 - variable which holds the number of rounds won by player 2 +t1 - the reaction time of player 1 in a round +t2 - the reaction time of player 2 in a round +avg1 - average reaction time of player 1 +avg2 - average reaction time of player 2 +*/ +AnalogIn analog_value(A0); +DigitalOut buzzer(D11); +TextLCD lcd(D2,D3,D4,D5,D6,D7); +Timer debounce, t, battle; +InterruptIn startButton(D8); +InterruptIn button1(D9); +InterruptIn button2(D10); +void start(void); +void resetGame(void); +void press1(void); +void press2(void); +void playB(void); +volatile int winner = 0, win1 = 0, win2 = 0; +float t1, t2, avg1, avg2; + +int main() { + debounce.start(); + + //set the buttons to pull up resistance mode + startButton.mode(PullUp); + button1.mode(PullUp); + button2.mode(PullUp); + + //set the function for each interrupt button + startButton.rise(&resetGame); + button1.rise(&press1); + button2.rise(&press2); +} + +void resetGame(){ + if(debounce.read_ms() > 50){ + //reset the stats for players at the start of a game + avg1 = 0; + avg2 = 0; + win1 = 0; + win2 = 0; + start(); + } + debounce.reset(); +} + +void start() { + lcd.cls(); + float f = analog_value.read(); + srand(f*1000); + t.reset(); //reset the round timer + t.start(); + float r = (rand() % (MAX + 1 - MIN)) + MIN; //generating the random number using the analog pin + wait(r); + battle.reset(); //reset the timer for the reaction times + battle.start(); + winner = 0; + playB(); //buzzer plays the B note +} + +void press1() { + if(debounce.read_ms() > 50){ + t1 = battle.read(); //read the reaction time + avg1 += t1; //add it to the average reaction time sum + if(winner == 0) //if player 1 is the first to press the button, increment the winner variable + winner++; + else { //if not, that means that the button has been already pressed, which means that player 2 won the round + win2++; + //print the reaction times + lcd.cls(); + lcd.printf("Loser: %f\n", t1); + lcd.printf("Winner: %f\n", t2); + wait(1); + lcd.cls(); + lcd.printf("Player 2 wins!\n"); + wait(1); + lcd.printf("Diff: %f\n", t1-t2); + if((win1 + win2) < 3){ //if the game is not over yet(the players did not play 3 rounds yet), start a new round + float f = analog_value.read(); + srand(f*1000); + float r = (rand() % (MAX + 1 - MIN)) + MIN; + wait(r); + start(); //starts a new round + } + else if(win1 > win2){ //the game is over, player 1 won more rounds + wait(1); + lcd.cls(); + lcd.printf("P1 is the winner!\n"); + wait(2); + lcd.cls(); + lcd.printf("Avg P1: %f\n", avg1/3); + wait(1); + lcd.printf("Avg P2: %f\n", avg2/3); + } + else { //the game is over, player 2 won more rounds + wait(1); + lcd.cls(); + lcd.printf("P2 is the winner!\n"); + wait(2); + lcd.cls(); + lcd.printf("Avg P1: %f\n", avg1/3); + wait(1); + lcd.printf("Avg P2: %f\n", avg2/3); + } + } + } + debounce.reset(); +} + +void press2() { + if(debounce.read_ms() > 50){ + t2 = battle.read(); //read the reaction time + avg2 += t2; //add it to the average reaction time sum + if(winner == 0) //if player 2 is the first to press the button, increment the winner variable + winner++; + else { //if not, that means that the button has been already pressed, which means that player 1 won the round + win1++; + //print the reaction times + lcd.cls(); + lcd.printf("Loser: %f\n", t2); + lcd.printf("Winner: %f\n", t1); + wait(1); + lcd.cls(); + lcd.printf("Player 1 wins!\n"); + wait(1); + lcd.printf("Diff: %f\n", t2-t1); + if((win1 + win2) < 3){ //if the game is not over yet(the players did not play 3 rounds yet), start a new round + float f = analog_value.read(); + srand(f*1000); + float r = (rand() % (MAX + 1 - MIN)) + MIN; + wait(r); + start(); //starts a new round + } + else if(win1 > win2){ //the game is over, player 1 won more rounds + wait(1); + lcd.cls(); + lcd.printf("P1 is the winner1"); + wait(2); + lcd.cls(); + lcd.printf("Avg P1: %f\n", avg1/3); + wait(1); + lcd.printf("Avg P2: %f\n", avg2/3); + } + else { //the game is over, player 2 won more rounds + wait(1); + lcd.cls(); + lcd.printf("P2 is the winner!"); + wait(2); + lcd.cls(); + lcd.printf("Avg P1: %f\n", avg1/3); + wait(1); + lcd.printf("Avg P2: %f\n", avg2/3); + } + } + } + debounce.reset(); +} + +//function used to play the B note +void playB(){ + for(int i = 0; i < 100; i++){ + wait(freqB/2); + buzzer.write(1); + wait(freqB/2); + buzzer.write(0); + } +} \ No newline at end of file