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:
7:b2576b2b916e
Parent:
6:1c87edbbafe0
Child:
8:7be76afe5597
diff -r 1c87edbbafe0 -r b2576b2b916e main.cpp
--- a/main.cpp	Fri Aug 19 14:23:57 2016 +0000
+++ b/main.cpp	Fri Aug 19 14:30:01 2016 +0000
@@ -10,7 +10,6 @@
 DigitalOut buzzer(D2);
 int input_index;
 int input[100];
-int sequence_size;
 int sequence[100];
 bool start_flag = false;
 
@@ -46,7 +45,7 @@
 
 /*  simon says loop: turn on corresponding LED Bars according to the # 
     in the sequence array (1=right, 2=mid, 3=left) */
-void simon_loop() { 
+void simon_loop(int sequence_size) { 
     for(int i = 0; i < sequence_size; i++) {  
         if (sequence[i] == 1) {
             right.setLevel(10);
@@ -96,7 +95,7 @@
 
 /*  gather user input based on buttons pressed, then store button index into
     the input array (1=button_right, 2=button_mid, 3=button_left) */
-void input_loop() {
+void input_loop(int sequence_size) {
     button_right.rise(&right_input);
     button_mid.rise(&mid_input);
     button_left.rise(&left_input);
@@ -108,7 +107,7 @@
 
 /*  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() {
+bool win_or_lose(int sequence_size) {
     for(int k = 0; k < sequence_size; k++) { 
         if (input[k] == sequence[k]) { continue; }
         else { return false; }
@@ -142,17 +141,18 @@
     int win_count = 0;
     int lose_count = 0;
     int level_index = 1;
+    int sequence_size = 2;
     while(1) {
         input_index = 0;
         mid.setLevel(0);
         right.setLevel(0);
         left.setLevel(0);
-        sequence_size = ((level_index - 1) + 3);
+        sequence_size++;
         level_index = level(level_index, sequence_size);
-        simon_loop();
-        input_loop();
+        simon_loop(sequence_size);
+        input_loop(sequence_size);
         wait(0.3);
-        bool winner = win_or_lose();
+        bool winner = win_or_lose(sequence_size);
         if (winner == true) { 
             win_count++; 
             success(); 
@@ -162,10 +162,10 @@
         }
         if (lose_count > 0) { 
             start_flag = false;
-            wait(1);
             win_count = 0;
             lose_count = 0;
-            level_index = 1;;
+            level_index = 1;
+            sequence_size = 2;
         }
     }
 }
\ No newline at end of file