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:
- 8:7be76afe5597
- Parent:
- 7:b2576b2b916e
- Child:
- 9:ac887bf9f85d
--- a/main.cpp Fri Aug 19 14:30:01 2016 +0000 +++ b/main.cpp Fri Aug 19 14:36:31 2016 +0000 @@ -10,7 +10,6 @@ DigitalOut buzzer(D2); int input_index; int input[100]; -int sequence[100]; bool start_flag = false; void start_level() { @@ -30,7 +29,7 @@ /* 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. */ -int level(int level_index, int sequence_size) { +int level(int level_index, int sequence_size, int* sequence) { for(int i = 0; i < sequence_size; i++) { sequence[i] = rand() % 3 + 1; } @@ -45,7 +44,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(int sequence_size) { +void simon_loop(int sequence_size, int* sequence) { for(int i = 0; i < sequence_size; i++) { if (sequence[i] == 1) { right.setLevel(10); @@ -107,7 +106,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(int sequence_size) { +bool win_or_lose(int sequence_size, int* sequence) { for(int k = 0; k < sequence_size; k++) { if (input[k] == sequence[k]) { continue; } else { return false; } @@ -142,17 +141,18 @@ int lose_count = 0; int level_index = 1; int sequence_size = 2; + int sequence[100]; while(1) { input_index = 0; mid.setLevel(0); right.setLevel(0); left.setLevel(0); sequence_size++; - level_index = level(level_index, sequence_size); - simon_loop(sequence_size); + level_index = level(level_index, sequence_size, sequence); + simon_loop(sequence_size, sequence); input_loop(sequence_size); wait(0.3); - bool winner = win_or_lose(sequence_size); + bool winner = win_or_lose(sequence_size, sequence); if (winner == true) { win_count++; success();