A simple Simon Says game using a Grove Base Shield, 3 Grove LED Bars, 1 Grove Buzzer, and 3 Grove Buttons.
Components Used
Platform: ST-Nucleo-F401RE
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
Diff: main.cpp
- Revision:
- 5:b6f37ce2a9a3
- Parent:
- 4:9950f9ec46cf
- Child:
- 6:1c87edbbafe0
--- a/main.cpp Thu Aug 18 19:43:09 2016 +0000
+++ b/main.cpp Thu Aug 18 21:34:55 2016 +0000
@@ -1,28 +1,53 @@
#include "mbed.h"
#include "LED_Bar.h"
+#include "main.h"
-LED_Bar top(D6, D5);
-LED_Bar right(D8, D7);
-LED_Bar left(D4, D3);
-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;
+int sequence[100];
+int level_index = 1;
+int level_array;
+int win_count = 0;
+int lose_count = 0;
+bool start_flag = false;
+
+void start_level() {
+ right.setLevel(10);
+ top.setLevel(10);
+ left.setLevel(10);
+ wait(0.2);
+ right.setLevel(0);
+ top.setLevel(0);
+ left.setLevel(0);
+ wait(0.2);
+ right.setLevel(10);
+ top.setLevel(10);
+ left.setLevel(10);
+ wait(0.2);
+ right.setLevel(0);
+ top.setLevel(0);
+ left.setLevel(0);
+ start_flag = true;
+ return;
+}
void level() {
- //generate random simon says sequence
-
- //wait for user input
- while(!(button_left or button_right or button_top)){
-
+ /* generate random simon says sequence:
+ level 1 will have a 3 LED blink combination, with each subsequent level
+ having 1 more LED blink, so level 2 has a 4 blink combination, etc. */
+ for (int i = 0; i < level_array; i++) {
+ sequence[i] = rand() % 3 + 1; // choose a random number b/w 1 to 3
}
+ level_index++;
+
+ /* button interrupts when user wants to begin the game/level */
+ button_right.rise(&start_level);
+ button_top.rise(&start_level);
+ button_left.rise(&start_level);
+ while(!start_flag) {
+ wait(0.2); // waiting for user to begin game LED animation
+ // perfect time to play waiting buzzer music
+ }
}
/* simon says loop: turn on corresponding LED Bars according to the #
@@ -33,22 +58,24 @@
right.setLevel(10);
wait(0.4);
right.setLevel(0);
- }
- else if (sequence[i] == 2) {
+ wait(0.4);
+ } else if (sequence[i] == 2) {
top.setLevel(10);
wait(0.4);
top.setLevel(0);
- }
- else {
+ wait(0.4);
+ } else {
left.setLevel(10);
wait(0.4);
left.setLevel(0);
+ wait(0.4);
}
}
}
void right_input() {
input[input_index] = 1;
+ input_index++;
while(button_right) {
right.setLevel(10);
}
@@ -58,6 +85,7 @@
void top_input() {
input[input_index] = 2;
+ input_index++;
while(button_top) {
top.setLevel(10);
}
@@ -67,6 +95,7 @@
void left_input() {
input[input_index] = 3;
+ input_index++;
while(button_left) {
left.setLevel(10);
}
@@ -77,14 +106,13 @@
/* 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() {
- button_right.rise(&right_input);
+ button_right.rise(&right_input); //interrupt when right button is pressed
button_top.rise(&top_input);
button_left.rise(&left_input);
- while(1) {
+ while(input_index < sequence_size) {
wait(0.2);
- if (input_index <= sequence_size) { continue; }
- else { return; }
}
+ input_index = 0;
}
/* compare input array to simon says sequence array, if they are a match
@@ -111,9 +139,6 @@
left.setLevel(h);
wait(0.05);
}
- top.setLevel(0);
- right.setLevel(0);
- left.setLevel(0);
}
/* display a sad LED animation for losing (LED levels 10 to 0 slow) */
@@ -128,25 +153,30 @@
void start_game() {
input_index = 0;
- sequence_size = sizeof(sequence)/sizeof(sequence[0]);
top.setLevel(0);
right.setLevel(0);
left.setLevel(0);
- //next_level:
- //level();
+ level_array = ((level_index - 1) + 3);
+ sequence_size = level_array;
+ level();
wait(0.3);
simon_loop();
wait(0.3);
input_loop();
- wait(0.2);
+ wait(0.3);
bool winner = win_or_lose();
- if (winner == true) { success(); }
- else { failure(); }
- //if (level < 10) then { GoTo next_level; }
+ if (winner == true) { win_count++; success(); }
+ else { lose_count++; failure(); }
+ if (lose_count > 0) {
+ start_flag = false;
+ wait(1); // add in end game buzzer music
+ win_count = 0;
+ lose_count = 0;
+ level_index = 1;
+ start_game();
+ } else { start_game(); }
}
int main() {
start_game();
-}
-
-//buzzer_timer.attach(&timer_handler, 0.5)
\ No newline at end of file
+}
\ No newline at end of file
Jenny Plunkett
