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:
Fri Aug 19 14:23:57 2016 +0000
Revision:
6:1c87edbbafe0
Parent:
5:b6f37ce2a9a3
Child:
7:b2576b2b916e
prettified

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 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 4:9950f9ec46cf 13 int sequence_size;
jplunkett 5:b6f37ce2a9a3 14 int sequence[100];
jplunkett 5:b6f37ce2a9a3 15 bool start_flag = false;
jplunkett 5:b6f37ce2a9a3 16
jplunkett 6:1c87edbbafe0 17 void start_level() {
jplunkett 6:1c87edbbafe0 18 for(int i = 0; i < 2; i++) {
jplunkett 6:1c87edbbafe0 19 right.setLevel(10);
jplunkett 6:1c87edbbafe0 20 mid.setLevel(10);
jplunkett 6:1c87edbbafe0 21 left.setLevel(10);
jplunkett 6:1c87edbbafe0 22 wait(0.2);
jplunkett 6:1c87edbbafe0 23 right.setLevel(0);
jplunkett 6:1c87edbbafe0 24 mid.setLevel(0);
jplunkett 6:1c87edbbafe0 25 left.setLevel(0);
jplunkett 6:1c87edbbafe0 26 wait(0.2);
jplunkett 6:1c87edbbafe0 27 }
jplunkett 5:b6f37ce2a9a3 28 start_flag = true;
jplunkett 5:b6f37ce2a9a3 29 }
jplunkett 1:1aca092ceb7e 30
jplunkett 6:1c87edbbafe0 31 /* generate random simon says sequence:
jplunkett 6:1c87edbbafe0 32 level 1 will have a 3 LED blink combination, with each subsequent level
jplunkett 6:1c87edbbafe0 33 having 1 more LED blink, so level 2 has a 4 blink combination, etc. */
jplunkett 6:1c87edbbafe0 34 int level(int level_index, int sequence_size) {
jplunkett 6:1c87edbbafe0 35 for(int i = 0; i < sequence_size; i++) {
jplunkett 6:1c87edbbafe0 36 sequence[i] = rand() % 3 + 1;
jplunkett 2:8ff96f7acb45 37 }
jplunkett 5:b6f37ce2a9a3 38 button_right.rise(&start_level);
jplunkett 6:1c87edbbafe0 39 button_mid.rise(&start_level);
jplunkett 5:b6f37ce2a9a3 40 button_left.rise(&start_level);
jplunkett 5:b6f37ce2a9a3 41 while(!start_flag) {
jplunkett 6:1c87edbbafe0 42 wait(0.2);
jplunkett 5:b6f37ce2a9a3 43 }
jplunkett 6:1c87edbbafe0 44 return level_index++;
jplunkett 2:8ff96f7acb45 45 }
jplunkett 2:8ff96f7acb45 46
jplunkett 2:8ff96f7acb45 47 /* simon says loop: turn on corresponding LED Bars according to the #
jplunkett 6:1c87edbbafe0 48 in the sequence array (1=right, 2=mid, 3=left) */
jplunkett 2:8ff96f7acb45 49 void simon_loop() {
jplunkett 4:9950f9ec46cf 50 for(int i = 0; i < sequence_size; i++) {
jplunkett 1:1aca092ceb7e 51 if (sequence[i] == 1) {
jplunkett 1:1aca092ceb7e 52 right.setLevel(10);
jplunkett 1:1aca092ceb7e 53 wait(0.4);
jplunkett 1:1aca092ceb7e 54 right.setLevel(0);
jplunkett 5:b6f37ce2a9a3 55 wait(0.4);
jplunkett 5:b6f37ce2a9a3 56 } else if (sequence[i] == 2) {
jplunkett 6:1c87edbbafe0 57 mid.setLevel(10);
jplunkett 1:1aca092ceb7e 58 wait(0.4);
jplunkett 6:1c87edbbafe0 59 mid.setLevel(0);
jplunkett 5:b6f37ce2a9a3 60 wait(0.4);
jplunkett 5:b6f37ce2a9a3 61 } else {
jplunkett 1:1aca092ceb7e 62 left.setLevel(10);
jplunkett 1:1aca092ceb7e 63 wait(0.4);
jplunkett 1:1aca092ceb7e 64 left.setLevel(0);
jplunkett 5:b6f37ce2a9a3 65 wait(0.4);
jplunkett 1:1aca092ceb7e 66 }
jplunkett 1:1aca092ceb7e 67 }
jplunkett 1:1aca092ceb7e 68 }
jplunkett 1:1aca092ceb7e 69
jplunkett 4:9950f9ec46cf 70 void right_input() {
jplunkett 4:9950f9ec46cf 71 input[input_index] = 1;
jplunkett 5:b6f37ce2a9a3 72 input_index++;
jplunkett 4:9950f9ec46cf 73 while(button_right) {
jplunkett 4:9950f9ec46cf 74 right.setLevel(10);
jplunkett 4:9950f9ec46cf 75 }
jplunkett 4:9950f9ec46cf 76 right.setLevel(0);
jplunkett 4:9950f9ec46cf 77 }
jplunkett 4:9950f9ec46cf 78
jplunkett 6:1c87edbbafe0 79 void mid_input() {
jplunkett 4:9950f9ec46cf 80 input[input_index] = 2;
jplunkett 5:b6f37ce2a9a3 81 input_index++;
jplunkett 6:1c87edbbafe0 82 while(button_mid) {
jplunkett 6:1c87edbbafe0 83 mid.setLevel(10);
jplunkett 4:9950f9ec46cf 84 }
jplunkett 6:1c87edbbafe0 85 mid.setLevel(0);
jplunkett 4:9950f9ec46cf 86 }
jplunkett 4:9950f9ec46cf 87
jplunkett 4:9950f9ec46cf 88 void left_input() {
jplunkett 4:9950f9ec46cf 89 input[input_index] = 3;
jplunkett 5:b6f37ce2a9a3 90 input_index++;
jplunkett 4:9950f9ec46cf 91 while(button_left) {
jplunkett 4:9950f9ec46cf 92 left.setLevel(10);
jplunkett 4:9950f9ec46cf 93 }
jplunkett 4:9950f9ec46cf 94 left.setLevel(0);
jplunkett 4:9950f9ec46cf 95 }
jplunkett 4:9950f9ec46cf 96
jplunkett 6:1c87edbbafe0 97 /* gather user input based on buttons pressed, then store button index into
jplunkett 6:1c87edbbafe0 98 the input array (1=button_right, 2=button_mid, 3=button_left) */
jplunkett 1:1aca092ceb7e 99 void input_loop() {
jplunkett 6:1c87edbbafe0 100 button_right.rise(&right_input);
jplunkett 6:1c87edbbafe0 101 button_mid.rise(&mid_input);
jplunkett 4:9950f9ec46cf 102 button_left.rise(&left_input);
jplunkett 5:b6f37ce2a9a3 103 while(input_index < sequence_size) {
jplunkett 4:9950f9ec46cf 104 wait(0.2);
jplunkett 1:1aca092ceb7e 105 }
jplunkett 5:b6f37ce2a9a3 106 input_index = 0;
jplunkett 1:1aca092ceb7e 107 }
jplunkett 1:1aca092ceb7e 108
jplunkett 2:8ff96f7acb45 109 /* compare input array to simon says sequence array, if they are a match
jplunkett 2:8ff96f7acb45 110 return true, if they are not a match return false */
jplunkett 1:1aca092ceb7e 111 bool win_or_lose() {
jplunkett 4:9950f9ec46cf 112 for(int k = 0; k < sequence_size; k++) {
jplunkett 1:1aca092ceb7e 113 if (input[k] == sequence[k]) { continue; }
jplunkett 1:1aca092ceb7e 114 else { return false; }
jplunkett 1:1aca092ceb7e 115 }
jplunkett 1:1aca092ceb7e 116 return true;
jplunkett 1:1aca092ceb7e 117 }
jplunkett 1:1aca092ceb7e 118
jplunkett 3:96879db74ec8 119 /* display a happy LED animation for winning (LED levels 0 to 10 fast) */
jplunkett 1:1aca092ceb7e 120 void success() {
jplunkett 6:1c87edbbafe0 121 for(int i = 0; i < 2; i ++){
jplunkett 6:1c87edbbafe0 122 for(int h = 0; h <=10; h++){
jplunkett 6:1c87edbbafe0 123 mid.setLevel(h);
jplunkett 6:1c87edbbafe0 124 right.setLevel(h);
jplunkett 6:1c87edbbafe0 125 left.setLevel(h);
jplunkett 6:1c87edbbafe0 126 wait(0.05);
jplunkett 6:1c87edbbafe0 127 }
jplunkett 6:1c87edbbafe0 128 }
jplunkett 1:1aca092ceb7e 129 }
jplunkett 1:1aca092ceb7e 130
jplunkett 2:8ff96f7acb45 131 /* display a sad LED animation for losing (LED levels 10 to 0 slow) */
jplunkett 1:1aca092ceb7e 132 void failure() {
jplunkett 1:1aca092ceb7e 133 for(int g = 0; g <= 10; g++){
jplunkett 6:1c87edbbafe0 134 mid.setLevel(10-g);
jplunkett 1:1aca092ceb7e 135 right.setLevel(10-g);
jplunkett 1:1aca092ceb7e 136 left.setLevel(10-g);
jplunkett 1:1aca092ceb7e 137 wait(0.1);
jplunkett 1:1aca092ceb7e 138 }
jplunkett 1:1aca092ceb7e 139 }
jplunkett 0:67efb4657416 140
jplunkett 3:96879db74ec8 141 int main() {
jplunkett 6:1c87edbbafe0 142 int win_count = 0;
jplunkett 6:1c87edbbafe0 143 int lose_count = 0;
jplunkett 6:1c87edbbafe0 144 int level_index = 1;
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 6:1c87edbbafe0 150 sequence_size = ((level_index - 1) + 3);
jplunkett 6:1c87edbbafe0 151 level_index = level(level_index, sequence_size);
jplunkett 6:1c87edbbafe0 152 simon_loop();
jplunkett 6:1c87edbbafe0 153 input_loop();
jplunkett 6:1c87edbbafe0 154 wait(0.3);
jplunkett 6:1c87edbbafe0 155 bool winner = win_or_lose();
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 wait(1);
jplunkett 6:1c87edbbafe0 166 win_count = 0;
jplunkett 6:1c87edbbafe0 167 lose_count = 0;
jplunkett 6:1c87edbbafe0 168 level_index = 1;;
jplunkett 6:1c87edbbafe0 169 }
jplunkett 6:1c87edbbafe0 170 }
jplunkett 5:b6f37ce2a9a3 171 }