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:
- 2:8ff96f7acb45
- Parent:
- 1:1aca092ceb7e
- Child:
- 3:96879db74ec8
--- a/main.cpp Wed Aug 17 15:22:18 2016 +0000 +++ b/main.cpp Wed Aug 17 15:45:49 2016 +0000 @@ -7,10 +7,22 @@ DigitalIn button_left(A2); DigitalIn button_top(A1); DigitalIn button_right(A0); -int input[10]; +int input[50]; int sequence[] = {3, 2, 1, 3, 2, 1}; -void simon_loop() { +void start_up() { + //generate random simon says sequence + + //wait for user input + while(!(button_left or button_right or button_top)){ + + } + +} + +/* 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++) { if (sequence[i] == 1) { right.setLevel(10); @@ -30,7 +42,10 @@ } } +/* 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) { @@ -58,6 +73,8 @@ 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++) { if (input[k] == sequence[k]) { continue; } @@ -66,24 +83,26 @@ return true; } +/* display a happy LED animation for winning (LED levels 0 to 10 fast, twice) */ void success() { for(int h = 0; h <=10; h++){ top.setLevel(h); right.setLevel(h); left.setLevel(h); - wait(0.1); + wait(0.05); } for(int h = 0; h <=10; h++){ top.setLevel(h); right.setLevel(h); left.setLevel(h); - wait(0.1); + 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) */ void failure() { for(int g = 0; g <= 10; g++){ top.setLevel(10-g); @@ -97,12 +116,12 @@ top.setLevel(0); right.setLevel(0); left.setLevel(0); - // wait for user to start simon says + start_up(); wait(0.3); simon_loop(); input_loop(); wait(0.2); bool winner = win_or_lose(); if (winner == true) { success(); } - else { failure; } + else { failure(); } }