SIM800l not reading input correctly

Hi all,

I received 3 sms texts until i unplugged.
First text "TEST"
Second "value change"
third " value change"

and i suspect it will continuosly send the latter text. My code is at fault. I would like to read a value from pin 10 and if its high have it text "value change" however the pin was low and i was getting those sms text messages as mentioned earlier. Can someone assist me.

This is my code :slightly_frowning_face: :

#include <SoftwareSerial.h>
 
//SIM800 TX is connected to Arduino D8
#define SIM800_TX_PIN 8
 
//SIM800 RX is connected to Arduino D7
#define SIM800_RX_PIN 7

 const int valuepin = 10;
 int valuepinvalue = 0;
//Create software serial object to communicate with SIM800
SoftwareSerial serialSIM800(SIM800_TX_PIN,SIM800_RX_PIN);
 
void setup() {
  pinMode(valuepin, INPUT);
  //Begin serial comunication with Arduino and Arduino IDE (Serial Monitor)
  Serial.begin(9600);
  while(!Serial);
   
  //Being serial communication witj Arduino and SIM800
  serialSIM800.begin(9600);
  delay(1000);
   
  Serial.println("Setup Complete!");
  Serial.println("Sending SMS...");
   
  //Set SMS format to ASCII
  serialSIM800.write("AT+CMGF=1\r\n");
  delay(1000);
 
  //Send new SMS command and message number
  serialSIM800.write("AT+CMGS=\"3385933\"\r");
  delay(1000);
   
  //Send SMS content
  serialSIM800.write("TEST");
  delay(1000);
   
  //Send Ctrl+Z / ESC to denote SMS message is complete
  serialSIM800.write((char)26);
  delay(1000);
     
  Serial.println("SMS Sent!");
}
 
void loop() {
valuepinvalue = digitalRead(valuepin);
if (valuepinvalue == 1){

  serialSIM800.write("AT+CMGF=1\r\n");
  delay(1000);
 
  //Send new SMS command and message number
  serialSIM800.write("AT+CMGS=\"3385933\"\r");
  delay(1000);
   
  //Send SMS content
  serialSIM800.write("value change");
  delay(1000);
   
  //Send Ctrl+Z / ESC to denote SMS message is complete
  serialSIM800.write((char)26);
  delay(1000);
     
  Serial.println("SMS Sent!");
  
}
  
}

my hardware is an arduino uno and a sim800l breakout board bought of eBay.
My SIM800l tx is connected to digital pin 8 and my SIM800; RX is conected to digital pin7

my hardware is an arduino uno and a sim800l breakout board bought of eBay.
My SIM800l tx is connected to digital pin 8 and my SIM800; RX is conected to digital pin7

No switch?

If you DO have a switch, how IS it wired? I'm going to guess that you don't have an external pulldown resistor, and that, therefore, you have a floating pin.

Hi PaulS thank you for your input. I didnt use any switch per say, i have a wire that i toggle from a 5V rail and ground.

i have a wire that i toggle from a 5V rail and ground.

So, no external resistors? Either add one or use the internal pullup resistor.

Thanks PaulS. It worked.

I now want to adjust my code to send one text when the value has changed, so if it goes low i want one text saying that and when it changes to high after i fix what cause it to go low i want one text saying it is low now.

I am a newbie so i want to start with just having it send one text when it is low for now.

I tried using a while loop when the counter is 0, its initial state and add 1 to the counter when the pin changes state so that it will only send one text and increment the counter thus forcing it to stop sending since the counter is not 0. but this does not work, it sends mutiple texts when the pin low and stops when i put it back to high.

Here is my code, can someone assist me please ?

#include <SoftwareSerial.h>
 
//SIM800 TX is connected to Arduino D8
#define SIM800_TX_PIN 8
 
//SIM800 RX is connected to Arduino D7
#define SIM800_RX_PIN 7

 const int valuepin = 5;
 int valuepinvalue = 0;
 int counter = 0;
//Create software serial object to communicate with SIM800
SoftwareSerial serialSIM800(SIM800_TX_PIN,SIM800_RX_PIN);
 
void setup() {
  pinMode(valuepin, INPUT);
  //Begin serial comunication with Arduino and Arduino IDE (Serial Monitor)
  Serial.begin(9600);
  while(!Serial);
   
  //Being serial communication witj Arduino and SIM800
  serialSIM800.begin(9600);
  delay(1000);
   
//  Serial.println("Setup Complete!");
//  Serial.println("Sending SMS...");
//   
//  //Set SMS format to ASCII
//  serialSIM800.write("AT+CMGF=1\r\n");
//  delay(1000);
// 
//  //Send new SMS command and message number
//  serialSIM800.write("AT+CMGS=\"3385933\"\r");
//  delay(1000);
//   
//  //Send SMS content
//  serialSIM800.write("TEST");
//  delay(1000);
//   
//  //Send Ctrl+Z / ESC to denote SMS message is complete
//  serialSIM800.write((char)26);
//  delay(1000);
//     
//  Serial.println("SMS Sent!");
}
 
void loop() {
  
 while (counter = 0);{

  
valuepinvalue = digitalRead(valuepin);
if (valuepinvalue == 0){

int counter = counter + 1;

  serialSIM800.write("AT+CMGF=1\r\n");
  delay(1000);
 
  //Send new SMS command and message number
  serialSIM800.write("AT+CMGS=\"3385933\"\r");
  delay(1000);
   
  //Send SMS content
  serialSIM800.write("Water Level Dropped");
  delay(1000);
   
  //Send Ctrl+Z / ESC to denote SMS message is complete
  serialSIM800.write((char)26);
  delay(1000);
     
  Serial.println("SMS Sent!");

  
}}
  
}
 while (counter = 0);{

WTF? Why are you assigning 0 to counter in a while statement? Why does nothing happen in the body of the while statement (the ; on the end forms the body)?

Whyistherethenauselesscurlybracejammedupagainsttheendofthestatement?

int counter = counter + 1;

Having a local variable with the same name as a global variable is plain stupid.

Look at the state change detection example. It shows how to determine that a switch has become pressed or has become released. Send one message when the switch becomes pressed. Send another when the switch becomes released. It is NOT necessary to count anything.