# Help with progaming a push button and sim800l

**URL:** <https://forum.arduino.cc/t/help-with-progaming-a-push-button-and-sim800l/1044903>\
**Category:** Programming\
**Created:** [October 22, 2022, 6:49pm UTC](https://forum.arduino.cc/t/help-with-progaming-a-push-button-and-sim800l/1044903 "2022-10-22T18:49:16Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![josecarlossilva](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@josecarlossilva](https://forum.arduino.cc/u/josecarlossilva)\
**Post date:** [October 22, 2022, 6:49pm UTC](https://forum.arduino.cc/t/help-with-progaming-a-push-button-and-sim800l/1044903/1 "2022-10-22T18:49:16Z")

</div>

hi  
im trying to modify a code i get to use it for one porpuse  
so the idea is a push botton is (nc) in stand bye and no message is send is button is pressed mensage is send this part is rworking the problem is that if the button stays pressed and it will stay a lot of mensages is send.  
the type of solucion i thick works  
button pressed send only 1 comand until the button is off and on again  
can someone help me with the code

code

```cpp
#include <SoftwareSerial.h>

SoftwareSerial sim800l(2, 3); // RX,TX for Arduino and for the module it's TXD RXD, they should be inverted

#define button1 7 //Button pin, on the other pin it's wired with GND

bool button_State; //Button state

void setup()
{

 pinMode(button1, INPUT_PULLUP); //The button is always on HIGH level, when pressed it goes LOW
 sim800l.begin(9600); //Module baude rate, this is on max, it depends on the version
 Serial.begin(9600);   
 delay(1000);
}

void loop()
{
 

 button_State = digitalRead(button1); //We are constantly reading the button State

 if (button_State == HIGH) { //And if it's pressed
 
   Serial.println("botao on"); //Shows this message on the serial monitor
   delay(200); //Small delay to avoid detecting the button press many times

SendSMS(); //And this function is called

}

 if (sim800l.available()){ //Displays on the serial monitor if there's a communication from the module
   Serial.write(sim800l.read()); 
 }
}

void SendSMS()
{
 Serial.println("Sending SMS...");               
 sim800l.print("AT+CMGF=1\r"); //Set the module to SMS mode
 delay(100);
 sim800l.print("AT+CMGS=\",,,,,\"\r"); //phone number
 delay(500);
 sim800l.print("1"); //mensage send
 delay(500);
 sim800l.print((char)26);
 delay(500);
 sim800l.println();
 Serial.println("Text Sent.");
 delay(500);

}

```

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [October 22, 2022, 6:59pm UTC](https://forum.arduino.cc/t/help-with-progaming-a-push-button-and-sim800l/1044903/2 "2022-10-22T18:59:32Z")

</div>

> [@josecarlossilva](#):
>
> ```arduino
> pinMode(button1, INPUT_PULLUP); //The button is always on HIGH level, when pressed it goes LOW
> 
> button_State = digitalRead(button1); //We are constantly reading the button State
> if (button_State == HIGH) { //And if it's pressed
> 
> ```

You should be looking for 'LOW'

---

<div class="post-metadata">

**Author:** ![josecarlossilva](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@josecarlossilva](https://forum.arduino.cc/u/josecarlossilva)\
**Post date:** [October 22, 2022, 8:43pm UTC](https://forum.arduino.cc/t/help-with-progaming-a-push-button-and-sim800l/1044903/3 "2022-10-22T20:43:09Z")

</div>

yes but how to if continuos pressed not keeping sending messages  
the button shoudl be able to be pressed , AND NOT RELEASED and only send 1 mensage , if release and pressed again send 1 mensage again (the same one)

---

<div class="post-metadata">

**Author:** ![red\_car](https://avatars.discourse-cdn.com/v4/letter/r/b77776/32.png) [@red\_car](https://forum.arduino.cc/u/red_car)\
**Post date:** [October 22, 2022, 8:59pm UTC](https://forum.arduino.cc/t/help-with-progaming-a-push-button-and-sim800l/1044903/4 "2022-10-22T20:59:49Z")

</div>

You need to check when the button first goes low... new variable.

```cpp
bool button_State = HIGH; //Button state
bool prev_State = HIGH; 

```

Only send when first goes LOW.

```cpp
 if (button_State != prev_State && button_State == LOW) { //And if it's pressed

```

Then at the end of loop...

```cpp
  prev_State = button_State;

```

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [October 22, 2022, 9:49pm UTC](https://forum.arduino.cc/t/help-with-progaming-a-push-button-and-sim800l/1044903/5 "2022-10-22T21:49:32Z")

</div>

> [@josecarlossilva](#):
>
> button shoudl be able to be pressed , AND NOT RELEASED and only send 1 mensage

See: File -\> Examples -\> 02.Digital -\> StateChangeDetection

---

<div class="post-metadata">

**Author:** ![josecarlossilva](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@josecarlossilva](https://forum.arduino.cc/u/josecarlossilva)\
**Post date:** [October 22, 2022, 9:49pm UTC](https://forum.arduino.cc/t/help-with-progaming-a-push-button-and-sim800l/1044903/6 "2022-10-22T21:49:59Z")

</div>

im gonna try tomorow thanks

---

<div class="post-metadata">

**Author:** ![josecarlossilva](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@josecarlossilva](https://forum.arduino.cc/u/josecarlossilva)\
**Post date:** [October 23, 2022, 5:53pm UTC](https://forum.arduino.cc/t/help-with-progaming-a-push-button-and-sim800l/1044903/7 "2022-10-23T17:53:28Z")

</div>

> [@red\_car](#):
>
> `prev_State = button_State;`

keep sending sms  
didnt work

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [October 23, 2022, 6:20pm UTC](https://forum.arduino.cc/t/help-with-progaming-a-push-button-and-sim800l/1044903/8 "2022-10-23T18:20:31Z")

</div>

> [@josecarlossilva](#):
>
> didnt work

Did you insert the other line in the right place? Please post your current code.

---

<div class="post-metadata">

**Author:** ![josecarlossilva](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@josecarlossilva](https://forum.arduino.cc/u/josecarlossilva)\
**Post date:** [October 23, 2022, 7:11pm UTC](https://forum.arduino.cc/t/help-with-progaming-a-push-button-and-sim800l/1044903/9 "2022-10-23T19:11:44Z")

</div>

> [@red\_car](#):
>
> `prev_State = button_State;`

yep it was in the wrong place ! noob mistake

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 21, 2023, 7:12pm UTC](https://forum.arduino.cc/t/help-with-progaming-a-push-button-and-sim800l/1044903/10 "2023-04-21T19:12:06Z")

</div>

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