SimpleTimer chained commands

Greetings:
I'm working on a project that would turn on a LED for ten seconds every ten minutes. I have no problem with turning on the LED for 10 seconds using the timer.setIntveral command. But I'm unsure of where to put the second command to do it every ten minutes. SHould I use the timer.setInterval command again, or something different . Where in the sketch should the second command go? Would it be another void loop? I've looked around for an example, but not been able to find one.

No idea!

Especially since we don't have your code and I for one have no idea what this "SimpleTimer" library is. :upside_down_face:

Mind you, I would not use it; I would be writing the necessary code using millis() referencing which seems far more appropriate.

Actually, that makes no sense but - never mind. :grin:

Thanks Paul, for taking the time to reply. I figured I might have to put up some code. I’m actually trying to adopt a remote water tank sensing program I wrote (with much help from this form). I need to cut down on how often it accesses the internet, so I figure if I can modify my program to access the internet every ten minutes for a minute, (sending up to Blynk) that would be acceptable. I figure it would be best to try and figure out the timers using a simplified sketch with an LED. Once I figure out that I’ll adopt the program to the sensor program.
I was hoping that since it doesn’t seem that exotic – turning on a device for a predetermined duration at a repeating time, someone might point me to an existing example I could modify. If not, I’ll take a bash at it and put my code up for comment.
thanks again.

Jeff

The 'Simple Timer' library is based on millis(). It's only a way to handle millis() more easy.
timer.setIntervall() sets the time to call a function repeatedly at fixed intervalls, so it seems very approriate for this usecase.

please see the example
"BlinkWithoutDelay"

The current rhythm is 1 second. You only have to change the interval depending if you are in on or off state.

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
      interval = 10 * 1000UL;
    } else {
      ledState = LOW;
      interval = 10 * 60 * 1000UL;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

no lib necessary, only the knowledge how to use millis().

Thanks so much for that. I’ll give it a shot later today.

Hi, Tried out the program with you loop… I’m getting an error for the line:
interval = 10 * 1000UL;
The error is “assignment of read-only variable ‘interval’
Any suggestions on how to rectify this?
Thanks
Jeff

please post your full program in code tags.

but I assume you have to change the const into a variable as it is not a constant any more.
As interval will never be negative, you can use unsigned instead:

// constants won't change:
//const long interval = 1000;           // interval at which to blink (milliseconds)
unsigned long interval = 1000;           // interval at which to blink (milliseconds)

Of couse! That does it.

Thanks.

Jeff

Perhaps, you can answer a question for me:
If the sole purpose of this exercise is to cut down sending data via the internet – in order to minimize internet use, would this not be an exception of the rule NOT to use a delay command instead?

Jeff

I'm not aware of the existence of a rule like "don't use delay".
If you can do it with delay, do it.
Howevery I'm using millis() more often than delay().

I guess I’ll try it with two or three approaches and see which works best – or more likely which I can get to work. Thanks again.

Best

Jeff

Hi:
I think I got it working! Which is quite amazing for me to get it going this early. Couldn't have done it without an assist. The sketch now connects to Blynk for about 10secs gets a reading then disconnects for 3 minutes (will increase to 10 or more later).
If anyone sees anything that might improve it, please let me know. Otherwise feel free to use it.

/*
 * For this example you'll need the following library:
 *  * 
 * 1) Arduino-LiquidCrystal-I2C-library: https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library
 *
 * 2) Blynk Library: https://github.com/blynkkk/blynk-library
 *
 * Conncetions:
 * D1 -> SCL of I2C LCD 
 * D2 -> SDA of I2C LCD
 * D3 -> Out of DHT11/22
 *
 */
#include <LoRa.h>
#define BLYNK_PRINT Serial  
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

#define I2C_SDA D3 //redefining I2C pins as D1 is needed by LoRa unit
#define I2C_SCL D4

#define ss 15
#define rst 16
#define dio0 2


unsigned long previousMillis = 0;        // will store last time LED was updated
unsigned long interval = 1000;          





const int ledPin = D1; //warning light that water has dropped beyone preset level

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; //Enter the Auth code which was send by Blink

char ssid[] = "xxxxxxxxxxxx;  //Enter your WIFI Name
char pass[] = "xxxxxxxxxx";  //Enter your WIFI Password


void setup()
{
pinMode(ledPin,OUTPUT);
Wire.pins(D3, D4);
  
  //Serial.begin(9600);

lcd.begin();   // iInit the LCD for 16 chars 2 lines
  lcd.backlight();   // Turn on the backligt (try lcd.noBaklight() to turn it off)
  delay (1000);
  Blynk.begin(auth, ssid, pass);

   Serial.println("LoRa Sender");
 LoRa.setPins(ss, rst, dio0);
  if (!LoRa.begin(433E6)) 
  {
    Serial.println("Starting LoRa failed!");
    delay(100);
    while (1);
  }
  Serial.println("LoRa Started");
  lcd.clear();
  lcd.print("Waiting");
   LoRa.setSpreadingFactor(10);
 LoRa.setSignalBandwidth(62.5E3);
LoRa.crc(); 
}

void loop()
{
 // try to parse packet
  int packetSize = LoRa.parsePacket();
  int targetDistance = LoRa.parseInt();

  if (packetSize)
  {  
    //Serial.print("distance is ");
    // Serial.print(targetDistance);
   // Serial.print(" inches ");
   // Serial.println(" ");
    
 
lcd.setCursor(0,0); //First line
lcd.print("water is down");
lcd.setCursor(0,1); //Second line
lcd.print((targetDistance - 4));
lcd.print(" inches");



unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
Blynk.connect();    
 Blynk.run(); // Initiates Blynk
 Blynk.virtualWrite(V1,(targetDistance - 4)); 
 interval = 10 * 1000UL;
 } 
 else {
  Blynk.disconnect();
 interval = 10 * 60 * 1000UL;
 }
 
delay(1000);

 {
      if ((targetDistance - 4) <= 25 )
      {
        digitalWrite(ledPin,LOW);
      }
      else 
      {
        digitalWrite(ledPin,HIGH);
      }
 }
  }
}

Edit your WiFi credentials so we don't see them.

Ouch! Thanks for that!!!

jeff