From 2ecd7546d77590ebc7aa8caa8b53ebccecd14a46 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Tue, 4 May 2021 15:47:10 +0200 Subject: [PATCH] Allow disabling sound at startup --- super_simon/include/main.h | 6 ++++-- super_simon/include/utils.h | 4 +++- super_simon/src/utils.cpp | 18 +++++++++++++----- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/super_simon/include/main.h b/super_simon/include/main.h index 3b9db81..d3d041d 100644 --- a/super_simon/include/main.h +++ b/super_simon/include/main.h @@ -8,10 +8,10 @@ #define LCD_COLS 16 #define LCD_ROWS 2 -#define BUZZER 11 +#define BUZZER 10 #define BUTTON_GREEN 2 -#define BUTTON_YELLOW 10 +#define BUTTON_YELLOW 11 #define BUTTON_BLUE 12 #define BUTTON_RED 3 @@ -26,6 +26,8 @@ #define TONE_RED 440 #define TONE_ERROR 100 +#define GREEN_INDEX 0 + #define MAX_GAME 20 #endif //SUPER_SIMON_MAIN_H diff --git a/super_simon/include/utils.h b/super_simon/include/utils.h index d6893b1..c50f482 100644 --- a/super_simon/include/utils.h +++ b/super_simon/include/utils.h @@ -12,7 +12,9 @@ void deactivateAll(); void playSequence(const byte sequence[], byte upTo); bool userSequence(const byte sequence[], byte upTo); byte waitForButton(); -void error(); + +void error(byte index); + void endGame(LiquidCrystal lcd, bool win, byte score); void configure(); void blink(byte index); diff --git a/super_simon/src/utils.cpp b/super_simon/src/utils.cpp index 047b09f..37aebe3 100644 --- a/super_simon/src/utils.cpp +++ b/super_simon/src/utils.cpp @@ -11,8 +11,12 @@ const uint8_t LEDS[] = {LED_GREEN, LED_YELLOW, LED_BLUE, LED_RED}; const uint8_t BUTTONS[] = {BUTTON_GREEN, BUTTON_YELLOW, BUTTON_BLUE, BUTTON_RED}; const uint16_t TONES[] = {TONE_GREEN, TONE_YELLOW, TONE_BLUE, TONE_RED}; +bool soundEnabled = true; void configure() { + if (buttonIsPressed(GREEN_INDEX)) { + soundEnabled = false; + } for (byte i = 0; i < 4; i++) { const byte led = LEDS[i]; pinMode(led, OUTPUT); @@ -31,7 +35,9 @@ void activate(byte index) { } void buzz(byte index, unsigned long duration) { - tone(BUZZER, TONES[index], duration); + if (soundEnabled) { + tone(BUZZER, TONES[index], duration); + } } bool buttonIsPressed(byte index) { @@ -64,16 +70,18 @@ bool userSequence(const byte sequence[], byte upTo) { deactivateAll(); } else { - error(); - blink(expectedButton); + error(expectedButton); return false; } } return true; } -void error() { - tone(BUZZER, TONE_ERROR, 500); +void error(byte expectedButton) { + if (soundEnabled) { + tone(BUZZER, TONE_ERROR, 500); + } + blink(expectedButton); delay(500); }