Void loop sending same message over and over

hi everyone, my name is mohammad im 31 years old had some C language classes when i was at high school, now we have alot of power cuts in my country and im using a generator a friend adviced that i use esp8266 to detect if its government electricity or my solar/generator electricity, so far i have managed to define pins connect to wifi etc...... but when i write the code in void loop so that whenever a pin's status change it sends me a message, it keeps on sending me the same message as long as the pin is in it's current state- if(lets say pin1=1){
bot.sendMessage(CHAT_ID, "EDL IS ON", "");
Serial.println("EDL IS ON");

how can i set it to only send 1 message whenever that pin for example state changes. thanks in advance and have a good day

Keep a 2 step history of the state so you can see when it changes.
So the code would be:
If current_state is different from last_state then send a message.

2 Likes

something like this:

int current_state;
int previous_state;

:
:
// code to determine current state
:
:

if(  current_state != previous_state )
{
    // State has changed - send the message

   previous_state = current_state ;
}

For readability, you could use an enum for the states ...

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.