// https://forum.arduino.cc/t/cancel-button-code/1191125
// https://wokwi.com/projects/382021819542270977
enum {ABLE = 0, BAKER, CHARLIE, DELTA, ECHO,};
char *relayTags[] = {"ABLE_", "BAKER_", "CHARLIE_", "DELTA_", "ECHO_",};
const byte relayPin[] = {2, 3, 5, 6, 7};
const byte nRelays = sizeof relayPin / sizeof relayPin[0];
const unsigned int onTime = 577;
const unsigned int offTime = 1222;
const byte killSwitch = 4;
void setup()
{
Serial.begin(115200);
Serial.println("Really Relays.");
Serial.print("machine sequence ");
Serial.print(nRelays);
Serial.println(" relays.\n");
for (byte ii = 0; ii < nRelays; ii++) {
pinMode(relayPin[ii], OUTPUT);
}
pinMode(killSwitch, INPUT_PULLUP);
startMixMachine();
}
unsigned long now;
bool running = true;
void loop() {
now = millis();
if (running) mixMachine();
else {
Serial.println("machine stopped or finished");
for (; ;); // dies here all done or interrupted
}
if (digitalRead(killSwitch) == LOW) killMixMachine();
}
enum states {IDLE, START, ON, OFF, DONE,};
char *stateTags[] = {"idle", "initialize", "relay on", "relay off", "all done!",};
byte theState = IDLE;
void startMixMachine()
{
theState = START;
}
void killMixMachine()
{
Serial.println("\n\n");
Serial.println("stop the mix.");
for (byte ii = 0; ii < nRelays; ii++) {
Serial.print("turn off relay ");
Serial.println(relayTags[ii]);
digitalWrite(relayPin[ii], LOW);
}
Serial.println("\n");
running = false;
theState = IDLE;
}
void mixMachine()
{
static unsigned long lastTime;
static unsigned long dwell;
static byte relayNumber;
if (dwell)
if (now - lastTime < dwell)
return;
lastTime = now;
switch (theState) {
case IDLE :
break;
case START :
Serial.println("release the hounds!");
relayNumber = 0;
theState = OFF;
break;
case OFF :
Serial.print("turn on relay ");
Serial.println(relayTags[relayNumber]);
digitalWrite(relayPin[relayNumber], HIGH);
dwell = onTime;
theState = ON;
break;
case ON :
Serial.print("turn off relay ");
Serial.println(relayTags[relayNumber]);
digitalWrite(relayPin[relayNumber], LOW);
relayNumber++;
if (relayNumber == nRelays) {
dwell = 0;
theState = DONE;
break;
}
dwell = offTime;
theState = OFF;
break;
case DONE :
running = false;
theState = IDLE;
break;
}
}
/*
// version 2
enum {ABLE = 0, BAKER, CHARLIE, DELTA, ECHO};
const byte relayPin[] = {2, 3, 4, 5, 6};
const byte nRelays = sizeof relayPin / sizeof relayPin[0];
void setup()
{
Serial.begin(115200);
Serial.println("Really Relays.");
Serial.print("machine sequence ");
Serial.print(nRelays);
Serial.println(" relays.\n");
startMixMachine();
}
unsigned long now;
bool running = true;
void loop() {
now = millis();
if (running) mixMachine();
else {
Serial.println("machine stopped or finished");
for (; ;); // dies here all done or interrupted
}
}
enum states {IDLE, START, ON, OFF, DONE,};
char *stateTags[] = {"idle", "initialize", "relay on", "relay off", "all done!"};
byte theState = IDLE;
void startMixMachine()
{
theState = START;
}
void mixMachine()
{
static unsigned long lastTine;
static unsigned long dwell;
static byte relayNumber;
Serial.print("the state is "); Serial.println(stateTags[theState]);
switch (theState) {
case IDLE :
break;
case START :
relayNumber = 0;
theState = OFF;
break;
case OFF :
Serial.print("turn on relay ");
Serial.println(relayNumber);
theState = ON;
break;
case ON :
Serial.print("turn off relay ");
Serial.println(relayNumber);
relayNumber++;
if (relayNumber == nRelays) {
theState = DONE;
break;
}
theState = OFF;
break;
case DONE :
running = false;
theState = IDLE;
break;
}
}
/*
// version 0
enum {ABLE = 0, BAKER, CHARLIE, DELTA, ECHO};
const byte relayPin[] = {2, 3, 4, 5, 6};
const byte nRelays = sizeof relayPin / sizeof relayPin[0];
void setup()
{
Serial.begin(115200);
Serial.println("Really Relays.");
Serial.print("machine sequence ");
Serial.print(nRelays);
Serial.println(" relays.\n");
}
void loop() {}
*/