Hello, I set this code for my arduino, It was working fine for around 36 hours until one day I came home and my relay switched on (water solenoid) and didn't turn off like it was supposed to. I had to disconnect the wire and mop up my flooded house -_-.
So my program is set using a Millis() function, and written into my own little function. this code switched a solenoid on for 5 seconds and then keep it off for an hour, repeatedly.
but I dont' understand why it would stay on like that. I may have coded in a bug that I cannot seem to fix. My plants are depending on this and so it really sucks that I can't seem to figure it out.
Can you please check it out and let me know what I have done to make it do that (stay on for longer than 5 seconds)?
unsigned char relayPin[4] = {4,5,6,7}; // Define the led's pin
void Zone(unsigned long on, unsigned long off, int i, unsigned long previousMilis[]);
int mode[4]={LOW,LOW,LOW,LOW};
unsigned long previousMillis[4]={0,0,0,0};
unsigned long Sec = 1000L;
unsigned long Min= 60*1000L;
unsigned long Hour=60*Min;
unsigned long onTime1=1*Sec;
unsigned long offTime1=5*Min;
unsigned long currentMillis[4]={0,0,0,0};
void setup()
{
int i;
for(i = 0; i < 4; i++)
{
pinMode(relayPin[i],OUTPUT);
digitalWrite(relayPin[i],mode[i]);
}
}
void loop(){
Zone(5000UL,3600000UL,0,previousMillis);
}
void Zone(unsigned long on, unsigned long off, int i, unsigned long previousMilis[]){
currentMillis[i] = millis();
if (currentMillis[i] - previousMillis[i]> off){
previousMillis[i]=currentMillis[i];
while(millis()-previousMillis[i]<=on){
digitalWrite(relayPin[i],HIGH);
}
digitalWrite(relayPin[i],LOW);
}
}