A simple Simon Says game using a Grove Base Shield, 3 Grove LED Bars, 1 Grove Buzzer, and 3 Grove Buttons.
Components Used
Platform: ST-Nucleo-F401RE
Component Configuration
LED_Bar mid(D6, D5); //D5 LED_Bar right(D8, D7); //D7 LED_Bar left(D4, D3); //D3 InterruptIn button_left(A2); InterruptIn button_mid(A1); InterruptIn button_right(A0); DigitalOut buzzer(D2);
Gallery
main.cpp@8:7be76afe5597, 2016-08-19 (annotated)
- Committer:
- jplunkett
- Date:
- Fri Aug 19 14:36:31 2016 +0000
- Revision:
- 8:7be76afe5597
- Parent:
- 7:b2576b2b916e
- Child:
- 9:ac887bf9f85d
pretty code/working game
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jplunkett | 0:67efb4657416 | 1 | #include "mbed.h" |
jplunkett | 0:67efb4657416 | 2 | #include "LED_Bar.h" |
jplunkett | 0:67efb4657416 | 3 | |
jplunkett | 6:1c87edbbafe0 | 4 | LED_Bar mid(D6, D5); |
jplunkett | 6:1c87edbbafe0 | 5 | LED_Bar right(D8, D7); |
jplunkett | 6:1c87edbbafe0 | 6 | LED_Bar left(D4, D3); |
jplunkett | 6:1c87edbbafe0 | 7 | InterruptIn button_left(A2); |
jplunkett | 6:1c87edbbafe0 | 8 | InterruptIn button_mid(A1); |
jplunkett | 6:1c87edbbafe0 | 9 | InterruptIn button_right(A0); |
jplunkett | 6:1c87edbbafe0 | 10 | DigitalOut buzzer(D2); |
jplunkett | 6:1c87edbbafe0 | 11 | int input_index; |
jplunkett | 6:1c87edbbafe0 | 12 | int input[100]; |
jplunkett | 5:b6f37ce2a9a3 | 13 | bool start_flag = false; |
jplunkett | 5:b6f37ce2a9a3 | 14 | |
jplunkett | 6:1c87edbbafe0 | 15 | void start_level() { |
jplunkett | 6:1c87edbbafe0 | 16 | for(int i = 0; i < 2; i++) { |
jplunkett | 6:1c87edbbafe0 | 17 | right.setLevel(10); |
jplunkett | 6:1c87edbbafe0 | 18 | mid.setLevel(10); |
jplunkett | 6:1c87edbbafe0 | 19 | left.setLevel(10); |
jplunkett | 6:1c87edbbafe0 | 20 | wait(0.2); |
jplunkett | 6:1c87edbbafe0 | 21 | right.setLevel(0); |
jplunkett | 6:1c87edbbafe0 | 22 | mid.setLevel(0); |
jplunkett | 6:1c87edbbafe0 | 23 | left.setLevel(0); |
jplunkett | 6:1c87edbbafe0 | 24 | wait(0.2); |
jplunkett | 6:1c87edbbafe0 | 25 | } |
jplunkett | 5:b6f37ce2a9a3 | 26 | start_flag = true; |
jplunkett | 5:b6f37ce2a9a3 | 27 | } |
jplunkett | 1:1aca092ceb7e | 28 | |
jplunkett | 6:1c87edbbafe0 | 29 | /* generate random simon says sequence: |
jplunkett | 6:1c87edbbafe0 | 30 | level 1 will have a 3 LED blink combination, with each subsequent level |
jplunkett | 6:1c87edbbafe0 | 31 | having 1 more LED blink, so level 2 has a 4 blink combination, etc. */ |
jplunkett | 8:7be76afe5597 | 32 | int level(int level_index, int sequence_size, int* sequence) { |
jplunkett | 6:1c87edbbafe0 | 33 | for(int i = 0; i < sequence_size; i++) { |
jplunkett | 6:1c87edbbafe0 | 34 | sequence[i] = rand() % 3 + 1; |
jplunkett | 2:8ff96f7acb45 | 35 | } |
jplunkett | 5:b6f37ce2a9a3 | 36 | button_right.rise(&start_level); |
jplunkett | 6:1c87edbbafe0 | 37 | button_mid.rise(&start_level); |
jplunkett | 5:b6f37ce2a9a3 | 38 | button_left.rise(&start_level); |
jplunkett | 5:b6f37ce2a9a3 | 39 | while(!start_flag) { |
jplunkett | 6:1c87edbbafe0 | 40 | wait(0.2); |
jplunkett | 5:b6f37ce2a9a3 | 41 | } |
jplunkett | 6:1c87edbbafe0 | 42 | return level_index++; |
jplunkett | 2:8ff96f7acb45 | 43 | } |
jplunkett | 2:8ff96f7acb45 | 44 | |
jplunkett | 2:8ff96f7acb45 | 45 | /* simon says loop: turn on corresponding LED Bars according to the # |
jplunkett | 6:1c87edbbafe0 | 46 | in the sequence array (1=right, 2=mid, 3=left) */ |
jplunkett | 8:7be76afe5597 | 47 | void simon_loop(int sequence_size, int* sequence) { |
jplunkett | 4:9950f9ec46cf | 48 | for(int i = 0; i < sequence_size; i++) { |
jplunkett | 1:1aca092ceb7e | 49 | if (sequence[i] == 1) { |
jplunkett | 1:1aca092ceb7e | 50 | right.setLevel(10); |
jplunkett | 1:1aca092ceb7e | 51 | wait(0.4); |
jplunkett | 1:1aca092ceb7e | 52 | right.setLevel(0); |
jplunkett | 5:b6f37ce2a9a3 | 53 | wait(0.4); |
jplunkett | 5:b6f37ce2a9a3 | 54 | } else if (sequence[i] == 2) { |
jplunkett | 6:1c87edbbafe0 | 55 | mid.setLevel(10); |
jplunkett | 1:1aca092ceb7e | 56 | wait(0.4); |
jplunkett | 6:1c87edbbafe0 | 57 | mid.setLevel(0); |
jplunkett | 5:b6f37ce2a9a3 | 58 | wait(0.4); |
jplunkett | 5:b6f37ce2a9a3 | 59 | } else { |
jplunkett | 1:1aca092ceb7e | 60 | left.setLevel(10); |
jplunkett | 1:1aca092ceb7e | 61 | wait(0.4); |
jplunkett | 1:1aca092ceb7e | 62 | left.setLevel(0); |
jplunkett | 5:b6f37ce2a9a3 | 63 | wait(0.4); |
jplunkett | 1:1aca092ceb7e | 64 | } |
jplunkett | 1:1aca092ceb7e | 65 | } |
jplunkett | 1:1aca092ceb7e | 66 | } |
jplunkett | 1:1aca092ceb7e | 67 | |
jplunkett | 4:9950f9ec46cf | 68 | void right_input() { |
jplunkett | 4:9950f9ec46cf | 69 | input[input_index] = 1; |
jplunkett | 5:b6f37ce2a9a3 | 70 | input_index++; |
jplunkett | 4:9950f9ec46cf | 71 | while(button_right) { |
jplunkett | 4:9950f9ec46cf | 72 | right.setLevel(10); |
jplunkett | 4:9950f9ec46cf | 73 | } |
jplunkett | 4:9950f9ec46cf | 74 | right.setLevel(0); |
jplunkett | 4:9950f9ec46cf | 75 | } |
jplunkett | 4:9950f9ec46cf | 76 | |
jplunkett | 6:1c87edbbafe0 | 77 | void mid_input() { |
jplunkett | 4:9950f9ec46cf | 78 | input[input_index] = 2; |
jplunkett | 5:b6f37ce2a9a3 | 79 | input_index++; |
jplunkett | 6:1c87edbbafe0 | 80 | while(button_mid) { |
jplunkett | 6:1c87edbbafe0 | 81 | mid.setLevel(10); |
jplunkett | 4:9950f9ec46cf | 82 | } |
jplunkett | 6:1c87edbbafe0 | 83 | mid.setLevel(0); |
jplunkett | 4:9950f9ec46cf | 84 | } |
jplunkett | 4:9950f9ec46cf | 85 | |
jplunkett | 4:9950f9ec46cf | 86 | void left_input() { |
jplunkett | 4:9950f9ec46cf | 87 | input[input_index] = 3; |
jplunkett | 5:b6f37ce2a9a3 | 88 | input_index++; |
jplunkett | 4:9950f9ec46cf | 89 | while(button_left) { |
jplunkett | 4:9950f9ec46cf | 90 | left.setLevel(10); |
jplunkett | 4:9950f9ec46cf | 91 | } |
jplunkett | 4:9950f9ec46cf | 92 | left.setLevel(0); |
jplunkett | 4:9950f9ec46cf | 93 | } |
jplunkett | 4:9950f9ec46cf | 94 | |
jplunkett | 6:1c87edbbafe0 | 95 | /* gather user input based on buttons pressed, then store button index into |
jplunkett | 6:1c87edbbafe0 | 96 | the input array (1=button_right, 2=button_mid, 3=button_left) */ |
jplunkett | 7:b2576b2b916e | 97 | void input_loop(int sequence_size) { |
jplunkett | 6:1c87edbbafe0 | 98 | button_right.rise(&right_input); |
jplunkett | 6:1c87edbbafe0 | 99 | button_mid.rise(&mid_input); |
jplunkett | 4:9950f9ec46cf | 100 | button_left.rise(&left_input); |
jplunkett | 5:b6f37ce2a9a3 | 101 | while(input_index < sequence_size) { |
jplunkett | 4:9950f9ec46cf | 102 | wait(0.2); |
jplunkett | 1:1aca092ceb7e | 103 | } |
jplunkett | 5:b6f37ce2a9a3 | 104 | input_index = 0; |
jplunkett | 1:1aca092ceb7e | 105 | } |
jplunkett | 1:1aca092ceb7e | 106 | |
jplunkett | 2:8ff96f7acb45 | 107 | /* compare input array to simon says sequence array, if they are a match |
jplunkett | 2:8ff96f7acb45 | 108 | return true, if they are not a match return false */ |
jplunkett | 8:7be76afe5597 | 109 | bool win_or_lose(int sequence_size, int* sequence) { |
jplunkett | 4:9950f9ec46cf | 110 | for(int k = 0; k < sequence_size; k++) { |
jplunkett | 1:1aca092ceb7e | 111 | if (input[k] == sequence[k]) { continue; } |
jplunkett | 1:1aca092ceb7e | 112 | else { return false; } |
jplunkett | 1:1aca092ceb7e | 113 | } |
jplunkett | 1:1aca092ceb7e | 114 | return true; |
jplunkett | 1:1aca092ceb7e | 115 | } |
jplunkett | 1:1aca092ceb7e | 116 | |
jplunkett | 3:96879db74ec8 | 117 | /* display a happy LED animation for winning (LED levels 0 to 10 fast) */ |
jplunkett | 1:1aca092ceb7e | 118 | void success() { |
jplunkett | 6:1c87edbbafe0 | 119 | for(int i = 0; i < 2; i ++){ |
jplunkett | 6:1c87edbbafe0 | 120 | for(int h = 0; h <=10; h++){ |
jplunkett | 6:1c87edbbafe0 | 121 | mid.setLevel(h); |
jplunkett | 6:1c87edbbafe0 | 122 | right.setLevel(h); |
jplunkett | 6:1c87edbbafe0 | 123 | left.setLevel(h); |
jplunkett | 6:1c87edbbafe0 | 124 | wait(0.05); |
jplunkett | 6:1c87edbbafe0 | 125 | } |
jplunkett | 6:1c87edbbafe0 | 126 | } |
jplunkett | 1:1aca092ceb7e | 127 | } |
jplunkett | 1:1aca092ceb7e | 128 | |
jplunkett | 2:8ff96f7acb45 | 129 | /* display a sad LED animation for losing (LED levels 10 to 0 slow) */ |
jplunkett | 1:1aca092ceb7e | 130 | void failure() { |
jplunkett | 1:1aca092ceb7e | 131 | for(int g = 0; g <= 10; g++){ |
jplunkett | 6:1c87edbbafe0 | 132 | mid.setLevel(10-g); |
jplunkett | 1:1aca092ceb7e | 133 | right.setLevel(10-g); |
jplunkett | 1:1aca092ceb7e | 134 | left.setLevel(10-g); |
jplunkett | 1:1aca092ceb7e | 135 | wait(0.1); |
jplunkett | 1:1aca092ceb7e | 136 | } |
jplunkett | 1:1aca092ceb7e | 137 | } |
jplunkett | 0:67efb4657416 | 138 | |
jplunkett | 3:96879db74ec8 | 139 | int main() { |
jplunkett | 6:1c87edbbafe0 | 140 | int win_count = 0; |
jplunkett | 6:1c87edbbafe0 | 141 | int lose_count = 0; |
jplunkett | 6:1c87edbbafe0 | 142 | int level_index = 1; |
jplunkett | 7:b2576b2b916e | 143 | int sequence_size = 2; |
jplunkett | 8:7be76afe5597 | 144 | int sequence[100]; |
jplunkett | 6:1c87edbbafe0 | 145 | while(1) { |
jplunkett | 6:1c87edbbafe0 | 146 | input_index = 0; |
jplunkett | 6:1c87edbbafe0 | 147 | mid.setLevel(0); |
jplunkett | 6:1c87edbbafe0 | 148 | right.setLevel(0); |
jplunkett | 6:1c87edbbafe0 | 149 | left.setLevel(0); |
jplunkett | 7:b2576b2b916e | 150 | sequence_size++; |
jplunkett | 8:7be76afe5597 | 151 | level_index = level(level_index, sequence_size, sequence); |
jplunkett | 8:7be76afe5597 | 152 | simon_loop(sequence_size, sequence); |
jplunkett | 7:b2576b2b916e | 153 | input_loop(sequence_size); |
jplunkett | 6:1c87edbbafe0 | 154 | wait(0.3); |
jplunkett | 8:7be76afe5597 | 155 | bool winner = win_or_lose(sequence_size, sequence); |
jplunkett | 6:1c87edbbafe0 | 156 | if (winner == true) { |
jplunkett | 6:1c87edbbafe0 | 157 | win_count++; |
jplunkett | 6:1c87edbbafe0 | 158 | success(); |
jplunkett | 6:1c87edbbafe0 | 159 | } else { |
jplunkett | 6:1c87edbbafe0 | 160 | lose_count++; |
jplunkett | 6:1c87edbbafe0 | 161 | failure(); |
jplunkett | 6:1c87edbbafe0 | 162 | } |
jplunkett | 6:1c87edbbafe0 | 163 | if (lose_count > 0) { |
jplunkett | 6:1c87edbbafe0 | 164 | start_flag = false; |
jplunkett | 6:1c87edbbafe0 | 165 | win_count = 0; |
jplunkett | 6:1c87edbbafe0 | 166 | lose_count = 0; |
jplunkett | 7:b2576b2b916e | 167 | level_index = 1; |
jplunkett | 7:b2576b2b916e | 168 | sequence_size = 2; |
jplunkett | 6:1c87edbbafe0 | 169 | } |
jplunkett | 6:1c87edbbafe0 | 170 | } |
jplunkett | 5:b6f37ce2a9a3 | 171 | } |