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?