So I am trying to use a switch statement to control states in my simple game but the issue I am experiencing is that when I switch to the play state, it doesnt run it at all, in the ready state I change the current_state and it also prints the play state ID (3) and it just doesnt run it at all, I am lost for words what I am doing wrong.
I am not sure what I am doing wrong, if I change it from play to another state like Select, that works but I cant change to play, I AM SAD!
#include <pitches.h>
#include <Button.h>
// PINS
const int buzzer_pin = 9;
// Button pins
const int button_pin_1 = 8;
const int button_pin_2 = 7;
const int button_pin_3 = 6;
const int button_pin_4 = 5;
Button button_1 = Button(button_pin_1);
Button button_2 = Button(button_pin_2);
Button button_3 = Button(button_pin_3);
Button button_4 = Button(button_pin_4);
enum states {Ready,Select,Check,Play,Won,Lost,Reset};
// Ready setups the game
// Select allow input from the player
// Check if the player has guessed correctly or incorrectly
// Play Plays the sequence
// Won plays a win tune and then goes to reset
// Lost plays a tune and then transitions to reset
// Reset resets the game and goes back to ready - creating a loop!
// The start controller
int current_state = Ready;
int won_tune[] = {
NOTE_C5, NOTE_E5, NOTE_G5, NOTE_C6, // ascending major triad
NOTE_G5, NOTE_E5, NOTE_C5, // descending
NOTE_F5, NOTE_A5, NOTE_C6, NOTE_F6, // new lift
NOTE_C6 // final accent
};
int won_durations[] = {
8, 8, 8, 4,
8, 8, 4,
8, 8, 8, 4,
2
};
int lost_tune[] = {
NOTE_E3, NOTE_C3, NOTE_D3, NOTE_G2,
NOTE_E2, NOTE_C2
};
int lost_durations[] = {
4, 4, 4, 4,
2, 2
};
int scale_duration = 250;
int scale_sounds[] = {
NOTE_C3, // 1 - Low
NOTE_C4, // 2 - Mid-low (1 octave up)
NOTE_C5, // 3 - Mid-high (2 octaves up)
NOTE_C6 // 4 - High (3 octaves up)
};
// Guesses is what the player has tried to guess
int guesses[10];
// Results is what we need to guess correctly
int results[10];
int game_length = 9;
int current_index = 0;
int current_target = 0;
int target = 2;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
init_buttons();
// Delaying for 1 second avoids running code again
delay(1000);
}
void loop() {
handle_states();
}
void handle_states() {
switch(current_state) {
case Ready:
//
generate_results();
play_scales();
delay(150);
Serial.println(current_state);
current_state = Play;
break;
case Select:
//
bool result = select_input();
if (result){
int pitch = guesses[current_index];
play_pitch(pitch);
current_state = Check;
break;
}
break;
case Check:
//
break;
case Play:
//
Serial.println("In the play state");
play_sequence();
current_state = Select;
break;
case Won:
//
break;
case Lost:
//
break;
case Reset:
//
break;
}
}
void init_buttons() {
button_1.begin();
button_2.begin();
button_3.begin();
button_4.begin();
}
bool select_input() {
// put your main code here, to run repeatedly:
if (button_1.is_just_pressed()) {
guesses[current_index] = 1;
return true;
}
if (button_2.is_just_pressed()) {
guesses[current_index] = 2;
return true;
}
if (button_3.is_just_pressed()) {
guesses[current_index] = 3;
return true;
}
if (button_4.is_just_pressed()) {
guesses[current_index] = 4;
return true;
}
return false;
}
void play_victory_tune() {
for (int i = 0; i < sizeof(won_tune) / sizeof(int); i++) {
int duration = 1000 / won_durations[i];
tone(buzzer_pin, won_tune[i], duration);
delay(duration * 1.3);
noTone(buzzer_pin);
}
}
void generate_results() {
//
for (int i = 0; i < game_length; i++) {
results[i] = random(1,5);
}
}
void play_scales() {
//
for (int i = 0; i < 4; i++) {
play_pitch(i + 1);
delay(150);
}
}
void play_sequence() {
for (int i = 0; i < target; i = i + 1) {
int result = results[i];
Serial.println(result);
play_pitch(result);
delay(300);
}
}
void play_pitch(int guess) {
if (guess <= 0) {
return;
}
if (guess > 4) {
return;
}
switch(guess) {
case 1:
tone(buzzer_pin, scale_sounds[0]);
delay(scale_duration);
noTone(buzzer_pin);
break;
case 2:
tone(buzzer_pin, scale_sounds[1]);
delay(scale_duration);
noTone(buzzer_pin);
break;
case 3:
tone(buzzer_pin, scale_sounds[2]);
delay(scale_duration);
noTone(buzzer_pin);
break;
case 4:
tone(buzzer_pin, scale_sounds[3]);
delay(scale_duration);
noTone(buzzer_pin);
break;
}
}