timer.setInterval error

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
SimpleTimer timer;
#define DHTPIN            12    
#d efine DHTTYPE           DHT11
WidgetRTC rtc;
WidgetLCD lcd(V20);
WidgetLED led1(V6);
WidgetLED led2(V7);
WidgetLED led3(V8);
WidgetLED led4(V9);
DHT_Unified dht(DHTPIN, DHTTYPE);
BLYNK_ATTACH_WIDGET(rtc, V0);
//#define server "192.168.10.229"   // or "blynk.cloud-com" for Blynk's cloud server
#define TestLED 2 
#define Relay1 4
#define Relay2 5
#define Relay3 0
#define Relay4 14
// on board LED pin assignment
char Date[16];
char Time[16];
char auth[] = "114ed6bbe86641d6b73579a419f41fe0";
char ssid[] = "ikness";
char pass[] = "12345678";
long startsecondswd;            // weekday start time in seconds
long stopsecondswd;             // weekday stop  time in seconds
long nowseconds;                // time now in seconds

void setup()
{
    Serial.begin(9600);
  Serial.println("\Starting");
  Blynk.begin(auth, ssid, pass);
  pinMode(TestLED, OUTPUT);
  pinMode(Relay1,OUTPUT);
  pinMode(Relay2,OUTPUT);
  pinMode(Relay3,OUTPUT);
  pinMode(Relay4,OUTPUT);
  digitalWrite(TestLED, HIGH); // set LED OFF
  digitalWrite(Relay1, HIGH);
  digitalWrite(Relay2, HIGH);
  digitalWrite(Relay3, HIGH);
  digitalWrite(Relay4, HIGH);
  led1.off();
  led2.off();
  led3.off();
  led4.off();

  int mytimeout = millis() / 1000;
  while (Blynk.connect() == false) { // try to connect to server for 10 seconds
    if((millis() / 1000) > mytimeout + 8){ // try local server if not connected within 9 seconds
       break;
    }
  }
  rtc.begin();
 dht.begin(); 
 timer.setInterval(2000L, activetoday);  // check every minute if schedule should run today 
  timer.setInterval(30000L, reconnectBlynk);  // check every 30s if still connected to server 
  timer.setInterval(2000L, sendUptime);
  timer.setInterval(2000L, Soilsensor);
}

void activetoday(){        // check if schedule should run today
  if(year() != 1970){
    Blynk.syncVirtual(V1); // sync timeinput widget    
  }
}

BLYNK_WRITE(V1) {
  sprintf(Date, "%02d/%02d/%04d",  day(), month(), year());
  sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());
  Blynk.virtualWrite(V13, Time);
  TimeInputParam t(param);
  
  Serial.print("Checked schedule at: ");
  Serial.println(Time);
  int dayadjustment = -1;  
  if(weekday() == 1){
    dayadjustment =  6; // needed for Sunday, Time library is day 1 and Blynk is day 7
  }
  if(t.isWeekdaySelected((weekday() + dayadjustment))){ //Time library starts week on Sunday, Blynk on Monday
    Serial.println("Schedule ACTIVE today");
      lcd.clear(); //Use it to clear the LCD Widget
      lcd.print(1, 0, "ACTIVE Today"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
    if (t.hasStartTime()) // Process start time
    {
      Serial.println(String("Start: ") + t.getStartHour() + ":" + t.getStartMinute());
    }
    if (t.hasStopTime()) // Process stop time
    {
      Serial.println(String("Stop : ") + t.getStopHour() + ":" + t.getStopMinute());
    }
    // Display timezone details, for information purposes only 
    Serial.println(String("Time zone: ") + t.getTZ()); // Timezone is already added to start/stop time 
    Serial.println(String("Time zone offset: ") + t.getTZ_Offset()); // Get timezone offset (in seconds)
  
    for (int i = 1; i <= 7; i++) {  // Process weekdays (1. Mon, 2. Tue, 3. Wed, ...)
      if (t.isWeekdaySelected(i)) {
        Serial.println(String("Day ") + i + " is selected");
      }
    } 
    nowseconds = ((hour() * 3600) + (minute() * 60) + second());
    
    startsecondswd = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
    //Serial.println(startsecondswd);  // used for debugging
    if(nowseconds >= startsecondswd){   
      Serial.print("Motor STARTED");
      Serial.println(String(" ") + t.getStartHour() + ":" + t.getStartMinute());
  lcd.clear(); //Use it to clear the LCD Widget
  lcd.print(1, 0, "Motor STARTED at"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
 // lcd.print(1, 1,(" ") +);
      if(nowseconds <= startsecondswd + 90){    // 90s on 60s timer ensures 1 trigger command is sent
        //digitalWrite(TestLED, LOW); // set LED ON
         digitalWrite(Relay1, LOW);;// code here to switch the relay ON
         led1.on();
      }      
    }
    else{
      Serial.println("Motor NOT STARTED today");
      
      // nothing more to do here, waiting for motor to be turned on later today      
    }
    stopsecondswd = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
    //Serial.println(stopsecondswd);  // used for debugging
    if(nowseconds >= stopsecondswd){
      Serial.print("Motor STOPPED at");
      Serial.println(String(" ") + t.getStopHour() + ":" + t.getStopMinute());
      if(nowseconds <= stopsecondswd + 90){   // 90s on 60s timer ensures 1 trigger command is sent
  lcd.clear(); //Use it to clear the LCD Widget
  lcd.print(1, 0, "Motor STOPPED"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
 // lcd.print(1, 1,(" ") +);
     //   digitalWrite(TestLED, HIGH); // set LED OFF
        digitalWrite(Relay1, HIGH); // code here to switch the relay OFF
        led1.off();
      }              
    }
    else{
      if(nowseconds >= startsecondswd){  // only show if motor has already started today
        Serial.println("Motor is still RUNNING");
        lcd.clear(); //Use it to clear the LCD Widget
        lcd.print(1, 0, "Motor RUNNING"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
        // nothing more to do here, waiting for motor to be turned off later today
      }          
    }
  }
  else{
    Serial.println("Schedule INACTIVE today");
        lcd.clear(); //Use it to clear the LCD Widget
        lcd.print(1, 0, "INACTIVE Today"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
    // nothing to do today, check again in 1 minutes time    
  }
  Serial.println();
}
BLYNK_WRITE(V2){
  if(param.asInt()){
    digitalWrite(Relay4, LOW); // set LED ON
    Serial.println("Button is on");
      }else{
        digitalWrite(Relay4, HIGH); // set LED ON 
     Serial.println("Button is Off");   
      }
}
void sendUptime()
{
  Blynk.virtualWrite(V5, millis() / 1000);
  sensors_event_t event;  
  dht.temperature().getEvent(&event);
  if (!isnan(event.temperature)) {
    Blynk.virtualWrite(V10, event.temperature);
    Serial.print("Temperature: ");
    Serial.println(event.temperature); 
     if(event.temperature >= 32){
      Serial.println("Spray Garden55555555");
     digitalWrite(Relay2, LOW); // set LED OFF 
     led2.on();
     }else {
      digitalWrite(Relay2, HIGH); // set LED OFF
      led2.off();  
     }
  }
  dht.humidity().getEvent(&event);
  if (!isnan(event.relative_humidity)) {
    Blynk.virtualWrite(V11, event.relative_humidity);
    Serial.print("Humidity: ");
    Serial.println(event.relative_humidity);
  }
}
void Soilsensor() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0); 
Blynk.virtualWrite(V15, sensorValue);
if (sensorValue >=1 && sensorValue <=1000){
Serial.println(sensorValue);
    digitalWrite(Relay3, HIGH);
   led3.off();
  }
  else {
    Serial.println(sensorValue);
    digitalWrite(Relay3, LOW);
    led3.on();
  }
}
void reconnectBlynk() {
  if (!Blynk.connected()) {
    if(Blynk.connect()) {
      BLYNK_LOG("Reconnected");
    } else {
      BLYNK_LOG("Not reconnected");
    }
  }
}

void loop()
{
  if (Blynk.connected()) {
    Blynk.run();
  }
  timer.run();
}

/```D:\เธ?เธฒเธ? pc\dth1\SimpleTimeInputV\New_Text_Document.txt\New_Text_Document.txt.ino: In function ‘void setup()’:

New_Text_Document.txt:73: error: ‘activetoday’ was not declared in this scope

New_Text_Document.txt:74: error: ‘reconnectBlynk’ was not declared in this scope

New_Text_Document.txt:75: error: ‘sendUptime’ was not declared in this scope

New_Text_Document.txt:76: error: ‘Soilsensor’ was not declared in this scope

Multiple libraries were found for “Adafruit_Sensor.h”
Used: C:\Users\KNESS-SD\Documents\Arduino\libraries\Adafruit_Sensor-master
Not used: C:\Users\KNESS-SD\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0-rc2\libraries\Adafruit_Sensor-master
Multiple libraries were found for “DHT.h”
Used: C:\Users\KNESS-SD\Documents\Arduino\libraries\DHT-sensor-library-master
Not used: C:\Users\KNESS-SD\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0-rc2\libraries\DHT-sensor-library-master
Multiple libraries were found for “DHT_U.h”
Used: C:\Users\KNESS-SD\Documents\Arduino\libraries\Adafruit_DHT_Unified-master
Not used: C:\Users\KNESS-SD\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0-rc2\libraries\Adafruit_DHT_Unified-master
Multiple libraries were found for “BlynkSimpleEsp8266.h”
Used: C:\Users\KNESS-SD\Documents\Arduino\libraries\arduino_42666
Not used: C:\Users\KNESS-SD\Documents\Arduino\libraries\Blynk
Not used: C:\Users\KNESS-SD\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0-rc2\libraries\Blynk
Not used: C:\Users\KNESS-SD\Documents\Arduino\libraries\Blynk
Not used: C:\Users\KNESS-SD\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0-rc2\libraries\Blynk
Not used: C:\Users\KNESS-SD\Documents\Arduino\libraries\Blynk
Not used: C:\Users\KNESS-SD\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0-rc2\libraries\Blynk
Not used: C:\Users\KNESS-SD\Documents\Arduino\libraries\Blynk
Not used: C:\Users\KNESS-SD\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0-rc2\libraries\Blynk
Multiple libraries were found for “SimpleTimer.h”
Used: C:\Users\KNESS-SD\Documents\Arduino\libraries\SimpleTimer
Not used: C:\Users\KNESS-SD\Documents\Arduino\libraries\SimpleTimer-master
Not used: C:\Users\KNESS-SD\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0-rc2\libraries\SimpleTimer-master
Not used: C:\Users\KNESS-SD\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0-rc2\libraries\SimpleTimer
Multiple libraries were found for “TimeLib.h”
Used: C:\Users\KNESS-SD\Documents\Arduino\libraries\Time-master
Not used: C:\Users\KNESS-SD\Documents\Arduino\libraries\Time
Not used: C:\Users\KNESS-SD\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0-rc2\libraries\Time-master
Not used: C:\Users\KNESS-SD\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0-rc2\libraries\Time
Not used: C:\Users\KNESS-SD\Documents\Arduino\libraries\Time
Not used: C:\Users\KNESS-SD\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0-rc2\libraries\Time-master
Not used: C:\Users\KNESS-SD\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0-rc2\libraries\Time
Not used: C:\Users\KNESS-SD\Documents\Arduino\libraries\Time
Not used: C:\Users\KNESS-SD\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0-rc2\libraries\Time-master
Not used: C:\Users\KNESS-SD\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0-rc2\libraries\Time
Not used: C:\Users\KNESS-SD\Documents\Arduino\libraries\Time
Not used: C:\Users\KNESS-SD\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0-rc2\libraries\Time-master
Not used: C:\Users\KNESS-SD\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0-rc2\libraries\Time
exit status 1
‘activetoday’ was not declared in this scope```

do you have a question?

Yes. timer.setinterval error
when i hiden this code flashing compleat how to solve
Thank you.

[quote=“Kness_Anupong, post:1, topic:9241”]
timer.setInterval(2000L, activetoday); // check every minute if schedule should run today
timer.setInterval(30000L, reconnectBlynk); // check every 30s if still connected to server
timer.setInterval(2000L, sendUptime);
timer.setInterval(2000L, Soilsensor);

delete spaces between function name and ()
void activetoday(){
}

and you cannot run 4 timers with same name. U should do it like this:


timer.setInterval(2000L, activetoday);  // check every minute if schedule should run today 
  timer1.setInterval(30000L, reconnectBlynk);  // check every 30s if still connected to server 
  timer2.setInterval(2000L, sendUptime);
  timer3.setInterval(2000L, Soilsensor);

and then you will simply add to loop:

timer.run();
timer1.run();
timer2.run();
timer3.run();

it has to be something like:

byte timerActivetoday = timer.setInterval();
byte timerActivetomorrow = timer.setInterval();

And furthermore:

What goes too wrong is the fact the functions are not declared in advance of the setup loop.

E.g.

void activetoday();

void setup(){
}

void loop(){
}

That should get rid of the errors.

@Kness_Anupong I wrote most of the sketch that you are using and I have just compiled it.

You are using a bad version of the Arduino IDE. Upgrade to 1.6.12. I suggest you tidy up your libraries as lots of them are duplicated (this will not stop the sketch compiling though).

You have just one error in the code that prevents compilation:

#d efine DHTTYPE           DHT11

should be:

#define DHTTYPE           DHT11 

Spaces are fine.

You can run 10 timers with each instance of SimpleTimer.

This is only required if you wish to enable, disable or delete a timer. Not required for this sketch.

With a recent version of the IDE the functions can be anywhere.

Than it must be a Mac OS X bug or something, but I cannot get simpletimer to run in anyway, except if I declare the functions in advance (which happens to be good practice, but aside that).

Are you running 1.6.12 for OS X? Maybe they didn’t fix the “bug” introduced around 1.6.9 for Windows in OS X, but you would think they updated the code for all operating systems.

One may never know … I just leave it be and declare all my functions in advance. It’s kind of nice to do because you can add a short comment of what each function is doing anyway :slight_smile:

@Lichtsignaal I’m a very sloppy coder and drop my functions all over the place (this sketch perhaps being a prime example) :slight_smile:

I am interested to know if you are using 1.6.12 for OS X though because if the “bug” hasn’t been fixed then I might start to structure my sketches to work across all Operating Systems.

Officially it wasn’t a bug it was a change of direction for Arduino but it caused so many problems they reverted back to the free for all positioning of functions. Or at least they did for Windows.

Tbh I haven’t checked this properly because I’m too busy with a lot of things, but I’ll try now and get back to you.

Ok, here’s the deal. I declare my timers outside of the setup() function. I don’t know why, but it’s probably something autistic :wink:

If you do declare them outside the setup or loop they aren’t aware of existing functions, hence you will get an error compiling. If you put them in the setup(), it’s ok though.

I’m not sure why this is, but this way you can setup conditional timers which are or are not compiled during runtime (using ifdef). Anway, it’s not a “bug” as such, but probably intended behaviour, but I have no clue as to why.

1 Like

Well it doesn’t worked for me that way idk why…

Are you running the timers continuously or are you trying to stop and start them? For the former you shouldn’t have to use more than one instance of SimpleTimer unless you use more than 10 timers.

Aaaah, so there was a problem, good to know thanks!