Doing a Task Between Two Times

I know my title probably makes no sense, but bear with me.

Arduino MKR1010 Board communicating with iOS

I am trying to do a task, between 4:00am and 10:00pm everyday. I thought it would be a simple if statement, but it seems I am wrong. Now I am beginning to think I need a while statement, with some way to break out of it. Below is the code I am talking about-

void calcMaxPressureDelta()
{
  while (hour() >= 4 && minute() == 0 && second() == 0)
   {
    pressureDelta = (prePsi - postPsi);
   
    if(pressureDelta > pressureDeltaMax)
    {
     pressureDeltaMax = pressureDelta;
    }  
  }
}
#include <Smoothed.h>
#include <Blynk.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
#include <TimeLib.h>
#include <WidgetRTC.h>


char ssid[] = "You";
char pass[] = "Wish";
int status = WL_IDLE_STATUS;
const byte wifiLedPin = 4;
const byte noWifiLedPin = 5;

const char auth[] = "Ask your dog";
BlynkTimer timer;
WidgetRTC rtc;
WidgetLCD lcd(V1);
WidgetLED ledGood(V2);
WidgetLED ledOrder(V3);
WidgetLED ledChange(V4);

Smoothed <float> sensorData;
Smoothed <float> sensorData2;
float currentPreSensorValue;
float currentPostSensorValue;
float smoothedPreSensorValueExp;
float smoothedPostSensorValueExp;
float preVoltage;
float postVoltage;
float prePsi;
float postPsi;
float pressureDelta;
float pressureDeltaMax;

float orderThreshold = 2.75;
float changeThreshold = 3.25;

enum FilterStatus {FilterOk, OrderFilter, ChangeFilter};

FilterStatus FilterCondition = FilterOk;

int i;
int beenNotified = 0;

byte preSensorPin = A1;
byte postSensorPin = A2;

BLYNK_CONNECTED() 
{
  // Synchronize time on connection
  rtc.begin();
}

void clockDisplay()
{
  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + " " + month() + " " + year();
  Serial.print("Current time: ");
  Serial.print(currentTime);
  Blynk.virtualWrite(V9, currentTime);
}

void setup()
{
 Serial.begin(9600);

  int attempts = 0;
  while (status != WL_CONNECTED && attempts < 6) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.print(ssid);
    status = WiFi.begin(ssid, pass);
    delay(10000);
    attempts++;
  }

  if (status != WL_CONNECTED) {
    Serial.print("Failed to connect to Wifi!");
    digitalWrite(noWifiLedPin, HIGH);
    delay(1000);
    digitalWrite(noWifiLedPin, LOW);
    delay(1000);
    while (true);
  }

  Serial.print("You're connected to the network");
  digitalWrite(wifiLedPin, HIGH);

  Blynk.begin(auth, ssid, pass);
  
  timer.setInterval(30000L, reconnectBlynk);
  timer.setInterval(1000L, readSensorValues);
  timer.setInterval(1000L, smoothSensorValues);
  timer.setInterval(1000L, checkFilter);
  timer.setInterval(1000L, filterNotify);
  timer.setInterval(1000L, calcMaxPressureDelta);
  timer.setInterval(1000L, blynkComm);
  timer.setInterval(5000L, clockDisplay);
  timer.setInterval(1000L, emailData);
  
  setSyncInterval(10 * 60); 

  pinMode(wifiLedPin, OUTPUT);
  pinMode(noWifiLedPin, OUTPUT);
  pinMode(preSensorPin, INPUT);
  pinMode(postSensorPin, INPUT);
  
  sensorData.begin(SMOOTHED_EXPONENTIAL, 10);
  sensorData2.begin(SMOOTHED_EXPONENTIAL, 10);
}

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

void readSensorValues()
{
  preVoltage =  (5.0 / 1023.0) * smoothedPreSensorValueExp;
  prePsi = (preVoltage - 0.5) * (100.0) / (4.5 - 0.5);

  postVoltage =  (5.0 / 1023.0) * smoothedPostSensorValueExp;
  postPsi = (postVoltage - 0.5) * (100.0) / (4.5 - 0.5);
}

void smoothSensorValues()
{
  // Read the value from the sensor
  currentPreSensorValue = analogRead(preSensorPin);
  currentPostSensorValue = analogRead(postSensorPin);

  //Add the new value to both sensor value stores
  sensorData.add(currentPreSensorValue);
  sensorData2.add(currentPostSensorValue);

  // Get the smoothed values
  smoothedPreSensorValueExp = sensorData.get();
  smoothedPostSensorValueExp = sensorData2.get(); 
}

void checkFilter()
{
  switch (FilterCondition)
  {
    case FilterOk:
      if (prePsi - postPsi > orderThreshold)      
      {
        FilterCondition = OrderFilter;
      }
      break;
    case OrderFilter:
      if (prePsi - postPsi > changeThreshold)        
      {
        FilterCondition = ChangeFilter;
      }
      break;
    case ChangeFilter:  
      break;
  }
}

void filterNotify()
{
  switch (FilterCondition)
  {
    case FilterOk:
      Serial.println("Filter ok");
      ledGood.on();
      ledOrder.off();
      ledChange.off();
      break;
    case OrderFilter:
      Serial.println("Order filter");
      if(beenNotified == 0){
      Blynk.email("mrcparker90@gmail.com", "FilterLynk Condition", "It is time to order a fitler");
      beenNotified++;
      }
      ledGood.off();
      ledOrder.on();
      break;
    case ChangeFilter:
      Serial.println("Replace filter");
      ledOrder.off();
      ledChange.on();
      break;
  }
}

void calcMaxPressureDelta()
{
  while (hour() >= 4 && minute() == 0 && second() == 0)
   {
    pressureDelta = (prePsi - postPsi);
   
    if(pressureDelta > pressureDeltaMax)
    {
     pressureDeltaMax = pressureDelta;
    }  
  }
}

void blynkComm()
{
  Blynk.virtualWrite(V5, prePsi);
  Blynk.virtualWrite(V6, postPsi);
  Blynk.virtualWrite(V7, pressureDelta);
  Blynk.virtualWrite(V8, pressureDeltaMax);
}

void emailData()
{
  if (hour() == 20 && minute() == 01 && second() == 0){
    String str = "Today's max delta pressure: " + String(pressureDeltaMax, 3);
    Blynk.email("mrcparker90@gmail.com", "FilterLynk Condition", str);
    pressureDeltaMax = 0;
  }
}

void reconnectBlynk() {
  if (!Blynk.connected()) {
    if (Blynk.connect()) {
      BLYNK_LOG("Reconnected");
    } else {
      BLYNK_LOG("Not reconnected");
    }
  }
}
  

Anybody willing to help me out?

This evaluates to true for just one second every hour from 04:00 onwards.
You are calling this with a timer that runs once every second, but which is competing for processor time with 6 other timers that are also being called once every second.
What are the chances of this working properly?

Personally, I’d avoid while loops, as they can easily block code execution.

Pete.

Pete, I understand that the code I posted would not complete the task I am trying to do, I just was trying to get the ball rolling. So, I would say the chances of this working properly are pretty slim.

Thank you for the advice on avoiding while loops. Are you suggesting I use a if statement?

That would be my preference.

Pete.

I tried this already, and doesn’t seem to work. Meaning during the hours between 4am and 10pm, my delta pressure and max delta pressure is not being updated.

void calcMaxPressureDelta()
{
if (hour() >= 4 && minute() == 0 && second() == 0 && hour() <= 20 && minute() == 0 && second() == 0)
{
pressureDelta = (prePsi - postPsi);

if(pressureDelta > pressureDeltaMax)
{
 pressureDeltaMax = pressureDelta;
}  

}
}

if you think through your if statement, then you may see the issue.

When will that statement be true? Only at the time of 4:00:00, 5:00:00, 6:00:00 … 20:00:00. So it will only update every hour. If you get rid of the minute() and second() stuff it may work like you want.
Also, 10PM would be 22:00. and if you want it to stop at 10:00PM, you would want to stop when it reached 22:00.

void calcMaxPressureDelta()
{
if ((hour() >= 4) && (hour() < 22)) //4:00:00AM - 9:59:59PM
{
pressureDelta = (prePsi - postPsi);

if(pressureDelta > pressureDeltaMax)
{
 pressureDeltaMax = pressureDelta;
}  

}
}

Based on suggestion of @Toro_Blanco, I suggest that you try the following code, based on yours, but with some modifications:

  1. Reduce numbers of timers and made them staggering
  2. Merge related functions
#include <Smoothed.h>
#include <Blynk.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
#include <TimeLib.h>
#include <WidgetRTC.h>


char ssid[] = "You";
char pass[] = "Wish";
int status = WL_IDLE_STATUS;
const byte wifiLedPin = 4;
const byte noWifiLedPin = 5;

const char auth[] = "Ask your dog";
BlynkTimer timer;
WidgetRTC rtc;
WidgetLCD lcd(V1);
WidgetLED ledGood(V2);
WidgetLED ledOrder(V3);
WidgetLED ledChange(V4);

Smoothed <float> sensorData;
Smoothed <float> sensorData2;
float currentPreSensorValue;
float currentPostSensorValue;
float smoothedPreSensorValueExp;
float smoothedPostSensorValueExp;
float preVoltage;
float postVoltage;
float prePsi;
float postPsi;
float pressureDelta;
float pressureDeltaMax;

float orderThreshold = 2.75;
float changeThreshold = 3.25;

enum FilterStatus {FilterOk, OrderFilter, ChangeFilter};

FilterStatus FilterCondition = FilterOk;

int i;
bool beenNotified = false;
bool beenEmailed = false;

byte preSensorPin = A1;
byte postSensorPin = A2;

BLYNK_CONNECTED() 
{
  // Synchronize time on connection
  rtc.begin();
}

void clockDisplay()
{
  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + " " + month() + " " + year();
  Serial.print("Current time: ");
  Serial.print(currentTime);
  Blynk.virtualWrite(V9, currentTime);
}

void setup()
{
  Serial.begin(9600);
 
  pinMode(wifiLedPin, OUTPUT);
  pinMode(noWifiLedPin, OUTPUT);
  pinMode(preSensorPin, INPUT);
  pinMode(postSensorPin, INPUT);
  
  sensorData.begin(SMOOTHED_EXPONENTIAL, 10);
  sensorData2.begin(SMOOTHED_EXPONENTIAL, 10); 

  int attempts = 0;
  while (status != WL_CONNECTED && attempts < 6) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.print(ssid);
    status = WiFi.begin(ssid, pass);
    delay(10000);
    attempts++;
  }

  if (status != WL_CONNECTED) {
    Serial.print("Failed to connect to Wifi!");
    digitalWrite(noWifiLedPin, HIGH);
    delay(1000);
    digitalWrite(noWifiLedPin, LOW);
    delay(1000);
    while (true);
  }

  Serial.print("You're connected to the network");
  digitalWrite(wifiLedPin, HIGH);

  Blynk.begin(auth, ssid, pass);
  
  timer.setInterval(30100L, reconnectBlynk);
  timer.setInterval(1011L, readSensorValues);
  // Check every 60secs
  timer.setInterval(60000L, checkFilter);
  
  timer.setInterval(1022L, blynkComm);
  timer.setInterval(5000L, clockDisplay);

  
  setSyncInterval(10 * 60); 

}

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

void readSensorValues()
{
  smoothSensorValues();
 
  preVoltage =  (5.0 / 1023.0) * smoothedPreSensorValueExp;
  prePsi = (preVoltage - 0.5) * (100.0) / (4.5 - 0.5);

  postVoltage =  (5.0 / 1023.0) * smoothedPostSensorValueExp;
  postPsi = (postVoltage - 0.5) * (100.0) / (4.5 - 0.5);
  
  calcMaxPressureDelta();
  
  emailData();
}

void checkFilter()
{
  switch (FilterCondition)
  {
    case FilterOk:
      if (prePsi - postPsi > orderThreshold)      
      {
        FilterCondition = OrderFilter;
      }
      else
      {
      Serial.println("Filter ok");
      ledGood.on();
      ledOrder.off();
      ledChange.off();
      } 
      break;
    case OrderFilter:
      if (prePsi - postPsi > changeThreshold)        
      {
        FilterCondition = ChangeFilter;
      }
      else
      {
        Serial.println("Order filter");

        if(!beenNotified)
        {
          Blynk.email("mrcparker90@gmail.com", "FilterLynk Condition", "It is time to order a fitler");
          beenNotified = true;
        }

        ledGood.off();
        ledOrder.on();
      }  
      break;
    case ChangeFilter:
      Serial.println("Replace filter");
      ledOrder.off();
      ledChange.on();    
      break;
  }
}
  
void calcMaxPressureDelta()
{
  if ((hour() >= 4) && (hour() < 22)) //4:00:00AM - 9:59:59PM
  {
    pressureDelta = (prePsi - postPsi);

    if(pressureDelta > pressureDeltaMax)
    {
     pressureDeltaMax = pressureDelta;
    }
    
    //Reset new cycle
    if (beenEmailed)
      beenEmailed = false;
  }
}
  

void blynkComm()
{
  Blynk.virtualWrite(V5, prePsi);
  Blynk.virtualWrite(V6, postPsi);
  Blynk.virtualWrite(V7, pressureDelta);
  Blynk.virtualWrite(V8, pressureDeltaMax);
}

void emailData()
{
  if (hour() > 22 && !beenEmailed)
  {
    String str = "Today's max delta pressure: " + String(pressureDeltaMax, 3);
    Blynk.email("mrcparker90@gmail.com", "FilterLynk Condition", str);
    pressureDeltaMax = 0;
    beenEmailed = true;
  }
}

void reconnectBlynk() {
  if (!Blynk.connected()) {
    if (Blynk.connect()) {
      BLYNK_LOG("Reconnected");
    } else {
      BLYNK_LOG("Not reconnected");
    }
  }
}


Good luck

Sorry to let func. smoothSensorValues() out

void smoothSensorValues()
{
  // Read the value from the sensor
  currentPreSensorValue = analogRead(preSensorPin);
  currentPostSensorValue = analogRead(postSensorPin);

  //Add the new value to both sensor value stores
  sensorData.add(currentPreSensorValue);
  sensorData2.add(currentPostSensorValue);

  // Get the smoothed values
  smoothedPreSensorValueExp = sensorData.get();
  smoothedPostSensorValueExp = sensorData2.get(); 
}