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:
1:1aca092ceb7e
Parent:
0:67efb4657416
Child:
2:8ff96f7acb45
--- a/main.cpp	Wed Aug 17 01:46:59 2016 +0000
+++ b/main.cpp	Wed Aug 17 15:22:18 2016 +0000
@@ -1,52 +1,108 @@
 #include "mbed.h"
 #include "LED_Bar.h"
 
-LED_Bar bar(D6, D5);
-DigitalIn button1(A3);
-DigitalIn button2(A2);
-DigitalIn button3(A1);
-DigitalIn button4(A0);
-int button_index;
+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);
+int input[10];
+int sequence[] = {3, 2, 1, 3, 2, 1};
+
+void simon_loop() {
+    for(int i = 0; i < (sizeof(sequence)/sizeof(sequence[0])); i++) {  
+        if (sequence[i] == 1) {
+            right.setLevel(10);
+            wait(0.4);
+            right.setLevel(0);
+        }
+        else if (sequence[i] == 2) {
+            top.setLevel(10);
+            wait(0.4);
+            top.setLevel(0);
+        }
+        else {
+            left.setLevel(10);
+            wait(0.4);
+            left.setLevel(0);
+        }
+    }
+}
+
+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);
+        }
+    }
+    return;
+}
+
+bool win_or_lose() {
+    for(int k = 0; k < (sizeof(sequence)/sizeof(sequence[0])); k++) { 
+        if (input[k] == sequence[k]) { continue; }
+        else { return false; }
+    }
+    return true;
+}
+
+void success() {
+    for(int h = 0; h <=10; h++){
+        top.setLevel(h);
+        right.setLevel(h);
+        left.setLevel(h);
+        wait(0.1);
+    } 
+    for(int h = 0; h <=10; h++){
+        top.setLevel(h);
+        right.setLevel(h);
+        left.setLevel(h);
+        wait(0.1);
+    }  
+    top.setLevel(0);
+    right.setLevel(0);
+    left.setLevel(0); 
+}
+
+void failure() {
+    for(int g = 0; g <= 10; g++){
+        top.setLevel(10-g);
+        right.setLevel(10-g);
+        left.setLevel(10-g);
+        wait(0.1);
+    }
+}
 
 int main() {
-    bar.setLevel(0);
-    while(1) {
-        wait(0.3);
-        if (button1) { 
-            button_index = 1;
-            while(button1){
-                bar.setSingleLed(9, 1); 
-                bar.setSingleLed(8, 1);
-            }; 
-            bar.setSingleLed(9, 0); 
-            bar.setSingleLed(8, 0);
-        }
-        if (button2) { 
-            button_index = 2;
-            while(button2){
-                bar.setSingleLed(7, 1); 
-                bar.setSingleLed(6, 1);
-            }; 
-            bar.setSingleLed(7, 0); 
-            bar.setSingleLed(6, 0);
-        }
-        if (button3) { 
-            button_index = 3;
-            while(button3){
-                bar.setSingleLed(5, 1); 
-                bar.setSingleLed(4, 1);
-            }; 
-            bar.setSingleLed(5, 0); 
-            bar.setSingleLed(4, 0);
-        }
-        if (button4) { 
-            button_index = 4;
-            while(button4){
-                bar.setSingleLed(3, 1); 
-                bar.setSingleLed(2, 1);
-            }; 
-            bar.setSingleLed(3, 0); 
-            bar.setSingleLed(2, 0);
-        }
-    }
-}
\ No newline at end of file
+    top.setLevel(0);
+    right.setLevel(0);
+    left.setLevel(0);
+    // wait for user to start simon says
+    wait(0.3);
+    simon_loop();
+    input_loop();
+    wait(0.2);
+    bool winner = win_or_lose();
+    if (winner == true) { success(); } 
+    else { failure; }
+}