Good Day,
I'm having a problem with a 2 channel relay running from a nodeMCU,
The relay acts like i want it to in the way that it turns on and off for the amounts of time stated, But when turning on the external 5V supply that powers the node and the relay externally. The Coil initially remains LOW/Off for the first off timer count and only then energizes.
Where i Would like the Coil to immediately switch ON/High state from the start and only turn off once the on timer count has been reached.
Hence Restarting it would turn on the relays straight away and not have to wait 30 minutes for an off counter to finish before turning on device.
I Have adapted this code to suit my needs and have tried changing around the logic in the if statements, setting the pin as HIGH in the setup, putting delays on the setup, moving the switch case below the setup thinking it was not setting the pin before the update statement started.. but not managing to get it right,
If anyone is able to point me in the right direction or spot any errors in code, it would be a great help. Thanks
class Switcher
{
// class member variables
byte relayPin; // number of pin for relay
long OnTime;
long OffTime;
int relayState; // set relay state (active HIGH)
unsigned long previousMillis; // set time since last update
public:
Switcher(byte pin, long on, long off)
{
relayPin = pin;
pinMode(relayPin, OUTPUT);
OnTime = on;
OffTime = off;
relayState = HIGH;
previousMillis = 0;
}
void Update()
{
// check the time to see if relays need to be turned on
// or off
unsigned long currentMillis = millis();
if ((relayState == HIGH) && (currentMillis - previousMillis>= OffTime))
{
relayState = LOW; // Turn it off
previousMillis = currentMillis; // Remember the time
digitalWrite(relayPin, relayState); //update the relay
}
else if ((relayState == LOW) && (currentMillis - previousMillis >= OnTime))
{
relayState = HIGH; // turn it on
previousMillis = currentMillis;
digitalWrite(relayPin, relayState);
}
}
};
Switcher waterpump(14, 60000, 300000);
Switcher vortexpump(12, 600000, 1800000);
void setup() {
// put your setup code here, to run once:
digitalWrite(14, OUTPUT);
digitalWrite(12, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
waterpump.Update();
vortexpump.Update();
}