Do it once now, then wait XXX to do it again...

I have a battery voltage/temperature monitor I am building on a mega with a W5100 ethernet shield. I have an odd issue I can’t see how to solve.

It reads the sensor values very 2s, displays them on an LCD. no problem. I want it to send me an email as soon as it sees the readings go above or below a value. This I can get it to to. This issue is that it sends an email every 2s as long as the trigger condition exists. (UGH! lol)

What I want it do do is send an email as soon as it sees the trigger value, but then wait a specified time before sending another email (assuming the trigger condition persists). I so far have it to wait say 5 mins before sending so that it only sends once every 5 mins, but not when it first sees the trigger value.

Any suggestions? Portion of my sloppy code below: :wink:

long previousMillis = 0; // for email timing
long interval = 300000; // for email timing



void setup() {

 lcd.init();
 lcd.backlight();
 lcd.clear();
 lcd.setCursor(6, 1);
 lcd.print("Startup");
 delay(500);
 lcd.clear();
 dht.begin();
 
 //Init Serial USB
 Serial.begin(9600);
 Serial.println(F("Initialize System"));
 lcd.setCursor(0, 1);
 lcd.print("Initializing System");
// Init W5100
 Ethernet.begin(mac, ip);
 while (!Ethernet.begin(mac)) {
   Serial.println(F("failed. Retrying in 5 seconds."));
   lcd.setCursor(0, 1);
   lcd.print(F("failed. Retrying"));
   delay(5000);
   Serial.print(F("Starting W5100..."));
   lcd.setCursor(0, 1);
   lcd.print("Starting W5100");
 }

 Serial.println(F("W5100 initialized"));
 lcd.clear();
 //lcd.setCursor(0, 0);
 //lcd.print("IP: ");
 //lcd.print(Ethernet.localIP());
 Serial.print(F("IP Address: "));
 Serial.println(Ethernet.localIP());
 Serial.println(F("System Online."));
 
}

    void loop()
    {
   unsigned long currentMillis = millis();
   int volta = analogRead(A8);// read the input
   int voltb = analogRead(A9);// read the input
   double voltagea = map(volta,0,1023, 0, 2500) + offseta;// map 0-1023 to 0-2500 and add correction offset
   voltagea /=100;// divide by 100 to get the decimal values
   double voltageb = map(voltb,0,1023, 0, 2500) + offsetb;// map 0-1023 to 0-2500 and add correction offset
   voltageb /=100;// divide by 100 to get the decimal values
   float h = dht.readHumidity();
   float t = dht.readTemperature();
   // check if returns are valid, if they are NaN (not a number) then something went wrong!
   if (isnan(t) || isnan(h)) 
   {
 
   lcd.setCursor(0, 1);
   lcd.print("Sensor Read Failed  ");
   lcd.setCursor(0, 2);
   lcd.print("                    ");
   lcd.setCursor(0, 3);
   lcd.print("                    ");
   //wdt_reset();
 } 
 else 
 {
   lcd.clear();
   lcd.setCursor(0, 0);
   lcd.print("Arduino Watchdog OK ");
   lcd.setCursor(0, 1);
   lcd.print("Temperature:  ");
   lcd.print(t -.4);
   lcd.print("C");
   lcd.setCursor(0, 2);
   lcd.print("Battery Bank: ");
   lcd.print(voltagea);
   lcd.print("V");
   lcd.setCursor(0, 3);
   lcd.print("5V Supply:     ");
   lcd.print(voltageb);
   lcd.print("V");
   delay(2000);
   //wdt_reset();
 }

 if(currentMillis - previousMillis > interval)
 {
  previousMillis = currentMillis;
  if (t > 20)
  {
    sendmessage();
    lcd.noBacklight();
    delay(200);
    lcd.backlight();
    delay(200);
    lcd.noBacklight();
    delay(200);
    lcd.backlight();
    delay(200);
    lcd.noBacklight();
    delay(200);
    lcd.backlight();
    delay(200);  
  }
  }
  else {
    lcd.backlight();
    }
 } 
    
     
void sendmessage()

Make a flag variable. If the threshold is exceeded but the flag is asserted then no email.
The flag is cleared when the condition goes back below the threshold.

This is where studying the StateChangeDetection example would be relevant. You need a
variable to represent one of three states - being normal, too large or too small.

When setting the state check to see if the state is different from the last time - only then
consider sending a message.

That makes way more sense than just looking at the values every "internal"..

I will look into that, thanks!