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

Revision:
4:9950f9ec46cf
Parent:
3:96879db74ec8
Child:
5:b6f37ce2a9a3
--- a/main.cpp	Thu Aug 18 16:47:00 2016 +0000
+++ b/main.cpp	Thu Aug 18 19:43:09 2016 +0000
@@ -4,13 +4,15 @@
 LED_Bar top(D6, D5);
 LED_Bar right(D8, D7);
 LED_Bar left(D4, D3);
-DigitalIn button_left(A2);
-DigitalIn button_top(A1);
-DigitalIn button_right(A0);
+InterruptIn button_left(A2);
+InterruptIn button_top(A1);
+InterruptIn button_right(A0);
 DigitalOut buzzer(D2);
 Ticker input_timer;
 int input[50];
+int input_index;
 int sequence[] = {3, 2, 1, 3, 2, 1};
+int sequence_size;
 int level_index = 0;
 
 void level() {
@@ -26,7 +28,7 @@
 /*  simon says loop: turn on corresponding LED Bars according to the # 
     in the sequence array (1=right, 2=top, 3=left) */
 void simon_loop() { 
-    for(int i = 0; i < (sizeof(sequence)/sizeof(sequence[0])); i++) {  
+    for(int i = 0; i < sequence_size; i++) {  
         if (sequence[i] == 1) {
             right.setLevel(10);
             wait(0.4);
@@ -45,40 +47,50 @@
     }
 }
 
+void right_input() {
+    input[input_index] = 1;
+    while(button_right) {
+        right.setLevel(10);
+    }
+    right.setLevel(0);
+    return;
+}
+
+void top_input() {
+    input[input_index] = 2;
+    while(button_top) {
+        top.setLevel(10);
+    }
+    top.setLevel(0);
+    return;
+}
+
+void left_input() {
+    input[input_index] = 3;
+    while(button_left) {
+        left.setLevel(10);
+    }
+    left.setLevel(0);
+    return;
+}
+
 /*  gather user input based on buttons pressed, then store button # into
     the input array (1=button_right, 2=button_top, 3=button_left) */
 void input_loop() {
-    for(int j = 0; j < (sizeof(sequence)/sizeof(sequence[0])); j++) { 
-        wait(0.4);
-        if (button_top) {
-            input[j] = 2;
-            while(button_top){
-                top.setLevel(10);
-            };
-            top.setLevel(0);
-        }
-        if (button_right) {
-            input[j] = 1;
-            while(button_right){
-                right.setLevel(10);
-            };
-            right.setLevel(0);
-        }
-        if (button_left) {
-            input[j] = 3;
-            while(button_left){
-                left.setLevel(10);
-            };
-            left.setLevel(0);
-        }
+    button_right.rise(&right_input);
+    button_top.rise(&top_input);
+    button_left.rise(&left_input);
+    while(1) {
+        wait(0.2);
+        if (input_index <= sequence_size) { continue; }
+        else { return; }
     }
-    return;
 }
 
 /*  compare input array to simon says sequence array, if they are a match
     return true, if they are not a match return false */
 bool win_or_lose() {
-    for(int k = 0; k < (sizeof(sequence)/sizeof(sequence[0])); k++) { 
+    for(int k = 0; k < sequence_size; k++) { 
         if (input[k] == sequence[k]) { continue; }
         else { return false; }
     }
@@ -115,6 +127,8 @@
 }
 
 void start_game() {
+    input_index = 0;
+    sequence_size = sizeof(sequence)/sizeof(sequence[0]);
     top.setLevel(0);
     right.setLevel(0);
     left.setLevel(0);
@@ -122,6 +136,7 @@
     //level();
     wait(0.3);
     simon_loop();
+    wait(0.3);
     input_loop();
     wait(0.2);
     bool winner = win_or_lose();