Okay, start by moving all of the stuff out of void loop() except for:
Blynk.run();
timer.run();
Put the code you take out into it’s own function and call that function with a timer once every half a second. You could call it less frequently, but then there would be a delay between the switch being operated and the corresponding LED coming on. Half a second maximum delay won’t cause an issue.
I’m not sure why you’re checking if switch 1 and switch 2 have been operated here:
Surely it would be better to know when the second set of medication has been taken, regardless of whether the first has been taken?
Here’s a sample of what I think your new function should look like:
void function_to_check_switches()
{
switchStatus = digitalRead(switch1);
switchStatus2 = digitalRead(switch2);
if (switchStatus == HIGH)
{
// We do this stuff if switch1 is ON
if (!First_Notification_Flag)
{
// We only do this if notification email hasn't been sent yet (First_Notification_Flag == false)
First_Notification_Flag=true; // Now set the notification flag to true
digitalWrite(led,HIGH); // Turn the corresponding LED ON
FirstNotification(); // Send the first notification emmail
}
}
else
{
// We do this stuff if switch is OFF
digitalWrite(led,LOW);
if (First_Notification_Flag)
{
// We get here if switch1 was ON, but is now OFF, and a notification email has been sent
// You might want to set the First_Notification_Flag back to false
// And you might want to send a different email
// Of course, whne you refill the medication and switch the switches back to OFF then you'd get that notification email
}
}
// Repeat the logic above for Switch2 and 3
} // End of function_to_check_switches
You’ll need a variable, preferably a Boolean, called “First_Notification_Flag” declared at the top of your code so that it’s a Global variable. You’ll need equivalent ones for the other notifications.
I’ve not compiled this code, so ther may be typos or other errors!
Pete.