how to send message once (not multiple times) when the switch is on or off
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
Well done Hiro hamada,,agree with you,,as it is the best and excellent approach with tested and verified by hardy
Debasis Parida
PermalinkIf 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!!!
- Log in or register to post comments
Joined August 22, 2019 125Thursday at 12:29 PM