Good day everyone,
Just want to ask some help regarding my program using millis() function.
My set-up is composed of 3 relays, controlled by arduino uno.
The 1st relay should first on for 10mins then off for 40mins then repeat
The 2nd relay should first off for 35mins then on for 15mins then repeat
The 3rd relay should first on for 10mins then off for 90mins then repeat
I have successfully uploaded the program, however, the problem is that 1st and 3rd relay is always on and 2nd relay is always off. It never changes its state.
#define RELAY1 6 //inlet
#define RELAY2 7 //exhaust
#define RELAY3 8 //mist maker
long previousMillis1 = 0;
const long interval1 = 21600000; //interval at which LED will switch off
const long interval2 = 7200000; //interval at whihc LED will switch on
long previousMillis2 = 0;
const long interval3 = 900000; //interval at which exhaust will off 900000
const long interval4 = 2100000; //interval at which exhaust will on 2100000
long previousMillis3 = 0;
const long interval5 = 600000; //interval at which inlet will off 600000
const long interval6 = 2400000; //interval at which inlet will on 2400000
long previousMillis4 = 0;
const long interval7 = 600000; //interval at which mist will off 600000
const long interval8 = 5400000; //interval at which mist will on 5400000
void setup() {
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(RELAY3, OUTPUT);
}
void Fan1(){ //inlet fan is 10min on 40mins off
static bool state = 1 ;
digitalWrite(RELAY1, LOW);
unsigned long currentMillis3 = millis();
if(currentMillis3 - previousMillis3 > interval6 && state == 0){
state = 1;
previousMillis3 = currentMillis3;
digitalWrite(RELAY1, LOW);
}
else if(currentMillis3-previousMillis3 > interval5 && state ==1){
state = 0;
previousMillis3 = currentMillis3;
digitalWrite(RELAY1, HIGH);
}
}
void Fan2(){ //exhaust fan is 35mins off 15mins on
static bool state = 0;
digitalWrite(RELAY2, HIGH);
unsigned long currentMillis2 = millis();
if(currentMillis2 - previousMillis2 > interval4 && state == 0){
state = 1;
previousMillis2 = currentMillis2;
digitalWrite(RELAY2, LOW);
}
else if(currentMillis2 - previousMillis2 > interval3 && state == 1){
state = 0;
previousMillis2 = currentMillis2;
digitalWrite(RELAY2, HIGH);
}
}
void MistMaker(){ //mist is 10mins on 90mins off
static bool state = 1;
digitalWrite(RELAY3, LOW);
unsigned long currentMillis4 = millis();
if(currentMillis4 - previousMillis4 > interval8 && state ==0){
state = 1;
previousMillis4 = currentMillis4;
digitalWrite(RELAY3, LOW);
}
else if(currentMillis4-previousMillis4 > interval7 && state ==1){
state = 0;
previousMillis4 = currentMillis4;
digitalWrite(RELAY3, HIGH);
}
}
void loop() {
Fan1();
Fan2();
MistMaker();
}
TestWhite.ino (3.41 KB)