Allow disabling sound at startup

This commit is contained in:
Gabriel Augendre 2021-05-04 15:47:10 +02:00
parent ee91c550a2
commit 2ecd7546d7
3 changed files with 20 additions and 8 deletions

View file

@ -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

View file

@ -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);

View file

@ -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) {
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() {
void error(byte expectedButton) {
if (soundEnabled) {
tone(BUZZER, TONE_ERROR, 500);
}
blink(expectedButton);
delay(500);
}