// https://forum.arduino.cc/t/timing-for-many-different-solenoid-valves-fountain-show/1048846/9
#include <Arduino.h>
// define pins for valves
# define NVALVES 5
# define MAXSEQUENCE 10
const unsigned char valvePin[NVALVES] = {
8, 9, 10, 11, 12, };
unsigned long valveTimer[NVALVES];
unsigned char valveProgramCounter[NVALVES];
unsigned char valveState[NVALVES];
#define PILOT_VALVE 7
unsigned long now; // always the current value of millis() if we are doing thins right
// show sequence timing
// MAKE SURE THERE ARE AN EVEN NUMBER OF ENTRIES OR VALVE WILL STAY ON
const unsigned char valveLength[NVALVES] = {8, 8, 8, 8, 8, }; // or use n/2?
unsigned long valveSequence[NVALVES][MAXSEQUENCE] = {
{500, 80, 500, 160, 500, 160, 500, 500},
{700, 80, 700, 80, 700, 80, 15, 780},
};
// easy to use helper-function for non-blocking timing
// well it's a long way to go for one line of code - eliminate after it is working
boolean TimePeriodIsOver(unsigned long startOfPeriod, unsigned long TimePeriod)
{
unsigned long currentMillis = now;
return currentMillis - startOfPeriod >= TimePeriod;
}
// function that switches the valve on or off
// that's all! according to its current state
void switchValve(unsigned char theValve)
{
digitalWrite(valvePin[theValve], valveState[theValve]);
}
void setup()
{
delay(777);
Serial.begin(9600);
Serial.println("solenoid control unit 0\n");
for (unsigned char ii = 0; ii < NVALVES; ii++) {
pinMode(valvePin[ii], OUTPUT);
valveState[ii] = 0; // OFF
digitalWrite(valvePin[ii], LOW);
valveProgramCounter[ii] = 0;
}
//digitalWrite(PILOT_VALVE, HIGH);
Serial.println("delay(5000)? Life too short, here we go!\n");
}
void main_case_loop()
{
for (unsigned char theValve = 0; theValve < NVALVES; theValve++) {
switch (valveState[theValve]) {
case 0 : // OFF. has its timer expired? turn on
// advance / reset the program counter
// get the ON time, set the timer
// go to state = 1 (ON)
break;
case 1 : // ON. has its timer expired? turn off
// get the OFF time, set the timer
// go to state = 1 (ON)
break;
}
}
}
void loop()
{
now = millis(); // everyone set your clocks by this
main_case_loop();
}