Code not complete since I never got that version with timers working.
Difference to your example is that I have several timer-objects (instances of Timer) not just one Timer (timer)…
/*
PIN 3 = DHT
PIN 6 = Power Realy
PIN 7 = "Connection Lost"-warrning LED
PIN 13 = Work in pprogress - onboard LED
PIN A0 = Light sensor
VPIN V0 = Light
VPIN V1 = Humidity
VPIN V2 = Temeparature
Your basic bathroom ventilation controller - MMonitor humidity and light.
By light-change to over 100, trig relay after 5 min., after light-change to below 100 reset relay after 5.
If humidity is abowe 60%, trig relay until humidity is 50% - set timer for 1h - if relay stille active send pushnotification.
Send monitoring data to blynk and allow for remote manually override.
*/
#include "DHT.h" //DHT22 sensor library
#include <SimpleTimer.h> // here is the SimpleTimer library
#include <UIPEthernet.h> //Serial Ethernet module library
#include <BlynkSimpleUIPEthernet.h> //Blynk library
SimpleTimer timer; // Create a Timer object called "timer"!
//SimpleTimer t2Light; // Create a Timer object for relay hold after light off
//SimpleTimer t1h; // Create a Timer object for 1 hour warrning
//#define BLYNK_PRINT Serial
#define DHTPIN 3 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE); //Delade DHT instance
const byte arduino_mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; //DE:ED:BA:FE:FE:ED
//IPAddress server_ip (192, 168, 2, 35);
//IPAddress arduino_ip (192, 168, 2, 179);
//IPAddress dns_ip (192, 168, 2, 1);
//IPAddress gateway_ip (192, 168, 2, 1);
//IPAddress subnet_mask (255, 255, 255, 0);
const char auth[] = "####"; //Token from Blynk app
int hum; //Stores humidity value
int temp; //Stores temperature value
int newLux = 0; //latest read lux
int lux = 0; //Light sensor read
int tLoop = 1;
int t2Light = 2;
int t1h = 3;
//const int ledPin = 13;
const byte warrningPin = 7;
const byte relayPin = 6;
void setup() //startup configurations
{
//pinMode(ledPin, OUTPUT);
pinMode(warrningPin, OUTPUT);
pinMode(relayPin, OUTPUT);
dht.begin(); //start DHT22 sensor
Serial.begin(115200); //start serial communication used for Ethernet module
Blynk.begin(auth, IPAddress(192, 168, 2, 35), 8442, IPAddress(192, 168, 2, 179), IPAddress(192, 168, 2, 1), IPAddress(192, 168, 2, 1), IPAddress(255, 255, 255, 0), arduino_mac); //Start blynk with Token, server IP, port
// while(Blynk.connected() == false){
//Serial.println("connecting to blynk");
//}
tLoop = timer.setInterval(5000L, tf); // Here you set interval and which function to call (time, function)
t2Light = timer.setTimeout(120000L, relayOff); // 2-min lights timeout to fan-start
t1h = timer.setTimeout(3600000L, relayOff); // Timer set to 1 hour
}
void hum1Hour(){
digitalWrite(relayPin, LOW);
Blynk.notify("FAN OFF - RUNNING FOR MORE THAN 1 HOUR!!");
}
void relayOff(){
digitalWrite(relayPin, LOW);
Blynk.notify("FAN OFF - LIGHTS OUT FOR 2 MINUTS.");
}
BLYNK_CONNECTED(){
digitalWrite(warrningPin, HIGH); //LED turns off when Bllynk is conneted once again
Blynk.syncAll();
Blynk.tweet("NANO Ventilation Connected");
}
void tf() //Timed functions goes here
{
// digitalWrite(ledPin, LOW);
hum = int(10.0*dht.readHumidity()); //Read humidity
temp = int(10.0*dht.readTemperature()); //Read temperature
newLux = analogRead(A0);
if(Blynk.connected()){
Blynk.virtualWrite(V2, (float(temp/10.0))); //Write temperature to virtual Blynk-pin
Blynk.virtualWrite(V1, (float(hum/10.0))); //Write humidity to virtual Blynk-pin
Blynk.virtualWrite(V0, newLux); //Read analog pin A0 and write value to virtual blynk-pin 0
// digitalWrite(ledPin, HIGH);
}else{
digitalWrite(warrningPin, LOW);
}
if(lux != newLux){
if(((100 - newLux) + lux) < 0){ // Light changed to on
}else if(((100-newLux) + lux) > 0){ // Lights changed to off
timer.restartTimer(t2Light);
}
}
lux = newLux;
if(hum/10 > 60){
if(digitalRead(relayPin) == LOW){
digitalWrite(relayPin, HIGH);
timer.restartTimer(t1h);
Blynk.notify("VENT ON from Humidity.");
}
}
else {
//Serial.println("else");
if(digitalRead(relayPin) == HIGH){
//Serial.println("if relay high");
digitalWrite(relayPin, LOW);
Blynk.notify("FAN OFF - Humidity below 50%");
timer.disable(t1h);
}
}
}
void loop() //main loop
{
Blynk.run(); //All the Blynk Magic starts here...
timer.run(); //Run timer
}