(Solved) Help with coding notification delays

so I have moved everything to its own function and my question is do I use millis or a library timer?

Either will work, but a timer requires less coding.

Pete.

SimpleTimer (aka BlynkTimer) makes using the millis() timer simple.

1 Like

next question is are the timer calls placed in setup, loop, or in the functions I created?

All 3 and in definitions. See PUSH_DATA example for the elements of SimpleTimer.

Many users put the setInterval in setup() but it can go anywhere except loop() that should be kept to a maximum of 2 lines of code.

Depends on the type of timer, what you want it to do and whether it is a continuously running or temporarily running timer… checkout #12 here for some timer examples

Ok I have the sketch working with timers but my next question is will a delay within a function halt the sketch? I have a LCDdisplay function displaying Name then switching to Greenhouse Temp and then Time with a delay of 3 seconds between screens. If delay does halt sketch what is the best way to get around this? I went with the BlynkTimer

   void LCDdisplay()
{
// set up the LCD's number of columns and rows:
  // Print a message to the LCD.
    lcd.clear ();
    lcd.setCursor(0, 0);
    lcd.print ("Windy Ridge Farm");
    delay(3000);

  lcd.clear ();
  lcd.print("Greenhouse Temp:" );
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  lcd.print(GreenhouseTemp);
  lcd.print((char)223);
  lcd.print ("F");
  delay (3000);


DateTime now = rtc.now();

   char dateBuffer[12];

      //Print Second Line
    lcd.clear ();
    lcd.setCursor(0, 0);
    lcd.print ("Eastern Time:");
    lcd.setCursor(0, 1);
    sprintf(dateBuffer,"%02u:%02u ",now.hour(),now.minute());
    lcd.print(dateBuffer);
  
   delay (3000);

}

Yes, it halts everything

Fancy use of timers… look at the link I sent already in previous post (links are in green… at least on a PC)

Gunner Thanks for all your help!! I think I finally got it figured out I will post the updated code with no delay to get your thoughts on anything else i could do to make it better. The sketch compiles and everything is working as intended



/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************

  You’ll need:
   - Blynk App (download from AppStore or Google Play)
   - Arduino Ethernet board
   - Decide how to connect to Blynk
     (USB, Ethernet, Wi-Fi, Bluetooth, ...)

  There is a bunch of great example sketches included to show you how to get
  started. Think of them as LEGO bricks  and combine them as you wish.
  For example, take the Ethernet Shield sketch and combine it with the
  Servo example, or choose a USB sketch and add a code from SendData
  example.
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display
#define ONE_WIRE_BUS 7          //Arduino Mega Digital Pin 7
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
BlynkTimer timer;

RTC_DS3231 rtc;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "***********************";

#define W5100_CS  10
#define SDCARD_CS 4

/*-----( Declare Constants )-----*/
#define RELAY_ON 0
#define RELAY_OFF 1
/*-----( Declare objects )-----*/
/*-----( Declare Variables )-----*/
#define Relay_1  40  // Arduino Digital I/O pin number
#define Relay_2  41
#define Relay_3  42
#define Relay_4  43

int displayState = 0; 



int GreenhouseTemp;            // Greenhouse temperature in F

unsigned long previousMillis1 = 0;        // will store last time Display was updated
long interval = 3000; // m

//int timeState = LOW;

unsigned long previousMillis2 = 0;       // will store last time Time was updated
long OnTime = 100;           // milliseconds of on-time
long OffTime = 200;          // milliseconds of off-time

int sensorPin = 0;    // select the input pin for the potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor



void setup()
{
  
  // Debug console
  Serial.begin(9600);

  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card

  //Blynk.begin(auth);
  // You can also specify server:
  Blynk.begin(auth, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, IPAddress(192,168,10,222), 8442);

  // Send e-mail when your hardware gets connected to Blynk Server
  // Just put the recepient's "e-mail address", "Subject" and the "message body"
  Blynk.email("***********************", "Subject", "Greenhouse is online.");
  

     lcd.init();      // initialize the lcd 
     lcd.backlight(); // Turn on LCD Backlight
     
   // Uncomment to set time to PC time
  //rtc.begin();
  //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    //Serial.println("RTC lost power, lets set the time!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust (DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // Adjust Time on line Below 
    //rtc.adjust(DateTime(2017, 8, 23, 10, 36, 0));
  }

  Serial.begin(9600);

      //---( THEN set pins as outputs )----  
  pinMode(Relay_1, OUTPUT);   
  pinMode(Relay_2, OUTPUT);  
  pinMode(Relay_3, OUTPUT);  
  pinMode(Relay_4, OUTPUT);    
  //delay(1000); //Check that all relays are inactive at Reset
    
    
    //-------( Initialize Pins so relays are inactive at reset)----
  digitalWrite(Relay_1, RELAY_OFF);
  digitalWrite(Relay_2, RELAY_OFF);
  digitalWrite(Relay_3, RELAY_OFF);
  digitalWrite(Relay_4, RELAY_OFF); 



 timer.setInterval(1000L, Date_Time );
 timer.setInterval(1000L, sendTemps );
 timer.setInterval(3000L, LCDdisplay );
 timer.setInterval(1000L, Temp );  
 timer.setInterval(1000L, sendTime );
}


void loop()
{
    Blynk.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!

    timer.run();
}
  
   
   void sendTemps()
{
  sensors.requestTemperatures();                  // Polls the sensors.
  GreenhouseTemp = sensors.getTempFByIndex(0);   // Stores temperature. Change to getTempCByIndex(0) for celcius.
  Blynk.virtualWrite(V5, GreenhouseTemp);         // Send temperature to Blynk app virtual pin 1.
}

   void sendTime()
 {
  DateTime now = rtc.now();

   char dateBuffer[12];

   sprintf(dateBuffer,"%02u:%02u ",now.hour(),now.minute());
   Serial.println(dateBuffer);

   Blynk.virtualWrite(V1,dateBuffer);   

 }
   
   void LCDdisplay()
{

 // check to see if it's time to change the state of the LED
  unsigned long currentMillis = millis();
 
  if((displayState  == 0) && (currentMillis - previousMillis1 >= interval))
  {
    displayState = 1;  // Turn it off
    // set up the LCD's number of columns and rows:
  // Print a message to the LCD.
    lcd.clear ();
    lcd.setCursor(0, 0);
    lcd.print ("Windy Ridge Farm");
  }
  
     else if ((displayState == 1) && (currentMillis - previousMillis1 >= interval))
  {
    displayState = 2;  // turn it on
    previousMillis1 = currentMillis;   // Remember the time
    lcd.clear ();
    lcd.print("Greenhouse Temp:" );
    // set the cursor to column 0, line 1
    // (note: line 1 is the second row, since counting begins with 0):
    lcd.setCursor(0, 1);
    lcd.print(GreenhouseTemp);
    lcd.print((char)223);
    lcd.print ("F");
  }
  
  else if((displayState  == 2) && (currentMillis - previousMillis1 >= interval))
  {
    displayState = 0;  // Turn it off
     DateTime now = rtc.now();
      char dateBuffer[12];
      
      //Print Second Line
    lcd.clear ();
    lcd.setCursor(0, 0);
    lcd.print ("Eastern Time:");
    lcd.setCursor(0, 1);
    sprintf(dateBuffer,"%02u:%02u ",now.hour(),now.minute());
    lcd.print(dateBuffer);
    
  }

}


   void Date_Time ()
{
  DateTime now = rtc.now();

   char dateBuffer[12];

   sprintf(dateBuffer,"%02u-%02u-%04u ",now.month(),now.day(),now.year());
   Serial.print(dateBuffer);

   sprintf(dateBuffer,"%02u:%02u:%02u ",now.hour(),now.minute(),now.second());
   Serial.println(dateBuffer);
  

      
      // ********Watering Schedule***********

  int SchOn1 = (now.hour() == 8 & now.minute() == 30 & now.second() <= 10);
  int SchOff1= (now.hour() == 8 & now.minute() == 33 & now.second() <= 10);     
  int SchOn2 = (now.hour() == 17 & now.minute() == 30 & now.second() <= 10);
  int SchOff2= (now.hour() == 17 & now.minute() == 33 & now.second() <= 10);   
         
         
         
     unsigned long currentMillis = millis();
 
    if((SchOn1) && (currentMillis - previousMillis2 >= OnTime))
  {
    Blynk.notify("Plants Just Watered!");
    Blynk.email("***********************", "Subject: Greenhouse", "Plants Just Watered!");
    digitalWrite(Relay_2, RELAY_ON);// Turn the Watering Relay ON
  }
    
    else if ((SchOff1) && (currentMillis - previousMillis2 >= OffTime))
  {
    previousMillis2 = currentMillis;   // Remember the time
    digitalWrite(Relay_2, RELAY_OFF);// Turn the Watering Relay OFF
  }

    if((SchOn2) && (currentMillis - previousMillis2 >= OnTime))
  {
    Blynk.notify("Plants Just Watered!");
    Blynk.email("***********************", "Subject: Greenhouse", "Plants Just Watered!");
    digitalWrite(Relay_2, RELAY_ON);// Turn the Watering Relay ON
  }
  
   else if ((SchOff2) && (currentMillis - previousMillis1 >= OffTime))
  {
    previousMillis2 = currentMillis;   // Remember the time
    digitalWrite(Relay_2, RELAY_OFF);// Turn the Watering Relay OFF
  }
  
 }

 
 


void Temp()
{
     //***********Cooling Code***************

  if (GreenhouseTemp >= 85)
     { digitalWrite (Relay_1, RELAY_ON);}//Greenhouse Temp Control Fan ON Above 85 Degrees
   
  else if  ((GreenhouseTemp > 73)  && (GreenhouseTemp< 75))

     { digitalWrite (Relay_1, RELAY_OFF);}  //Greenhouse Temp Control  coolingFan OFF Below 75 Degrees
   
    bool tempflag = true;  
   
   if ((GreenhouseTemp >= 95)  && (tempflag == true))
  {
   Blynk.notify("GREENHOUSE OVERHEATING!!!");
   tempflag = false;
  }

if (GreenhouseTemp < 95)
{
  tempflag = true;
}



    //*************Heating Code****************
    
  if  ((GreenhouseTemp > 55)  && (GreenhouseTemp< 57))  
  { digitalWrite (Relay_3, RELAY_OFF);}    //Greenhouse Temp Control Heater Fan OFF Above 55 Degrees F

  if (GreenhouseTemp  <= 45.00) 
  { digitalWrite (Relay_3, RELAY_ON);}  //Greenhouse Temp Control Heater Fan ON Below 45 Degrees F


}


yes bl**dy backticks.

is that better?

Nope
3 backticks
sketch
3 backticks

i don’t understand what your talking about what is a backtick

If you don’t understand what anyone tells you ask big G.
https://www.google.com/search?q=what+is+a+backtick

Some forum mods kindly add the backticks for you, but I prefer to teach people how to fish rather than simply handing out the fish.

1 Like

well Im out of worms, where in the sketch are they needed and why what is their purpose im new at coding? Big G told me where they are on the keyboard but not what they are used for in a sketch

It’s not related to coding. It relates to posting certain things on forums that people can easily read.
Now you know what a backtick is, does my earlier post make any sense?

I prefer to labour the point so you and others become great anglers.

yes now is that better?

Just beautiful, now remove the second entry of Serial.begin(9600); in setup().

Coders don’t really want to look through your original posting of the sketch. Sure you would agree it’s much easier to understand now and specifically coders can easily grab a copy and put it in their IDE for debugging.
Without backticks copy and paste from this and other forum sites is not possible without us going through each line and formatting the sketch.

Ok thanks