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@2:8ff96f7acb45, 2016-08-17 (annotated)
- Committer:
- jplunkett
- Date:
- Wed Aug 17 15:45:49 2016 +0000
- Revision:
- 2:8ff96f7acb45
- Parent:
- 1:1aca092ceb7e
- Child:
- 3:96879db74ec8
prep for start up and randomization
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 | 1:1aca092ceb7e | 4 | LED_Bar top(D6, D5); |
jplunkett | 1:1aca092ceb7e | 5 | LED_Bar right(D8, D7); |
jplunkett | 1:1aca092ceb7e | 6 | LED_Bar left(D4, D3); |
jplunkett | 1:1aca092ceb7e | 7 | DigitalIn button_left(A2); |
jplunkett | 1:1aca092ceb7e | 8 | DigitalIn button_top(A1); |
jplunkett | 1:1aca092ceb7e | 9 | DigitalIn button_right(A0); |
jplunkett | 2:8ff96f7acb45 | 10 | int input[50]; |
jplunkett | 1:1aca092ceb7e | 11 | int sequence[] = {3, 2, 1, 3, 2, 1}; |
jplunkett | 1:1aca092ceb7e | 12 | |
jplunkett | 2:8ff96f7acb45 | 13 | void start_up() { |
jplunkett | 2:8ff96f7acb45 | 14 | //generate random simon says sequence |
jplunkett | 2:8ff96f7acb45 | 15 | |
jplunkett | 2:8ff96f7acb45 | 16 | //wait for user input |
jplunkett | 2:8ff96f7acb45 | 17 | while(!(button_left or button_right or button_top)){ |
jplunkett | 2:8ff96f7acb45 | 18 | |
jplunkett | 2:8ff96f7acb45 | 19 | } |
jplunkett | 2:8ff96f7acb45 | 20 | |
jplunkett | 2:8ff96f7acb45 | 21 | } |
jplunkett | 2:8ff96f7acb45 | 22 | |
jplunkett | 2:8ff96f7acb45 | 23 | /* simon says loop: turn on corresponding LED Bars according to the # |
jplunkett | 2:8ff96f7acb45 | 24 | in the sequence array (1=right, 2=top, 3=left) */ |
jplunkett | 2:8ff96f7acb45 | 25 | void simon_loop() { |
jplunkett | 1:1aca092ceb7e | 26 | for(int i = 0; i < (sizeof(sequence)/sizeof(sequence[0])); i++) { |
jplunkett | 1:1aca092ceb7e | 27 | if (sequence[i] == 1) { |
jplunkett | 1:1aca092ceb7e | 28 | right.setLevel(10); |
jplunkett | 1:1aca092ceb7e | 29 | wait(0.4); |
jplunkett | 1:1aca092ceb7e | 30 | right.setLevel(0); |
jplunkett | 1:1aca092ceb7e | 31 | } |
jplunkett | 1:1aca092ceb7e | 32 | else if (sequence[i] == 2) { |
jplunkett | 1:1aca092ceb7e | 33 | top.setLevel(10); |
jplunkett | 1:1aca092ceb7e | 34 | wait(0.4); |
jplunkett | 1:1aca092ceb7e | 35 | top.setLevel(0); |
jplunkett | 1:1aca092ceb7e | 36 | } |
jplunkett | 1:1aca092ceb7e | 37 | else { |
jplunkett | 1:1aca092ceb7e | 38 | left.setLevel(10); |
jplunkett | 1:1aca092ceb7e | 39 | wait(0.4); |
jplunkett | 1:1aca092ceb7e | 40 | left.setLevel(0); |
jplunkett | 1:1aca092ceb7e | 41 | } |
jplunkett | 1:1aca092ceb7e | 42 | } |
jplunkett | 1:1aca092ceb7e | 43 | } |
jplunkett | 1:1aca092ceb7e | 44 | |
jplunkett | 2:8ff96f7acb45 | 45 | /* gather user input based on buttons pressed, then store button # into |
jplunkett | 2:8ff96f7acb45 | 46 | the input array (1=button_right, 2=button_top, 3=button_left) */ |
jplunkett | 1:1aca092ceb7e | 47 | void input_loop() { |
jplunkett | 2:8ff96f7acb45 | 48 | |
jplunkett | 1:1aca092ceb7e | 49 | for(int j = 0; j < (sizeof(sequence)/sizeof(sequence[0])); j++) { |
jplunkett | 1:1aca092ceb7e | 50 | wait(0.4); |
jplunkett | 1:1aca092ceb7e | 51 | if (button_top) { |
jplunkett | 1:1aca092ceb7e | 52 | input[j] = 2; |
jplunkett | 1:1aca092ceb7e | 53 | while(button_top){ |
jplunkett | 1:1aca092ceb7e | 54 | top.setLevel(10); |
jplunkett | 1:1aca092ceb7e | 55 | }; |
jplunkett | 1:1aca092ceb7e | 56 | top.setLevel(0); |
jplunkett | 1:1aca092ceb7e | 57 | } |
jplunkett | 1:1aca092ceb7e | 58 | if (button_right) { |
jplunkett | 1:1aca092ceb7e | 59 | input[j] = 1; |
jplunkett | 1:1aca092ceb7e | 60 | while(button_right){ |
jplunkett | 1:1aca092ceb7e | 61 | right.setLevel(10); |
jplunkett | 1:1aca092ceb7e | 62 | }; |
jplunkett | 1:1aca092ceb7e | 63 | right.setLevel(0); |
jplunkett | 1:1aca092ceb7e | 64 | } |
jplunkett | 1:1aca092ceb7e | 65 | if (button_left) { |
jplunkett | 1:1aca092ceb7e | 66 | input[j] = 3; |
jplunkett | 1:1aca092ceb7e | 67 | while(button_left){ |
jplunkett | 1:1aca092ceb7e | 68 | left.setLevel(10); |
jplunkett | 1:1aca092ceb7e | 69 | }; |
jplunkett | 1:1aca092ceb7e | 70 | left.setLevel(0); |
jplunkett | 1:1aca092ceb7e | 71 | } |
jplunkett | 1:1aca092ceb7e | 72 | } |
jplunkett | 1:1aca092ceb7e | 73 | return; |
jplunkett | 1:1aca092ceb7e | 74 | } |
jplunkett | 1:1aca092ceb7e | 75 | |
jplunkett | 2:8ff96f7acb45 | 76 | /* compare input array to simon says sequence array, if they are a match |
jplunkett | 2:8ff96f7acb45 | 77 | return true, if they are not a match return false */ |
jplunkett | 1:1aca092ceb7e | 78 | bool win_or_lose() { |
jplunkett | 1:1aca092ceb7e | 79 | for(int k = 0; k < (sizeof(sequence)/sizeof(sequence[0])); k++) { |
jplunkett | 1:1aca092ceb7e | 80 | if (input[k] == sequence[k]) { continue; } |
jplunkett | 1:1aca092ceb7e | 81 | else { return false; } |
jplunkett | 1:1aca092ceb7e | 82 | } |
jplunkett | 1:1aca092ceb7e | 83 | return true; |
jplunkett | 1:1aca092ceb7e | 84 | } |
jplunkett | 1:1aca092ceb7e | 85 | |
jplunkett | 2:8ff96f7acb45 | 86 | /* display a happy LED animation for winning (LED levels 0 to 10 fast, twice) */ |
jplunkett | 1:1aca092ceb7e | 87 | void success() { |
jplunkett | 1:1aca092ceb7e | 88 | for(int h = 0; h <=10; h++){ |
jplunkett | 1:1aca092ceb7e | 89 | top.setLevel(h); |
jplunkett | 1:1aca092ceb7e | 90 | right.setLevel(h); |
jplunkett | 1:1aca092ceb7e | 91 | left.setLevel(h); |
jplunkett | 2:8ff96f7acb45 | 92 | wait(0.05); |
jplunkett | 1:1aca092ceb7e | 93 | } |
jplunkett | 1:1aca092ceb7e | 94 | for(int h = 0; h <=10; h++){ |
jplunkett | 1:1aca092ceb7e | 95 | top.setLevel(h); |
jplunkett | 1:1aca092ceb7e | 96 | right.setLevel(h); |
jplunkett | 1:1aca092ceb7e | 97 | left.setLevel(h); |
jplunkett | 2:8ff96f7acb45 | 98 | wait(0.05); |
jplunkett | 1:1aca092ceb7e | 99 | } |
jplunkett | 1:1aca092ceb7e | 100 | top.setLevel(0); |
jplunkett | 1:1aca092ceb7e | 101 | right.setLevel(0); |
jplunkett | 1:1aca092ceb7e | 102 | left.setLevel(0); |
jplunkett | 1:1aca092ceb7e | 103 | } |
jplunkett | 1:1aca092ceb7e | 104 | |
jplunkett | 2:8ff96f7acb45 | 105 | /* display a sad LED animation for losing (LED levels 10 to 0 slow) */ |
jplunkett | 1:1aca092ceb7e | 106 | void failure() { |
jplunkett | 1:1aca092ceb7e | 107 | for(int g = 0; g <= 10; g++){ |
jplunkett | 1:1aca092ceb7e | 108 | top.setLevel(10-g); |
jplunkett | 1:1aca092ceb7e | 109 | right.setLevel(10-g); |
jplunkett | 1:1aca092ceb7e | 110 | left.setLevel(10-g); |
jplunkett | 1:1aca092ceb7e | 111 | wait(0.1); |
jplunkett | 1:1aca092ceb7e | 112 | } |
jplunkett | 1:1aca092ceb7e | 113 | } |
jplunkett | 0:67efb4657416 | 114 | |
jplunkett | 0:67efb4657416 | 115 | int main() { |
jplunkett | 1:1aca092ceb7e | 116 | top.setLevel(0); |
jplunkett | 1:1aca092ceb7e | 117 | right.setLevel(0); |
jplunkett | 1:1aca092ceb7e | 118 | left.setLevel(0); |
jplunkett | 2:8ff96f7acb45 | 119 | start_up(); |
jplunkett | 1:1aca092ceb7e | 120 | wait(0.3); |
jplunkett | 1:1aca092ceb7e | 121 | simon_loop(); |
jplunkett | 1:1aca092ceb7e | 122 | input_loop(); |
jplunkett | 1:1aca092ceb7e | 123 | wait(0.2); |
jplunkett | 1:1aca092ceb7e | 124 | bool winner = win_or_lose(); |
jplunkett | 1:1aca092ceb7e | 125 | if (winner == true) { success(); } |
jplunkett | 2:8ff96f7acb45 | 126 | else { failure(); } |
jplunkett | 1:1aca092ceb7e | 127 | } |