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: PinDetect mbed beep
Simon.cpp
00001 #include "mbed.h" 00002 #include "PinDetect.h" 00003 #include "Simon.h" 00004 #include "Piezo.h" 00005 #include <vector> 00006 #include <cstdlib> 00007 00008 DigitalOut Simon::led[4] = {LED1, LED2, LED3, LED4}; 00009 PinDetect Simon::button[5] = {p12, p13, p14, p15, p16}; 00010 00011 Simon::Simon() : queue(turn) { 00012 piezo.NoteC4(); 00013 wait(0.4); 00014 piezo.NoteB4(); 00015 LEDSBlink(); 00016 run = false; 00017 button[0].attach_asserted(this, &Simon::keyPressedSt); 00018 button[1].attach_asserted(this, &Simon::keyPressedNd); 00019 button[2].attach_asserted(this, &Simon::keyPressedRd); 00020 button[3].attach_asserted(this, &Simon::keyPressedTh); 00021 button[4].attach_asserted(this, &Simon::ReStart); 00022 for(i = 0; i < 5; i++) { 00023 button[i].mode( PullDown ); 00024 button[i].setSampleFrequency(); 00025 } 00026 } 00027 00028 void Simon::gameStart(int first_turn) { 00029 this -> first_turn = first_turn; 00030 turn = first_turn; 00031 while(1) { 00032 while(run == true) { 00033 newQueue(); 00034 showQueue(); 00035 run = userTurn(); 00036 } 00037 } 00038 } 00039 00040 int Simon::testQueue() { 00041 if (queue[i] == test_val) { 00042 piezo.NoteFor(queue[i]); 00043 led[queue[i]] = 1; 00044 wait(0.2); 00045 led[queue[i]] = 0; 00046 return 1; 00047 } 00048 else return 0; 00049 } 00050 00051 void Simon::LEDSBlink() { 00052 for(i = 0; i < 4; i++) led[i] = 1; 00053 wait(0.4); 00054 for(i = 0; i < 4; i++) led[i] = 0; 00055 } 00056 00057 void Simon::newQueue() { 00058 for(i = 0; i < turn; i++) queue[i] = rand() % 4; 00059 } 00060 00061 void Simon::showQueue() { 00062 wait(0.2); 00063 for(i = 0; i < turn; i++) { 00064 piezo.NoteFor(queue[i]); 00065 led[queue[i]] = 1; 00066 wait(0.5); 00067 led[queue[i]] = 0; 00068 wait(0.1); 00069 } 00070 } 00071 00072 bool Simon::userTurn() { 00073 i = 0; 00074 button_press = false; 00075 while(i < turn){ 00076 if (button_press == 1) { 00077 if (testQueue() == 0) { 00078 i = turn; 00079 button_press = 0; 00080 LEDSBlink(); 00081 piezo.NoteB4(); 00082 wait(0.3); 00083 piezo.NoteC4(); 00084 return false; 00085 } 00086 else if (testQueue() == 1) { 00087 i++; 00088 button_press = 0; 00089 } 00090 } 00091 } 00092 this -> turn++; 00093 wait(0.5); 00094 return true; 00095 } 00096 00097 void Simon::keyPressedSt() { 00098 button_press = 1; 00099 test_val = 0; 00100 } 00101 00102 void Simon::keyPressedNd() { 00103 button_press = 1; 00104 test_val = 1; 00105 } 00106 00107 void Simon::keyPressedRd() { 00108 button_press = 1; 00109 test_val = 2; 00110 } 00111 00112 void Simon::keyPressedTh() { 00113 button_press = 1; 00114 test_val = 3; 00115 } 00116 00117 void Simon:: ReStart() { 00118 run = true; 00119 turn = first_turn; 00120 }
Generated on Wed Jul 20 2022 14:21:39 by
1.7.2