///Currently programmed to be plugged in on Saturday at 10am. ///To change this modify day and/or hour below int day = 7; //1=sunday...7=saturday int hour = 10; //requires plug in at noon /// int relayPinoff = 1; // the number of the LED pin, INA int relayPin = 0; // the number of the relay pin, INB //OUTA to bottom left, OUTB to right int minute = 0; int power = 1; //0=off 1=on(NC) relay will start in NC mode, power on unsigned long previousMillis = 0; // the follow variables is a long because the time, measured in miliseconds, // will quickly become a bigger number than can be stored in an int. long interval = 30235; // interval of one minute. millis not very accurate void setup() { // set the digital pins as outputs: pinMode(relayPinoff, OUTPUT); pinMode(relayPin, OUTPUT); digitalWrite(relayPinoff, LOW); digitalWrite(relayPin, HIGH); //switch the relay to NC (heater on) delay(25); //for relay time of 7ms digitalWrite(relayPin, LOW); } void loop() { //rollover at 4,294,967,295ms =4,294,967s =71582.78m =1193.046h =49.7day //even if after rollover current - previous will calculate the correct difference // 7302 - 4,294,967,255 = 7342ms //1hr=60min=3600s=3600000ms, 1m=60s=60000ms // check to see if it's time to switch the relay; that is, if the time // is between noon or 1900 hrs the relay/power to heater should be off. // if the difference between the current time and last time you updated // the minute is bigger than the interval, i.e. it has been another minute // then update the minute counter, and hour counter if needed. unsigned long currentMillis = millis(); if(currentMillis - previousMillis > interval) { //this section runs every minute if (minute ==59) { minute = 0; if (hour ==23) { hour = 0; //midnight if (day < 7) { day = day +1; } else { day =1; } } else { hour = hour +1; } } else { minute = minute +1; } previousMillis = currentMillis; if (day >= 2 && day <=6) { //weekdays if (hour >= 12 && hour < 19 && power == 1) { //power off during peak hrs noon-7pm digitalWrite(relayPinoff, HIGH); //switch the relay to NO (heater off) //digitalWrite(LEDPin, LOW); //mark that heater is off delay(25); //for relay time of 7ms digitalWrite(relayPinoff, LOW); //latching relay requires no power after switching power = 0; } else if (hour >= 19 || hour < 12 && power == 0) { digitalWrite(relayPin, HIGH); //switch the relay to NC (heater on) //digitalWrite(LEDPin, HIGH); //mark that heater is on delay(25); //for relay time of 7ms digitalWrite(relayPin, LOW); power = 1; } } else if (day == 1 || day == 7 && power == 0) { //unnecessary elseif, but demonstrates point digitalWrite(relayPin, HIGH); //switch the relay to NC (heater on) //digitalWrite(LEDPin, HIGH); //mark that heater is on delay(25); //for relay time of 7ms digitalWrite(relayPin, LOW); power = 1; } } }