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.
Dependencies: mbed C12832 mbed-rtos
main.cpp
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 #include "Timer.h" 00004 #include "C12832.h" 00005 #include "stdio.h" 00006 #include "stdlib.h" 00007 // Samuel Pearson 19064819 00008 // Joystick push kills LED bit on left, if it is on 00009 // If bit is not on, another is created 00010 // Goal is to kill off all of the bits 00011 // LEDs flash and speaker beeps on a win 00012 00013 DigitalOut myled1(LED1); 00014 DigitalOut myled2(LED2); 00015 DigitalOut myled3(LED3); 00016 DigitalOut myled4(LED4); 00017 DigitalIn pb(p14); //the player hits the switch connected here to respond 00018 PwmOut spkr(p26); 00019 PwmOut RGBLED_r(p23); 00020 PwmOut RGBLED_g(p24); 00021 PwmOut RGBLED_b(p25); 00022 C12832 lcd(p5, p7, p6, p8, p11); 00023 00024 // mutex to make the lcd lib thread safe 00025 Mutex lcd_mutex; 00026 Mutex mtx; 00027 00028 Timer t1, t2; //used to measure the response time 00029 int player1, player2, player2Ready, StartGame, GameFinished = 0; 00030 float Player1Time, Player2Time = 0; 00031 00032 void display(int number) 00033 { 00034 myled1 = (number) & 0x01; 00035 myled2 = (number>>1) & 0x01; 00036 myled3 = (number>>2) & 0x01; 00037 myled4 = (number>>3) & 0x01; 00038 } 00039 00040 void measure (int value) // calls when the joystick is pressed 00041 { 00042 if (pb) 00043 { //Check if button is held 00044 lcd_mutex.lock(); 00045 lcd.cls(); 00046 lcd.printf("Button Press!\n"); 00047 lcd_mutex.unlock(); 00048 } 00049 } 00050 00051 void calculate (float T1, float T2) 00052 { 00053 mtx.lock(); 00054 if (T1 < T2) 00055 { 00056 lcd.printf("Player 1 Wins!!\n"); 00057 } 00058 if (T1 > T2) 00059 { 00060 lcd.printf("Player 2 Wins!\n"); 00061 } 00062 if (T1 == T2) 00063 { 00064 lcd.printf("Game was a Draw, Try Again\n"); 00065 } 00066 mtx.unlock(); 00067 } 00068 00069 void thread3(void const *args) 00070 { 00071 while(true) // thread loop 00072 { 00073 RGBLED_r = 0.7 + (rand() % 11)/20.0; 00074 RGBLED_g = 0.7 + (rand() % 11)/20.0; 00075 RGBLED_b = 0.7 + (rand() % 11)/20.0; 00076 Thread::wait(1667); // wait 1.5s 00077 } 00078 } 00079 00080 int main() 00081 { 00082 while(1) 00083 { 00084 Thread t3(thread3); //RGB LED 00085 lcd.printf("Destroy the Bit - \n\r"); 00086 lcd.printf("Player 1 Start\n\r"); 00087 lcd.printf("Press Joystick to Start\n\r"); 00088 unsigned int value = 0x12; 00089 spkr.period(1.0/2000.0); 00090 while (StartGame == 0) 00091 { 00092 if (pb) 00093 { 00094 StartGame = 1; 00095 player2 = 1; 00096 player1 = 0; 00097 t1.start(); 00098 t2.start(); 00099 } 00100 } 00101 while(StartGame == 1) 00102 { 00103 if (player1 == 0) 00104 { 00105 lcd.cls(); 00106 lcd.printf("Press Joystick to kill bit\n\r"); 00107 value = value ^ pb; 00108 if (value == 0) { 00109 for (int i=0; i<5; ++i) { 00110 spkr = 0.5; 00111 lcd.cls(); 00112 display(0x0F); 00113 wait(.5); 00114 display(0); 00115 spkr = 0.0; 00116 wait(.25); 00117 } 00118 t1.stop(); 00119 value = 0x012; 00120 lcd_mutex.lock(); 00121 lcd.printf("Player 1 Finished!!!!!!!\n\r"); 00122 lcd.printf("Finish time - %f secs\n\r", t1.read()); 00123 wait(2); 00124 lcd_mutex.unlock(); 00125 Player1Time = t1.read(); 00126 player1 = 1; 00127 player2 = 0; 00128 t1.reset(); 00129 t2.reset(); 00130 } 00131 value = ((value & 0x01)<<3) | value >> 1; 00132 display(value); 00133 measure(value); 00134 wait(.25); 00135 } 00136 if (player2 == 0) 00137 { 00138 if (player2Ready == 0) 00139 { 00140 lcd.cls(); 00141 lcd.printf("Player 2 Start\n\r"); 00142 lcd.printf("Press JoyStick to Start\n\r"); 00143 } 00144 while(player2Ready == 0) 00145 { 00146 if (pb) 00147 { 00148 t2.start(); 00149 player2Ready = 1; 00150 } 00151 } 00152 lcd.cls(); 00153 lcd.printf("Press Joystick to kill bit\n\r"); 00154 value = value ^ pb; 00155 if (value == 0) { 00156 for (int i=0; i<5; ++i) { 00157 spkr = 0.5; 00158 display(0x0F); 00159 wait(.5); 00160 display(0); 00161 spkr = 0.0; 00162 wait(.25); 00163 } 00164 t2.stop(); 00165 value = 0x012; 00166 lcd_mutex.lock(); 00167 lcd.printf("Player 2 Finished!!!!!!!\n\r"); 00168 lcd.printf("Finish time - %f secs\n\r", t2.read()); 00169 wait(2); 00170 lcd_mutex.unlock(); 00171 Player2Time = t2.read(); 00172 player2 = 1; 00173 StartGame = 0; 00174 GameFinished = 1; 00175 t2.reset(); 00176 } 00177 value = ((value & 0x01)<<3) | value >> 1; 00178 display(value); 00179 measure(value); 00180 wait(.25); 00181 } 00182 } 00183 if (GameFinished == 1) 00184 { 00185 lcd.cls(); 00186 wait(1); 00187 lcd.printf("Game has Ended\n"); 00188 calculate(Player1Time, Player2Time); 00189 wait(1); 00190 lcd.cls(); 00191 lcd.printf("Game will reset in 5 secs\n"); 00192 wait(5); 00193 GameFinished = 0; 00194 Player1Time, Player2Time = 0; 00195 lcd.cls(); 00196 } 00197 } 00198 }
Generated on Sat Sep 17 2022 18:51:04 by
1.7.2