sending message

Submitted by viresh on

how to send message once (not multiple times) when the switch is on or off

If you want to send a sms once, when a condition is reached like button is pressed or a sensor is detected,then define a variable as set bit like flag=0 initially.when condition satisfies,then in the message loop set the flag bit to 1.A sample body of program is shown below:

int flag=0;

if(button= =high)

{

if(flag= =0)

{

sendsms();

}

}

else

{

flag=0;

}

void sendsms()

{

flag=1;

}

 

Hope it helps!!!

  Joined August 22, 2019      125
Thursday at 12:29 PM

Using a flag bit is a good idea like parida sugested. But I can share a more optimised way of doing it

 

boolean flag = FALSE;

void loop(){

if (button == HIGH && flag == FALSE)

{

Serial.println ("Send the ON message");

flag = TRUE; //prevent this condition to get executed again until button is released 

delay (100); //delay to avoid switch debounce problem 

}

if (button == LOW && flag == TRUE)

{

Serial.println ("Send the OFF message");

flag = FALSE; //prevent this condition to get executed again until button is pressed again 

delay (100); //delay to avoid switch debounce problem 

}

 

}

 

You can think of flag bit as a software switch. this switch can be controlled with your program. You can make it change its state everytime the condition is executed once. Also consider switch debounce problem when you are working with buttons 

 

 

  Joined April 17, 2018      120
Tuesday at 07:57 AM

Well done Hiro hamada,,agree with you,,as it is the best and excellent approach with tested and verified by hardy

  Joined November 19, 2019      4
Tuesday at 08:44 PM