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 4DGL-uLCD-SE PinDetect
main.cpp
00001 #include "uLCD_4DGL.h" 00002 #include "PinDetect.h" 00003 #include "mbed.h" 00004 #include <ctime> // For time() 00005 #include <cstdlib> // For srand() and rand() 00006 #include "Speaker.h" 00007 uLCD_4DGL uLCD(p9, p10, p11); 00008 00009 PinDetect pbLeft(p21); //left 00010 PinDetect pbRight(p22); //right 00011 PinDetect pbExit(p23); //Exit 00012 Speaker mySpeaker(p24); 00013 00014 int lemur_game(); 00015 void print_result_screen(bool left, int delay); 00016 void print_exit_screen(); 00017 int check_value_in_random_array(int &val); 00018 void clear_random_array(); 00019 void generate_shape(int box, int shape); 00020 void start_up_hello(); 00021 void clear_screen(); 00022 void print_two_white_rect(); 00023 void print_random_circle(int x1, int y1, int x2, int y2); 00024 void print_random_triangle(int x1, int y1, int x2, int y2); 00025 void print_random_square(int x1, int y1, int x2, int y2); 00026 void pick_spot(int box, int index, int* result); 00027 00028 int arr[4]; // store x1, y1, x2, y2 00029 int rand0 = 0; // number of random objects in left box 00030 int rand1 = 0; // the number of random objects in right box 00031 int random_check_array[15] = {}; 00032 int random_check_value = 0; 00033 int trial_number = 1; 00034 int correct_trial_number = 0; 00035 00036 00037 int main() { 00038 start_up_hello(); 00039 pbLeft.mode(PullUp); 00040 pbRight.mode(PullUp); 00041 pbExit.mode(PullUp); 00042 wait_ms(1000); 00043 print_two_white_rect(); // init the 2 rectangular box 00044 srand(time(0)); 00045 lemur_game(); // start trial! 00046 } 00047 00048 int lemur_game() { 00049 while(1) { 00050 Timer t = Timer(); 00051 int shape = rand()%3; 00052 generate_shape(0, shape); 00053 generate_shape(1, shape); 00054 t.start(); 00055 bool done = false; 00056 int left = 0; 00057 while(!done) { 00058 if(!pbLeft) { 00059 t.stop(); 00060 done = true; 00061 left = true; 00062 wait_ms(1000); 00063 } 00064 if(!pbRight) { 00065 t.stop(); 00066 done = true; 00067 left = false; 00068 wait_ms(1000); 00069 } 00070 if(!pbExit) { 00071 print_exit_screen(); 00072 return 1; 00073 } 00074 } 00075 00076 print_result_screen(left, t.read_ms()); 00077 t.reset(); 00078 clear_screen(); 00079 } 00080 } 00081 00082 void print_result_screen(bool left, int delay) { // print result screen after each trial 00083 if((left && rand0 < rand1) || (!left && rand1 < rand0)) { // check if the button hit was right or not 00084 correct_trial_number++; // if its right, execute the correct code 00085 uLCD.cls(); 00086 uLCD.textbackground_color(0x000000); 00087 uLCD.color(GREEN); 00088 uLCD.text_bold(TEXTBOLD); 00089 uLCD.text_height(1.5); 00090 uLCD.text_width(1.5); 00091 uLCD.locate(6, 4); 00092 uLCD.printf("Trial %d\n", trial_number); 00093 uLCD.locate(2, 6); 00094 uLCD.printf("You are correct!\n"); 00095 uLCD.locate(4,8); 00096 uLCD.printf("time: %dms\n", delay); 00097 uLCD.locate(1, 10); 00098 uLCD.printf("fraction correct:\n"); 00099 uLCD.locate(6, 12); 00100 uLCD.printf("%.2f%%\n", (((float)correct_trial_number)/trial_number)*100.0); 00101 wait_ms(2000); //wait 2s 00102 uLCD.cls(); 00103 } else { 00104 uLCD.cls(); // execute the incorrect code 00105 uLCD.textbackground_color(0x000000); 00106 uLCD.color(GREEN); 00107 uLCD.text_bold(TEXTBOLD); 00108 uLCD.text_height(1.5); 00109 uLCD.text_width(1.5); 00110 uLCD.locate(6, 4); 00111 uLCD.printf("Trial %d\n", trial_number); 00112 uLCD.locate(1, 6); 00113 uLCD.printf("You are incorrect\n"); 00114 uLCD.locate(4,8); 00115 uLCD.printf("time: %dms\n", delay); 00116 uLCD.locate(1, 10); 00117 uLCD.printf("fraction correct:\n"); 00118 uLCD.locate(6, 12); 00119 uLCD.printf("%.2f%%\n", (((float)correct_trial_number)/trial_number)*100.0); 00120 wait_ms(2000); //wait 2s 00121 uLCD.cls(); 00122 } 00123 trial_number++; // increment trial_number 00124 } 00125 void print_exit_screen() { // print exit screen when exit button is hit 00126 uLCD.cls(); 00127 uLCD.textbackground_color(0x000000); 00128 uLCD.color(GREEN); 00129 uLCD.text_bold(TEXTBOLD); 00130 uLCD.text_height(1.5); 00131 uLCD.text_width(1.5); 00132 uLCD.locate(4, 4); 00133 uLCD.printf("Trial Done!\n"); 00134 uLCD.locate(2, 7); 00135 uLCD.printf("You've been an\n"); 00136 uLCD.locate(3, 10); 00137 uLCD.printf("Awesome lemur\n"); 00138 wait_ms(2000); //wait 2s 00139 uLCD.cls(); 00140 } 00141 00142 int check_value_in_random_array(int &val) { // return -1 if not found. This takes in a value and we compare it with every value in our random_check_array 00143 for(int i = 0; i < sizeof(random_check_array)/sizeof(int); i++) { // if our current random_check_array does not contain any value equals to val, return -1 00144 if(random_check_array[i] == val) { // else return 1 00145 return 1; 00146 } 00147 } 00148 return -1; 00149 } 00150 void clear_random_array() { // reset random array and random_check_value 00151 for(int i = 0; i < sizeof(random_check_array)/sizeof(int); i++) { 00152 random_check_array[i] = -1; 00153 } 00154 random_check_value = 0; 00155 } 00156 void generate_shape(int box, int shape) { // This method will generate the shapes in a specific box, box = 0 -> left box, box = 1 -> right box. shape = 0 -> circle 00157 int pos; // shape = 1 -> triangle, shape = 2 -> square 00158 if(box == 0) { // left box 00159 do { 00160 rand0 = rand()%15 + 1; 00161 } while(rand0 == rand1); // randomly get the number of shape for this box and make sure it is not the same number as the other box 00162 for(int i = 0; i < rand0;i++) { // loop from 0 to that number-1 to generate shapes 00163 pos = rand()%18; // pos will check to make sure all shapes are uniquely placed and no shape is overwritten by any other shape 00164 while(check_value_in_random_array(pos) == 1) { 00165 pos = rand()%18; 00166 } 00167 00168 random_check_array[random_check_value] = pos; // push pos onto the random_check_value array to keep checking 00169 random_check_value++; 00170 pick_spot(box, pos, arr); // call pick_spot to assign values to arr element. arr[0] stores x1, arr[1] stores y1, arr[2] stores x2, arr[3] stores y3 00171 if(shape == 0) { // generate the shape based on our random input 00172 print_random_circle(arr[0], arr[1], arr[2], arr[3]); 00173 } else if(shape == 1) { 00174 print_random_triangle(arr[0], arr[1], arr[2], arr[3]); 00175 } else if(shape == 2) { 00176 print_random_square(arr[0], arr[1], arr[2], arr[3]); 00177 } 00178 } 00179 clear_random_array(); // clear to reset random_check_array 00180 } else if(box == 1) { // do the same thing but with box 1 00181 do { 00182 rand1 = rand()%15 + 1; 00183 } while(rand0 == rand1); 00184 for(int i = 0; i < rand1;i++) { 00185 pos = rand()%18; 00186 while(check_value_in_random_array(pos) == 1) { 00187 pos = rand()%18; 00188 } 00189 random_check_array[random_check_value] = pos; 00190 random_check_value++; 00191 pick_spot(box, pos, arr); 00192 if(shape == 0) { 00193 print_random_circle(arr[0], arr[1], arr[2], arr[3]); 00194 } else if(shape == 1) { 00195 print_random_triangle(arr[0], arr[1], arr[2], arr[3]); 00196 } else if(shape == 2) { 00197 print_random_square(arr[0], arr[1], arr[2], arr[3]); 00198 } 00199 } 00200 clear_random_array(); 00201 } 00202 } 00203 void start_up_hello() { // 1.1, print the startup screen and play some music 00204 uLCD.textbackground_color(0x000000); 00205 uLCD.color(GREEN); 00206 uLCD.text_bold(TEXTBOLD); 00207 uLCD.text_height(1.5); 00208 uLCD.text_width(1.5); 00209 uLCD.locate(4, 4); 00210 uLCD.printf("Welcome to\n"); 00211 uLCD.locate(2, 7); 00212 uLCD.printf("Peter's Awesome\n"); 00213 uLCD.locate(3, 10); 00214 uLCD.printf("Lemur Party\n"); 00215 wait_ms(5000); 00216 mySpeaker.PlayNote(400, 0.5, 0.5); 00217 mySpeaker.PlayNote(400, 0.5, 0.5); 00218 mySpeaker.PlayNote(450, 0.8, 0.5); 00219 mySpeaker.PlayNote(400, 0.8, 0.5); 00220 mySpeaker.PlayNote(540, 0.8, 0.5); 00221 mySpeaker.PlayNote(500, 0.8, 0.5); 00222 00223 wait_ms(2000); //wait 2s 00224 uLCD.cls(); 00225 } 00226 void clear_screen() { // clear the trial screen, leaving two empty white boxes 00227 uLCD.cls(); 00228 print_two_white_rect(); 00229 wait_ms(200); 00230 } 00231 void print_two_white_rect() { // print the empty white boxes 00232 uLCD.filled_rectangle(0, 0, 62, 1, 0xFFFFFF); // I was a dumbass here I should've called rectangle 2 times instead of doing a bunch of small filled_rectangle lmao 00233 uLCD.filled_rectangle(64,0, 126, 1, 0xFFFFFF); // I'm too lazy to fix this to make it cleaner so bear with me! :D 00234 00235 uLCD.filled_rectangle(0, 0, 1, 122, 0xFFFFFF); 00236 uLCD.filled_rectangle(61, 0, 62, 122, 0xFFFFFF); 00237 uLCD.filled_rectangle(64, 0, 65, 122, 0xFFFFFF); 00238 uLCD.filled_rectangle(125, 0, 126, 122, 0xFFFFFF); 00239 00240 uLCD.filled_rectangle(0, 121, 62, 122, 0xFFFFFF); 00241 uLCD.filled_rectangle(64, 121, 126, 122, 0xFFFFFF); 00242 } 00243 00244 void print_random_circle(int x1, int y1, int x2, int y2) { // print a circle at the specific position given with a random radius 00245 uLCD.filled_circle((x2 + x1)/2.0 , (y2 + y1)/2.0 , (rand() % ((21-1)/2)), RED); 00246 } 00247 00248 void print_random_triangle(int x1, int y1, int x2, int y2) { // print a triangle at the specific position 00249 uLCD.triangle((x2+x1)/2.0, y1, x1, y2, x2, y2, RED); 00250 } 00251 00252 void print_random_square(int x1, int y1, int x2, int y2) { // print a square at a specific position 00253 uLCD.filled_rectangle(x1+2, y1+2, x2-2, y2-2, RED); 00254 } 00255 00256 void pick_spot(int box, int index, int* result) { // box = 0 -> left box, box = 1 -> right box. index goes from 0-17, left to right, up to down representing the square spaces inside a box 00257 if(box == 0) { // we would pass our arr into result to get specific x,y coordinate out 00258 switch(index) { 00259 case 0: 00260 result[0] = 1; 00261 result[1] = 1; 00262 result[2] = 20; 00263 result[3] = 20; 00264 break; 00265 case 1: 00266 result[0] = 21; 00267 result[1] = 1; 00268 result[2] = 40; 00269 result[3] = 20; 00270 break; 00271 case 2: 00272 result[0] = 41; 00273 result[1] = 1; 00274 result[2] = 60; 00275 result[3] = 20; 00276 break; 00277 case 3: 00278 result[0] = 1; 00279 00280 result[1] = 21; 00281 result[2] = 20; 00282 result[3] = 40; 00283 break; 00284 case 4: 00285 result[0] = 21; 00286 result[1] = 21; 00287 result[2] = 40; 00288 result[3] = 40; 00289 break; 00290 case 5: 00291 result[0] = 41; 00292 result[1] = 21; 00293 result[2] = 60; 00294 result[3] = 40; 00295 break; 00296 case 6: 00297 result[0] = 1; 00298 result[1] = 41; 00299 result[2] = 20; 00300 result[3] = 60; 00301 break; 00302 case 7: 00303 result[0] = 21; 00304 result[1] = 41; 00305 result[2] = 40; 00306 result[3] = 60; 00307 break; 00308 case 8: 00309 result[0] = 41; 00310 result[1] = 41; 00311 result[2] = 60; 00312 result[3] = 60; 00313 break; 00314 case 9: 00315 result[0] = 1; 00316 result[1] = 61; 00317 result[2] = 20; 00318 result[3] = 80; 00319 break; 00320 case 10: 00321 result[0] = 21; 00322 result[1] = 61; 00323 result[2] = 40; 00324 result[3] = 80; 00325 break; 00326 case 11: 00327 result[0] = 41; 00328 result[1] = 61; 00329 result[2] = 60; 00330 result[3] = 80; 00331 break; 00332 case 12: 00333 result[0] = 1; 00334 result[1] = 81; 00335 result[2] = 20; 00336 result[3] = 100; 00337 break; 00338 case 13: 00339 result[0] = 21; 00340 result[1] = 81; 00341 result[2] = 40; 00342 result[3] = 100; 00343 break; 00344 case 14: 00345 result[0] = 41; 00346 result[1] = 81; 00347 result[2] = 60; 00348 result[3] = 100; 00349 break; 00350 case 15: 00351 result[0] = 1; 00352 result[1] = 101; 00353 result[2] = 20; 00354 result[3] = 120; 00355 break; 00356 case 16: 00357 result[0] = 21; 00358 result[1] = 101; 00359 result[2] = 40; 00360 result[3] = 120; 00361 break; 00362 case 17: 00363 result[0] = 41; 00364 result[1] = 101; 00365 result[2] = 60; 00366 result[3] = 120; 00367 break; 00368 } 00369 00370 } else if(box == 1) { 00371 switch(index) { 00372 case 0: 00373 result[0] = 65; 00374 result[1] = 1; 00375 result[2] = 84; 00376 result[3] = 20; 00377 break; 00378 case 1: 00379 result[0] = 85; 00380 result[1] = 1; 00381 result[2] = 104; 00382 result[3] = 20; 00383 break; 00384 case 2: 00385 result[0] = 105; 00386 result[1] = 1; 00387 result[2] = 124; 00388 result[3] = 20; 00389 break; 00390 case 3: 00391 result[0] = 65; 00392 result[1] = 21; 00393 result[2] = 84; 00394 result[3] = 40; 00395 break; 00396 case 4: 00397 result[0] = 85; 00398 result[1] = 21; 00399 result[2] = 104; 00400 result[3] = 40; 00401 break; 00402 case 5: 00403 result[0] = 105; 00404 result[1] = 21; 00405 result[2] = 124; 00406 result[3] = 40; 00407 break; 00408 case 6: 00409 result[0] = 65; 00410 result[1] = 41; 00411 result[2] = 84; 00412 result[3] = 60; 00413 break; 00414 case 7: 00415 result[0] = 85; 00416 result[1] = 41; 00417 result[2] = 104; 00418 result[3] = 60; 00419 break; 00420 case 8: 00421 result[0] = 105; 00422 result[1] = 41; 00423 result[2] = 125; 00424 result[3] = 60; 00425 break; 00426 case 9: 00427 result[0] = 65; 00428 result[1] = 61; 00429 result[2] = 84; 00430 result[3] = 80; 00431 break; 00432 case 10: 00433 result[0] = 85; 00434 result[1] = 61; 00435 result[2] = 104; 00436 result[3] = 80; 00437 break; 00438 case 11: 00439 result[0] = 105; 00440 result[1] = 61; 00441 result[2] = 124; 00442 result[3] = 80; 00443 break; 00444 case 12: 00445 result[0] = 65; 00446 result[1] = 81; 00447 result[2] = 84; 00448 result[3] = 100; 00449 break; 00450 case 13: 00451 result[0] = 85; 00452 result[1] = 81; 00453 result[2] = 104; 00454 result[3] = 100; 00455 break; 00456 case 14: 00457 result[0] = 105; 00458 result[1] = 81; 00459 result[2] = 124; 00460 result[3] = 100; 00461 break; 00462 case 15: 00463 result[0] = 65; 00464 result[1] = 101; 00465 result[2] = 84; 00466 result[3] = 120; 00467 break; 00468 case 16: 00469 result[0] = 85; 00470 result[1] = 101; 00471 result[2] = 104; 00472 result[3] = 120; 00473 break; 00474 case 17: 00475 result[0] = 105; 00476 result[1] = 101; 00477 result[2] = 124; 00478 result[3] = 120; 00479 break; 00480 } 00481 } 00482 }
Generated on Wed Mar 29 2023 11:36:32 by
1.7.2