// https://forum.arduino.cc/t/resetting-or-zeroing-the-millis-at-end-of-each-cycle/1221287
// https://wokwi.com/projects/389172935034378241
// Pin definitions
const int relay1 = 53; // Valve 4, Exhuast
const int relay2 = 51; // Valve 3, Pressurize
const int relay3 = 49; // Valve 2, Slide Movement
const int relay4 = 47; // Valve 1, Chuck Movement
const int relay5 = 45; // Valve 1, Chuck Movement
byte relayPin[] = {relay1, relay2, relay3, relay4, relay5};
byte NRELAYS = sizeof relayPin / sizeof *relayPin;
unsigned long aTimer;
unsigned long now; // always millis() for the entire loop
const int INTERVAL = 333; // step duration
const byte R1 = 1;
const byte R2 = 2;
const byte R3 = 4;
const byte R4 = 8;
const byte R5 = 16;
byte relayBit[] = {R1, R2, R3, R4, R5};
byte sequence[] = {
0,
0,
0,
R1,
R1 + R3,
R1 + R3 + R5,
R2 + R4,
R1 + R3 + R5,
R2 + R4,
R1 + R3 + R5,
R2 + R4,
R1 + R3 + R5,
R3 + R5,
R5,
0,
0,
0,
R1 + R2 + R4 + R5,
R3,
R1 + R2 + R4 + R5,
R3,
R1 + R2 + R4 + R5,
R3,
R1 + R2 + R4 + R5,
0,
0,
};
const int sequenceLength = sizeof sequence / sizeof *sequence;
void setup() {
Serial.begin(115200);
Serial.println("UA Sequencer V.0\n");
for (int ii = 0; ii < NRELAYS; ii++) {
pinMode(relayPin[ii], OUTPUT);
digitalWrite(relayPin[ii], LOW);
}
}
void loop() {
static unsigned long lastTime;
static int step;
now = millis();
if (now - lastTime < INTERVAL) return; // not time to do anything - use 1000 for real once a second loop, life too short
lastTime = now;
for (int ii = 0; ii < NRELAYS; ii++) {
if (sequence[step] & relayBit[ii])
digitalWrite(relayPin[ii], HIGH);
else
digitalWrite(relayPin[ii], LOW);
}
step++;
if (step >= sequenceLength) step = 0; // rinse and repeat
}