I have 8 pins connected to 8 solenoids and I need to turn them all on and after a brief delay off again at regular intervals. The problem is that each one of the pins needs to be for a different amount of time because of variables in the hardware. That is to say, that they will on turn on at the same time but will turn off one at time at different intervals
greengiant83:
I have 8 pins connected to 8 solenoids and I need to turn them all on and after a brief delay off again at regular intervals. The problem is that each one of the pins needs to be for a different amount of time because of variables in the hardware. That is to say, that they will on turn on at the same time but will turn off one at time at different intervals
How can I do this with an arduino?
An approach that should work is to set variables with the millis() value that the solenoids should
turn off at. During the loop() cycle, periodically check these values and turn off at the right time:
long solenoid1stop, solenoid2stop, solenoid3stop...;
void solenoidsOn() {
digitalWrite(solenoid1pin, HIGH);
digitalWrite(solenoid2pin, HIGH);
[..etc..]
solenoid1stop = millis() + [millis to stay on for];
solenoid2stop = millis() + [millis to stay on for];
}
void loop () {
...
if (solenoid1stop && millis() > solenoid1stop) {
digitalWrite(solenoid1pin, LOW);
solenoid1stop = 0;
}
if (solenoid2stop && millis() > solenoid2stop) {
digitalWrite(solenoid2pin, LOW);
solenoid2stop = 0;
}
...
}