How do I get the GSM module to only send one text when the floater is pulled?

Can't quite figure this one out. I've set upp a GSM module and a float switch so that if my water tank makes the floater float up, it would send a text. However it just kept on sending text after text upon reaching the count of 4 (Was following a state change detection tutorial, this one: https://www.arduino.cc/en/Tutorial/StateChangeDetection). How do I have it send only one text once it gets pulled up once and then send another once it gets pulled down?

Code_test.ino (1.68 KB)

Instead of incrementing buttonPushCounter every time the button becomes pressed and acting when it is divisible by 4, change the program to set a boolean to true when the button becomes pressed.

Later in loop() send the text only when the boolean is true but set it to false at the same time to prevent further texts being sent

Thanks for the reply and sorry for the late reply! Been super busy with school. I'm not sure i'm doing this right. I've tried what you said (attached is the code) and it still keeps on sending messages. I added in a Serial.print to show what it was doing in the serial monitor and it keeps on switching from 1 to 0 then 1 to 0 repeatedly.

GSM SIM900A BEGIN
Enter character for control option:
h : to disconnect a call
i : to receive a call
s : to send message
c : to make a call
e : to redial

1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1

Code_test.ino (1.56 KB)

Does this work?

/*THIS TUTORIAL USED GSM SIM900A MINI V3.9.2
 
  Connect 5VT to D9 and 5VR to D10
  Feed GSM SIM900A with Arduino's 5V

  Code by IDAYU SABRI - MYBOTIC
*/
#define FLOAT_SWITCH 2
#include <SoftwareSerial.h>

SoftwareSerial mySerial(9, 10);

char msg;
char call;

int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState;

void setup()
{
    pinMode(FLOAT_SWITCH, INPUT_PULLUP);
    lastButtonState = digitalRead( FLOAT_SWITCH );
    
    mySerial.begin( 9600 );   // Setting the baud rate of GSM Module  
    Serial.begin( 9600 );    // Setting the baud rate of Serial Monitor (Arduino)
    Serial.println( "GSM SIM900A BEGIN" );
    Serial.println( "Enter character for control option:" );
    Serial.println( "h : to disconnect a call" );
    Serial.println( "i : to receive a call" );
    Serial.println( "s : to send message" );
    Serial.println( "c : to make a call" );  
    Serial.println( "e : to redial" );
    Serial.println();
    
    delay(100);
    
}//setup

void loop()
{
    buttonState = digitalRead(FLOAT_SWITCH);
    if(buttonState != lastButtonState) 
    {
        lastButtonState = buttonState;
        buttonPushCounter++;
        
        mySerial.println( "AT+CMGF=1" );    //Sets the GSM Module in Text Mode
        delay( 1000 );  // Delay of 1000 milli seconds or 1 second
        mySerial.println( "AT+CMGS=\"+639499964149\"\r" ); // Replace x with mobile number
        delay( 1000 );
        mySerial.println( (buttonState==HIGH) ? "HIGH":"LOW" );// The SMS text you want to send
        delay( 100 );
        mySerial.println( (char)26 );// ASCII code of CTRL+Z
        delay( 1000 );
            
        Serial.println( "Number of button changes: " );
        Serial.println( buttonPushCounter );
                     
    }//if
    else
        delay(50); 
        
}//loop

The OPs code

/*THIS TUTORIAL USED GSM SIM900A MINI V3.9.2

  Connect 5VT to D9 and 5VR to D10
  Feed GSM SIM900A with Arduino's 5V

  Code by IDAYU SABRI - MYBOTIC
*/
#define FLOAT_SWITCH 2
#include <SoftwareSerial.h>
SoftwareSerial mySerial(9, 10);
char msg;
char call;
bool buttonPushCounter = false;
int buttonState = 0;
int lastButtonState = 0;
void setup()
{
  pinMode(FLOAT_SWITCH, INPUT_PULLUP);
  mySerial.begin(9600);   // Setting the baud rate of GSM Module
  Serial.begin(9600);    // Setting the baud rate of Serial Monitor (Arduino)
  Serial.println("GSM SIM900A BEGIN");
  Serial.println("Enter character for control option:");
  Serial.println("h : to disconnect a call");
  Serial.println("i : to receive a call");
  Serial.println("s : to send message");
  Serial.println("c : to make a call");
  Serial.println("e : to redial");
  Serial.println();
  delay(100);
}

void loop()
{
  {
    buttonState = digitalRead(FLOAT_SWITCH);
  }
  {
    if (buttonState == HIGH)
    {
      buttonPushCounter = !buttonPushCounter;
      Serial.println(buttonPushCounter);
    }
    if (buttonPushCounter == true)
    {
      buttonPushCounter == false;
      mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
      delay(1000);  // Delay of 1000 milli seconds or 1 second
      mySerial.println("AT+CMGS=\"+639499964149\"\r"); // Replace x with mobile number
      delay(1000);
      mySerial.println("PLACEHOLDER TEXT ABOVE");// The SMS text you want to send
      delay(100);
      mySerial.println((char)26);// ASCII code of CTRL+Z
      delay(1000);
    }
  }
}

I'm not sure i'm doing this right. I've tried what you said (attached is the code)

You are not doing it right I am afraid. You did not do what I said. You are not detecting when the button becomes pressed, you are detecting when the button is pressed

Look at the code in reply #3 to see the difference

I finally get it! I understand where I went wrong now. Thanks so much!

Here's the final code. Thanks again for helping me understand and I hope this helps someone else out there too!

/*THIS TUTORIAL USED GSM SIM900A MINI V3.9.2
 
  Connect 5VT to D9 and 5VR to D10
  Feed GSM SIM900A with Arduino's 5V

  Code by IDAYU SABRI - MYBOTIC
*/
int lastButtonState = 0;
int buttonState = 0;
int buttonPushCounter = 0;
#define FLOAT_SENSOR 2
#include <SoftwareSerial.h>
SoftwareSerial mySerial(9, 10);
char msg;
char call;
void setup()
{
  
  mySerial.begin(9600);   // Setting the baud rate of GSM Module  
  Serial.begin(9600);    // Setting the baud rate of Serial Monitor (Arduino)
  Serial.println("GSM SIM900A BEGIN");
  Serial.println("PLACEHOLDER TEXT");
  pinMode(FLOAT_SENSOR, INPUT_PULLUP);
  lastButtonState = digitalRead(FLOAT_SENSOR);
}

void loop()
{

buttonState = digitalRead(FLOAT_SENSOR);
if(buttonState != lastButtonState)
{
lastButtonState = buttonState;
mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000); // Delay of 1000 milli seconds or 1 second
mySerial.println("AT+CMGS="+639499964149"\r"); // Replace x with mobile number
delay(1000);
mySerial.println("FLOAT SWITCH TRIGGERED");// The SMS text you want to send
delay(100);
mySerial.println((char)26);// ASCII code of CTRL+Z
delay(1000);
buttonPushCounter++;
Serial.println(buttonPushCounter);
}
}