// https://forum.arduino.cc/t/void-loop-inside-a-switch-case/1344132/27
#include <LiquidCrystal.h>
#include <Toggle.h>
const byte buttonPin = 8; // the number of the pushbutton pin
Toggle selectButton;
LiquidCrystal lcd( 7, 6, 5, 4, 3, 2);
const byte sequenceCount = 6;
byte currentSequence = 0;
// counters
int16_t pushCount = 0;
// up/dn switches
const uint8_t upSWPin = A0;
const uint8_t dnSWPin = A1;
Toggle upSW;
Toggle dnSW;
// prototypes to declare the arrays
void initSequence0();
void initSequence1();
void initSequence2();
void initpusherControl();
void initSequence4();
void initSequence5();
void Sequence0();
void Sequence1();
void Sequence2();
void pusherControl();
void Sequence4();
void Sequence5();
void endSequence0();
void endSequence1();
void endSequence2();
void endpusherControl();
void endSequence4();
void endSequence5();
// arrays of functions pointers
void (*initSequence[])() = {initSequence0, initSequence1, initSequence2, initpusherControl, initSequence4, initSequence5};
void (*sequence[])() = {Sequence0, Sequence1, Sequence2, pusherControl, Sequence4, Sequence5};
void (*endSequence[])() = {endSequence0, endSequence1, endSequence2, endpusherControl, endSequence4, endSequence5};
// some heartbeat for the current sequence
void heartbeat() {
static unsigned long chrono = 0;
if (millis() - chrono >= 5000) {
chrono = millis();
Serial.print("In sequence #"); Serial.println(currentSequence + 1);
}
}
// Implementation of the functions for the sequences
void initSequence0() {
Serial.print("configuring for sequence #");
Serial.println(currentSequence + 1);
}
void initSequence1() {
Serial.print("configuring for sequence #");
Serial.println(currentSequence + 1);
}
void initSequence2() {
Serial.print("configuring for sequence #");
Serial.println(currentSequence + 1);
}
void initpusherControl()
{ lcd.clear();
lcd.print("Case Pusher");
lcd.setCursor(0, 1);
lcd.print(pushCount);
}
void initSequence4() {
Serial.print("configuring for sequence #");
Serial.println(currentSequence + 1);
}
void initSequence5() {
Serial.print("configuring for sequence #");
Serial.println(currentSequence + 1);
}
void Sequence0() {
heartbeat();
}
void Sequence1() {
heartbeat();
}
void Sequence2() {
heartbeat();
}
void pusherControl() {
if (upSW.onPress()) {
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(++pushCount);
}
if (dnSW.onPress()) {
if (pushCount > 0) {
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(--pushCount);
}
}
}
void Sequence4() {
heartbeat();
}
void Sequence5() {
heartbeat();
}
void endSequence0() {
Serial.print("Ending sequence #");
Serial.println(currentSequence + 1);
}
void endSequence1() {
Serial.print("Ending sequence #");
Serial.println(currentSequence + 1);
}
void endSequence2() {
Serial.print("Ending sequence #");
Serial.println(currentSequence + 1);
}
void endpusherControl() {
Serial.print("Ending sequence #");
Serial.println(currentSequence + 1);
}
void endSequence4() {
Serial.print("Ending sequence #");
Serial.println(currentSequence + 1);
delay(3000);
}
void endSequence5() {
Serial.print("Ending sequence #");
Serial.println(currentSequence + 1);
}
void displayCurrentSequence() {
lcd.clear();
lcd.print("Sequence ");
lcd.print(currentSequence + 1);
lcd.setCursor(0, 1);
lcd.print("Count to ");
lcd.print(currentSequence + 4);
}
void handleSelectButton() {
selectButton.poll();
if (selectButton.onPress()) {
endSequence[currentSequence]();
currentSequence = (currentSequence + 1) % sequenceCount;
displayCurrentSequence();
initSequence[currentSequence]();
}
}
void setup() {
selectButton.begin(buttonPin);
upSW.begin(upSWPin);
dnSW.begin(dnSWPin);
Serial.begin(115200);
lcd.begin(16, 2);
lcd.print("-- SEQUENCING --");
delay(1000);
displayCurrentSequence();
initSequence[currentSequence]();
}
void loop() {
handleSelectButton(); // are we switching sequence ?
upSW.poll();
dnSW.poll();
sequence[currentSequence](); // call in a loop the function for the sepcific sequence
}