A simple Simon Says game using a Grove Base Shield, 3 Grove LED Bars, 1 Grove Buzzer, and 3 Grove Buttons.

Dependencies:   LED_Bar mbed

Components Used

Platform: ST-Nucleo-F401RE

Components / Grove Shield V2
As an expansion board, Base Shield v2 has many Grove connectors, making it convenient for you to use Grove products together
Digital on / off buzzer
Seeed Grove Button module with digital interface
The Grove LED Bar is comprised of a 10 segment LED gauge bar and an MY9221 LED controlling chip.

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

Committer:
jplunkett
Date:
Thu Aug 18 21:34:55 2016 +0000
Revision:
5:b6f37ce2a9a3
Parent:
4:9950f9ec46cf
Child:
6:1c87edbbafe0
added start game interrupts, animations, etc.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jplunkett 0:67efb4657416 1 #include "mbed.h"
jplunkett 0:67efb4657416 2 #include "LED_Bar.h"
jplunkett 5:b6f37ce2a9a3 3 #include "main.h"
jplunkett 0:67efb4657416 4
jplunkett 4:9950f9ec46cf 5 int sequence_size;
jplunkett 5:b6f37ce2a9a3 6 int sequence[100];
jplunkett 5:b6f37ce2a9a3 7 int level_index = 1;
jplunkett 5:b6f37ce2a9a3 8 int level_array;
jplunkett 5:b6f37ce2a9a3 9 int win_count = 0;
jplunkett 5:b6f37ce2a9a3 10 int lose_count = 0;
jplunkett 5:b6f37ce2a9a3 11 bool start_flag = false;
jplunkett 5:b6f37ce2a9a3 12
jplunkett 5:b6f37ce2a9a3 13 void start_level() {
jplunkett 5:b6f37ce2a9a3 14 right.setLevel(10);
jplunkett 5:b6f37ce2a9a3 15 top.setLevel(10);
jplunkett 5:b6f37ce2a9a3 16 left.setLevel(10);
jplunkett 5:b6f37ce2a9a3 17 wait(0.2);
jplunkett 5:b6f37ce2a9a3 18 right.setLevel(0);
jplunkett 5:b6f37ce2a9a3 19 top.setLevel(0);
jplunkett 5:b6f37ce2a9a3 20 left.setLevel(0);
jplunkett 5:b6f37ce2a9a3 21 wait(0.2);
jplunkett 5:b6f37ce2a9a3 22 right.setLevel(10);
jplunkett 5:b6f37ce2a9a3 23 top.setLevel(10);
jplunkett 5:b6f37ce2a9a3 24 left.setLevel(10);
jplunkett 5:b6f37ce2a9a3 25 wait(0.2);
jplunkett 5:b6f37ce2a9a3 26 right.setLevel(0);
jplunkett 5:b6f37ce2a9a3 27 top.setLevel(0);
jplunkett 5:b6f37ce2a9a3 28 left.setLevel(0);
jplunkett 5:b6f37ce2a9a3 29 start_flag = true;
jplunkett 5:b6f37ce2a9a3 30 return;
jplunkett 5:b6f37ce2a9a3 31 }
jplunkett 1:1aca092ceb7e 32
jplunkett 3:96879db74ec8 33 void level() {
jplunkett 5:b6f37ce2a9a3 34 /* generate random simon says sequence:
jplunkett 5:b6f37ce2a9a3 35 level 1 will have a 3 LED blink combination, with each subsequent level
jplunkett 5:b6f37ce2a9a3 36 having 1 more LED blink, so level 2 has a 4 blink combination, etc. */
jplunkett 5:b6f37ce2a9a3 37 for (int i = 0; i < level_array; i++) {
jplunkett 5:b6f37ce2a9a3 38 sequence[i] = rand() % 3 + 1; // choose a random number b/w 1 to 3
jplunkett 2:8ff96f7acb45 39 }
jplunkett 2:8ff96f7acb45 40
jplunkett 5:b6f37ce2a9a3 41 level_index++;
jplunkett 5:b6f37ce2a9a3 42
jplunkett 5:b6f37ce2a9a3 43 /* button interrupts when user wants to begin the game/level */
jplunkett 5:b6f37ce2a9a3 44 button_right.rise(&start_level);
jplunkett 5:b6f37ce2a9a3 45 button_top.rise(&start_level);
jplunkett 5:b6f37ce2a9a3 46 button_left.rise(&start_level);
jplunkett 5:b6f37ce2a9a3 47 while(!start_flag) {
jplunkett 5:b6f37ce2a9a3 48 wait(0.2); // waiting for user to begin game LED animation
jplunkett 5:b6f37ce2a9a3 49 // perfect time to play waiting buzzer music
jplunkett 5:b6f37ce2a9a3 50 }
jplunkett 2:8ff96f7acb45 51 }
jplunkett 2:8ff96f7acb45 52
jplunkett 2:8ff96f7acb45 53 /* simon says loop: turn on corresponding LED Bars according to the #
jplunkett 2:8ff96f7acb45 54 in the sequence array (1=right, 2=top, 3=left) */
jplunkett 2:8ff96f7acb45 55 void simon_loop() {
jplunkett 4:9950f9ec46cf 56 for(int i = 0; i < sequence_size; i++) {
jplunkett 1:1aca092ceb7e 57 if (sequence[i] == 1) {
jplunkett 1:1aca092ceb7e 58 right.setLevel(10);
jplunkett 1:1aca092ceb7e 59 wait(0.4);
jplunkett 1:1aca092ceb7e 60 right.setLevel(0);
jplunkett 5:b6f37ce2a9a3 61 wait(0.4);
jplunkett 5:b6f37ce2a9a3 62 } else if (sequence[i] == 2) {
jplunkett 1:1aca092ceb7e 63 top.setLevel(10);
jplunkett 1:1aca092ceb7e 64 wait(0.4);
jplunkett 1:1aca092ceb7e 65 top.setLevel(0);
jplunkett 5:b6f37ce2a9a3 66 wait(0.4);
jplunkett 5:b6f37ce2a9a3 67 } else {
jplunkett 1:1aca092ceb7e 68 left.setLevel(10);
jplunkett 1:1aca092ceb7e 69 wait(0.4);
jplunkett 1:1aca092ceb7e 70 left.setLevel(0);
jplunkett 5:b6f37ce2a9a3 71 wait(0.4);
jplunkett 1:1aca092ceb7e 72 }
jplunkett 1:1aca092ceb7e 73 }
jplunkett 1:1aca092ceb7e 74 }
jplunkett 1:1aca092ceb7e 75
jplunkett 4:9950f9ec46cf 76 void right_input() {
jplunkett 4:9950f9ec46cf 77 input[input_index] = 1;
jplunkett 5:b6f37ce2a9a3 78 input_index++;
jplunkett 4:9950f9ec46cf 79 while(button_right) {
jplunkett 4:9950f9ec46cf 80 right.setLevel(10);
jplunkett 4:9950f9ec46cf 81 }
jplunkett 4:9950f9ec46cf 82 right.setLevel(0);
jplunkett 4:9950f9ec46cf 83 return;
jplunkett 4:9950f9ec46cf 84 }
jplunkett 4:9950f9ec46cf 85
jplunkett 4:9950f9ec46cf 86 void top_input() {
jplunkett 4:9950f9ec46cf 87 input[input_index] = 2;
jplunkett 5:b6f37ce2a9a3 88 input_index++;
jplunkett 4:9950f9ec46cf 89 while(button_top) {
jplunkett 4:9950f9ec46cf 90 top.setLevel(10);
jplunkett 4:9950f9ec46cf 91 }
jplunkett 4:9950f9ec46cf 92 top.setLevel(0);
jplunkett 4:9950f9ec46cf 93 return;
jplunkett 4:9950f9ec46cf 94 }
jplunkett 4:9950f9ec46cf 95
jplunkett 4:9950f9ec46cf 96 void left_input() {
jplunkett 4:9950f9ec46cf 97 input[input_index] = 3;
jplunkett 5:b6f37ce2a9a3 98 input_index++;
jplunkett 4:9950f9ec46cf 99 while(button_left) {
jplunkett 4:9950f9ec46cf 100 left.setLevel(10);
jplunkett 4:9950f9ec46cf 101 }
jplunkett 4:9950f9ec46cf 102 left.setLevel(0);
jplunkett 4:9950f9ec46cf 103 return;
jplunkett 4:9950f9ec46cf 104 }
jplunkett 4:9950f9ec46cf 105
jplunkett 2:8ff96f7acb45 106 /* gather user input based on buttons pressed, then store button # into
jplunkett 2:8ff96f7acb45 107 the input array (1=button_right, 2=button_top, 3=button_left) */
jplunkett 1:1aca092ceb7e 108 void input_loop() {
jplunkett 5:b6f37ce2a9a3 109 button_right.rise(&right_input); //interrupt when right button is pressed
jplunkett 4:9950f9ec46cf 110 button_top.rise(&top_input);
jplunkett 4:9950f9ec46cf 111 button_left.rise(&left_input);
jplunkett 5:b6f37ce2a9a3 112 while(input_index < sequence_size) {
jplunkett 4:9950f9ec46cf 113 wait(0.2);
jplunkett 1:1aca092ceb7e 114 }
jplunkett 5:b6f37ce2a9a3 115 input_index = 0;
jplunkett 1:1aca092ceb7e 116 }
jplunkett 1:1aca092ceb7e 117
jplunkett 2:8ff96f7acb45 118 /* compare input array to simon says sequence array, if they are a match
jplunkett 2:8ff96f7acb45 119 return true, if they are not a match return false */
jplunkett 1:1aca092ceb7e 120 bool win_or_lose() {
jplunkett 4:9950f9ec46cf 121 for(int k = 0; k < sequence_size; k++) {
jplunkett 1:1aca092ceb7e 122 if (input[k] == sequence[k]) { continue; }
jplunkett 1:1aca092ceb7e 123 else { return false; }
jplunkett 1:1aca092ceb7e 124 }
jplunkett 1:1aca092ceb7e 125 return true;
jplunkett 1:1aca092ceb7e 126 }
jplunkett 1:1aca092ceb7e 127
jplunkett 3:96879db74ec8 128 /* display a happy LED animation for winning (LED levels 0 to 10 fast) */
jplunkett 1:1aca092ceb7e 129 void success() {
jplunkett 1:1aca092ceb7e 130 for(int h = 0; h <=10; h++){
jplunkett 1:1aca092ceb7e 131 top.setLevel(h);
jplunkett 1:1aca092ceb7e 132 right.setLevel(h);
jplunkett 1:1aca092ceb7e 133 left.setLevel(h);
jplunkett 2:8ff96f7acb45 134 wait(0.05);
jplunkett 1:1aca092ceb7e 135 }
jplunkett 1:1aca092ceb7e 136 for(int h = 0; h <=10; h++){
jplunkett 1:1aca092ceb7e 137 top.setLevel(h);
jplunkett 1:1aca092ceb7e 138 right.setLevel(h);
jplunkett 1:1aca092ceb7e 139 left.setLevel(h);
jplunkett 2:8ff96f7acb45 140 wait(0.05);
jplunkett 1:1aca092ceb7e 141 }
jplunkett 1:1aca092ceb7e 142 }
jplunkett 1:1aca092ceb7e 143
jplunkett 2:8ff96f7acb45 144 /* display a sad LED animation for losing (LED levels 10 to 0 slow) */
jplunkett 1:1aca092ceb7e 145 void failure() {
jplunkett 1:1aca092ceb7e 146 for(int g = 0; g <= 10; g++){
jplunkett 1:1aca092ceb7e 147 top.setLevel(10-g);
jplunkett 1:1aca092ceb7e 148 right.setLevel(10-g);
jplunkett 1:1aca092ceb7e 149 left.setLevel(10-g);
jplunkett 1:1aca092ceb7e 150 wait(0.1);
jplunkett 1:1aca092ceb7e 151 }
jplunkett 1:1aca092ceb7e 152 }
jplunkett 0:67efb4657416 153
jplunkett 3:96879db74ec8 154 void start_game() {
jplunkett 4:9950f9ec46cf 155 input_index = 0;
jplunkett 1:1aca092ceb7e 156 top.setLevel(0);
jplunkett 1:1aca092ceb7e 157 right.setLevel(0);
jplunkett 1:1aca092ceb7e 158 left.setLevel(0);
jplunkett 5:b6f37ce2a9a3 159 level_array = ((level_index - 1) + 3);
jplunkett 5:b6f37ce2a9a3 160 sequence_size = level_array;
jplunkett 5:b6f37ce2a9a3 161 level();
jplunkett 1:1aca092ceb7e 162 wait(0.3);
jplunkett 1:1aca092ceb7e 163 simon_loop();
jplunkett 4:9950f9ec46cf 164 wait(0.3);
jplunkett 1:1aca092ceb7e 165 input_loop();
jplunkett 5:b6f37ce2a9a3 166 wait(0.3);
jplunkett 1:1aca092ceb7e 167 bool winner = win_or_lose();
jplunkett 5:b6f37ce2a9a3 168 if (winner == true) { win_count++; success(); }
jplunkett 5:b6f37ce2a9a3 169 else { lose_count++; failure(); }
jplunkett 5:b6f37ce2a9a3 170 if (lose_count > 0) {
jplunkett 5:b6f37ce2a9a3 171 start_flag = false;
jplunkett 5:b6f37ce2a9a3 172 wait(1); // add in end game buzzer music
jplunkett 5:b6f37ce2a9a3 173 win_count = 0;
jplunkett 5:b6f37ce2a9a3 174 lose_count = 0;
jplunkett 5:b6f37ce2a9a3 175 level_index = 1;
jplunkett 5:b6f37ce2a9a3 176 start_game();
jplunkett 5:b6f37ce2a9a3 177 } else { start_game(); }
jplunkett 1:1aca092ceb7e 178 }
jplunkett 3:96879db74ec8 179
jplunkett 3:96879db74ec8 180 int main() {
jplunkett 3:96879db74ec8 181 start_game();
jplunkett 5:b6f37ce2a9a3 182 }