I want to control a water pump using this code. As written, the relay turns on but does not turn off. If I replace the value of 'onTime' with 6000 (6 seconds) and the value of 'offTime' with 2000 (2 seconds) it works but it reverses the HIGH and LOW states. Is there a problem with my conversion to minutes? I'm running this on a mega 2560. As mentioned above, when the code is running using the 'seconds' version, the HIGH and LOW states get reversed. The relay will open for 2 seconds and close for 6. This probably relates to the INPUT vs OUTPUT declaration. Any help is much appreciated.
// This code will pump water to Grow Bed 'A' for 10 minutes and then shut off for 50 minutes and then repeat
int relayPin_GBA = 8; // The number of the Relay pin servicing Grow Beds A
int relayPin_GBA_State = HIGH; // Relay pin State used to set the Relay
unsigned long previousMillis_GBA = 0; // Stores last time Relay was updated
long onTime_GB = 10*60*1000; // 10 minutes of on-time
long offTime_GB = 50*60*1000; // 50 minutes of off-time
void setup()
{
pinMode(relayPin_GBA, OUTPUT); // Set the digital pin as output
}
void loop()
{
unsigned long currentMillis = millis(); // check to see if it's time to change the state of the Relay
if ((relayPin_GBA_State == HIGH) && (currentMillis - previousMillis_GBA >= onTime_GB))
{
relayPin_GBA_State = LOW; // Turn the Relay off
digitalWrite(relayPin_GBA, relayPin_GBA_State); // Update the Relay
previousMillis_GBA = currentMillis; // Remember the time
}
else if ((relayPin_GBA_State == LOW) && (currentMillis - previousMillis_GBA >= offTime_GB))
{
relayPin_GBA_State = HIGH; // turn the Relay on
digitalWrite(relayPin_GBA, relayPin_GBA_State); // Update the Relay
previousMillis_GBA = currentMillis; // Remember the time
}
}