Completing a Task Between Times

I know my title is probably confusing, but I am looking to complete a task only between the time of 4:00am to 10:00pm. I think I need an if statement, or maybe a couple to be able to do this. It looks silly, but I tried-

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

As far as I can tell it is not working. Any suggestions on how to do this more better? Thanks!

MKR1010 board communicating with Blynk app. Full code below.

#include <Smoothed.h>
#include <Blynk.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
#include <TimeLib.h>
#include <WidgetRTC.h>


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

const char auth[] = "------";
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()
{
  if (hour() >= 4 && minute() == 0 && second() == 0 && hour() <= 20 && 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");
    }
  }
}

The statement looks alright assuming it is guaranteed to be called at least once every second otherwise it may miss the hour change over.
It might be cleaner though to do the activity on an hour change over. Something like:

static int lastHour ; // static initialized only once
if ( hour() >= 4 && hour() <= 20 && lastHour != hour() ) {
// do stuff
}
lastHour = hour() ;

timer.setInterval(1000L, calcMaxPressureDelta);

It appears you have set a timer to call calcMaxPressureDelta() every second, but the function itself will only calculate the value once on each hour between 4AM and 10PM.

How many maxPressureDelta readings do you actually want to take?

10pm is 22.00hrs btw.

Convert everything to seconds. Then it becomes easy

#define START_TIME_SEC some number
#define END_TIME_SEC some other number

if (timeSec>=START_TIME_SEC && timeSec<=END_TIME_SEC) {
do your stuff
}

-jim lee

jimLee:
Convert everything to seconds. Then it becomes easy

This. But I'd prefer to see const rather than #define.

cattledog:

timer.setInterval(1000L, calcMaxPressureDelta);

It appears you have set a timer to call calcMaxPressureDelta() every second, but the function itself will only calculate the value once on each hour between 4AM and 10PM.

How many maxPressureDelta readings do you actually want to take?

Yeah that's not want I want, I want the maxPressureDelta to be updated as many times as needed through the time of 4:00am to 10:00pm. Only reason it can't be down all the time is there are tanks being flushed out during the middle of the night, skewing my data.

jimLee:
Convert everything to seconds. Then it becomes easy

#define START_TIME_SEC some number
#define END_TIME_SEC some other number

if (timeSec>=START_TIME_SEC && timeSec<=END_TIME_SEC) {
do your stuff
}

-jim lee

This idea seems like a real winner. If I am understanding you right-

void calcMaxPressureDelta()
{
  const startTimeSec = 14400;

  const endTimeSec = 79200;

  if (second() >= startTimeSec && second() <= endTimeSec)
   {
    pressureDelta = (prePsi - postPsi);
   
    if(pressureDelta > pressureDeltaMax)
    {
     pressureDeltaMax = pressureDelta;
    }  
  }
}

What do we say? Do we love it? Hate it? Don’t care?

second() is going to give you 0-59. You need to calculate seconds from midnight. Also, those consts need a type, unsigned long for preference.

wildbill:
second() is going to give you 0-59. You need to calculate seconds from midnight. Also, those consts need a type, unsigned long for preference.

Dang it, you are right, of course that won't work.

What do we say? Do we love it? Hate it? Don’t care?

In the present sketch you do not calculate seconds since midnight. Seconds is only a value from 0-59.

For actions which run between hour values, I do not see any difference between calculating and using seconds since midnight and the hours values.

if ( hour() >= 4 && hour() < 20 ) {
  // do stuff
}

For stop and start times not on the hour, then the seconds or minutes since midnight make sense.

void calcMaxPressureDelta()

{
  const startTimeSec = 14400;

const endTimeSec = 79200;

Rather than doing the math youself, make the computer do as much of the work for you as you can make it.

#define MINUTES_TO_SECONDS(x) ((x)*UINT32_C(60))
#define HOURS_TO_MINUTES(x) ((x)*UINT32_C(60))

#define HOURS_TO_SECONDS(x) (MINUTES_TO_SECONDS(HOURS_TO_MINUTES(x)))

const unsigned long startTimeSec = HOURS_TO_SECONDS(4);  // 4:00 am
const unsigned long endTimeSec = HOURS_TO_SECONDS(10+12); // 10:00 pm

A particular kind of laziness like this can be a programmer's greatest asset. Instead of just dumping in the magic number, making the computer perform the calculation for you can not only help you avoid arithmetic mistakes, but can make your intentions clearer. 61200 won't mean much to the average person, but HOURS_TO_SECONDS(17) is much more obviously referring to 5:00 pm.

And you may need to take into account that the running task may take less then a second to complete and you may not want your task to run multiple times in the same second.

This is all great information, thanks everyone. I got the code working a few hours ago. I don’t know what I ever did to deserve all of you and your awesome help! Thanks again.

 if (hour() >= 4 && minute() == 0 && second() == 0 && hour() <= 20 && minute() == 0 && second() == 0)

Don’t do that. Do this:

  long secondnow = hour() * 3600L + minute() * 60 + second();
  if(secondnow >= 4 * 3600L && secondnow < 22 * 3600L) {
    // it's between 4am and 10PM.
  }