I am requesting guidance in controlling an Arduino compatible relay board that has 8 5v controlled relays. I am having trouble modifying my code to get what I want. The code below works and turns on my relays, however I want to turn/off relays at specific times and for specific periods. It appears the millis() command could be used to turn the relays on/off at the specific times and durations, however I have not found examples to learn to implement this. Below is the timing of turning on/off the relays:
Time 0: Turn on Pump1 for 8 seconds then off
Time 8-88sec: Turn on pump relief valve for 80 seconds then off
Time 8-8.2 sec: Turn on pump2 for .2sec then off
Time 8-88sec: Turn on compressor for 80 sec then off
Time 18-18.2sec: Turn on pump2 for .2sec then off
// Relay program using the delay command
int led = 2;
int led1 = 3;
int led2 = 4;
// the setup routine runs once when you press reset:
void setup() {
// turn on pump1
int led = 2;
pinMode(led, OUTPUT);
digitalWrite(led, LOW); // turn on relay with pump1
delay(2000); // time for pump1
digitalWrite(led, HIGH);
// turn relay with pump relief valve
int led1 = 3;
pinMode(led1, OUTPUT);
digitalWrite(led1, LOW);
delay(2000);
digitalWrite(led1, HIGH);
// turn on pump2
int led2 = 4;
pinMode(led2, OUTPUT);
digitalWrite(led2, LOW);
delay(200);
digitalWrite(led2, HIGH);
// turn on compressor
int led3 = 5;
pinMode(led3, OUTPUT);
digitalWrite(led3, LOW);
delay(6000);
digitalWrite(led3, HIGH);
}
void loop() {
}
With the code you posted, the delay function pauses the steps until the timer expires, which doesn't allow for multiple operations. You can start a counter and use a while statement for the various outputs to fire when between steps in the incrementing counter.
I didn't push this to a board, but the code compiles and should get you pointed in the right direction.
While this does happen in the main loop of the program, it will only run once and stop when the counter gets to a high point. With this method you could use a push button to reset the values of the startMills and the currentCount variables and let the chain of events run again.
int startMills;
int currentCount;
//Modify the below variables to reflect your actual pin definitions
int pump1 = 0;
int pumpRelief = 1;
int pump2 = 2;
int compressor = 3;
void setup() {
//Set the pin modes and force all the outputs low
pinMode(pump1, OUTPUT);
pinMode(pumpRelief, OUTPUT);
pinMode(pump2, OUTPUT);
pinMode(compressor, OUTPUT);
digitalWrite(pump1, LOW);
digitalWrite(pumpRelief, LOW);
digitalWrite(pump2, LOW);
digitalWrite(compressor, LOW);
//Set the starting point for our counter to the millis() value at startup
startMills = millis();
}
void loop() {
//Set time elapsed counter to new value each loop, but only if the counter is less than 90 seconds
//Once the counter gets above 90 seconds it will stay there until it gets reset
if (currentCount < 90000){
currentCount = millis() - startMills;
}
//While the elapsed counter is less than 8000 turn on pump1
while (currentCount < 8000){
digitalWrite(pump1, HIGH);
}
//Turn off pump when times gets high enough
if (currentCount > 8000){digitalWrite(pump1, LOW);}
//Turn on the pump relief valve when between 8 and 88 seconds
while ((currentCount > 8000) && (currentCount < 88000)){
digitalWrite(pumpRelief, HIGH);
}
//Turn off pump relief valve when above 88 seconds
if (currentCount > 88000){digitalWrite(pumpRelief, LOW);}
//Turn on pump2 when greater than 8 seconds and less than 8.2 seconds
while ((currentCount > 8000) && (currentCount < 8200)){
digitalWrite(pump2, HIGH);
}
//Force pump 2 low when between 8.2 seconds and 18 seconds
while ((currentCount > 8200) && (currentCount < 18000)){
digitalWrite(pump2, LOW);
}
//Turn on pump2 when above 18 seconds and below 18.2 seconds
while ((currentCount > 18000) && (currentCount < 18200)){
digitalWrite(pump2, HIGH);
}
//Turn off pump2 when above 18.2 seconds
if (currentCount > 18200){digitalWrite(pump2, LOW);}
//Turn on compressor when between 8 and 88 seconds
while ((currentCount > 8000) && (currentCount < 88000)){
digitalWrite(compressor, HIGH);
}
//Turn off compressor when above 88 seconds
if (currentCount > 88000){digitalWrite(compressor, LOW);}
}
I think the timing concept in the Blink Without Delay example sketch will be easier to follow. I wrote an extended demo of that concept to show how several things can be controlled at the same time in the first post in this Thread.
...R