I am trying to make a state machine with if/else. But some of the code does not seem to be executing. I have information being displayed on an LCD screen so I know where it is, and it stays on Todo=1 and does not go to the next state. I have tried using switch/case and it works. But I think it might be better for my project if I use if/else. Can I do this using if/else?
Void loop{
if (state == PLAY) {
Todo = 1;
Mastermachine(Todo);
} else if (state == TRYAGIN) {
}
}
void Mastermachine(int Todo) {
if (Todo == 1) {
lcd.setCursor(16, 2);
lcd.print(String(Todo));
SongSeq[largestindex];
for (int index = 0; index < largestindex; index++) {
SongSeq[index]();
}
Todo = 2;
} else if (Todo == 2) {
for (learnindex = 0; learnindex < largestindex;) {
MIDI.read();
if (learnindex >= largestindex) {
Todo = 3;
}
lcd.setCursor(16, 2);
lcd.print(String(Todo));
}
} else if (Todo == 3) {
if (array_cmp(learn, note, largestindex, largestindex) == true) {
largestindex++;
delay(2000);
Todo = 1;
} else {
delay(2000);
Todo = 1;
}
lcd.setCursor(16, 2);
lcd.print(String(Todo));
}
}
Todo
is lost after each call ofMasterMachine()
(if you do no introduce a loop).