sending message

viresh's picture

Offline

Joined: Apr 29, 2019

Replies: 6
sending message
April 29, 2019 - 4:24pm

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

Debasis Parida's picture

Offline

Joined: Aug 22, 2019

Replies: 124
Sending a message once
November 20, 2019 - 11:19pm

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!!!

Hiro_Hamada's picture

Offline

Joined: Apr 17, 2018

Replies: 120
sending message on trigger
November 21, 2019 - 2:18pm

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 

 

 

Bilal's picture

Offline

Joined: Nov 19, 2019

Replies: 4
Well done Hiro hamada,,agree
November 21, 2019 - 5:22pm

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