Please Help Me. I want to send the Message once but it keeps repeating i have no idea to fix this

#include <SoftwareSerial.h>

//Alarm reciever's phone number with country code
const String PHONE = "94+++++++++";

//GSM Module RX pin to Arduino 3
//GSM Module TX pin to Arduino 2
#define rxPin 2
#define txPin 3
SoftwareSerial sim800(rxPin, txPin);

//the pin that the Arduino Board is atteched to
#define trigPin 8 // Ultrasonic Sensor
#define echoPin 9 // Ultrasonic Sensor
#define greenLight 13 //(Relay 1)
#define redLight 12 //(Relay 2)

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(greenLight, OUTPUT);
pinMode(redLight, OUTPUT);
pinMode(warnLight, OUTPUT);

Serial.begin(115200); // initialize serial

sim800.begin(9600);
Serial.println("SIM800L software serial initialize");

sim800.println("AT");
delay(1000);
}

void loop() {

//////////////////////////////////////////////////
while (sim800.available()) {
Serial.println(sim800.readString());
}
//////////////////////////////////////////////////
while (Serial.available()) {
sim800.println(Serial.readString());
}
//////////////////////////////////////////////////
long time_duration, distance_in_cm;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
time_duration = pulseIn(echoPin, HIGH);
distance_in_cm = time_duration / 29 / 2;

Serial.print(distance_in_cm);
Serial.println(" cm");

//Ranging Distance − 2cm – 400 cm
if (distance_in_cm <= 55) {
Serial.println("Red Light");
Serial.println("Transmitting Message");
sim800.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000);
sim800.println("AT+CMGS="" + PHONE + ""\r"); //Mobile phone number to send message
delay(1000);
String SMS = "Red Light";
sim800.println(SMS);
delay(100);
sim800.println((char)26);// ASCII code of CTRL+Z
delay(1000);
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
delay(500);
}
else {
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
delay(500);
Serial.println("Green Light");
Serial.println("Transmitting Message");
sim800.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000);
sim800.println("AT+CMGS="" + PHONE + ""\r"); //Mobile phone number to send message
delay(1000);
String SMS = "Green Light";
sim800.println(SMS);
delay(100);
sim800.println((char)26);// ASCII code of CTRL+Z
delay(1000);
}
}

Do you understand that the loop() function gets called over and over and over? Every time through the loop, you send a message so why would you expect it to only send once?

1 Like

See if reading this thread hepls you

a7

1 Like

It's the loop function, you have to modify that so it doesn't continuously come up

1 Like

I know it's getting called over and over. i want the GSM Module(Sim800l) to send the massage (redLight) Once if the Ultrasonic Sensor distance_in_cm <= 55
else
send the massage (greenLight) Once

Plz Help Me. What Should I Do

Thank you for your concern. but that didnt Help :slightly_smiling_face:

how should I supposed to modify it. if you know something to resolve this problem please help me

Create a global flag variable. Initialize it to false. Make sending the message depend on the flag being false. When you send the message set the flag value to true. Then the next time through loop() the flag will not be false and sending the message will be skipped.

1 Like

Haha, seemed perfect.

You want to do something once, when a condition is met, but not over and over, until, so to speak, the condition is un-met.

Which raises the question - what woukd have to happen before you ever sent the message again? What would, in @groundFungus’s idea, fix the suggested flag so the message would be sent again?

Assuming you do not literally mean once, only once, when the condition is met, and never again no matter what?

While I am misleading you, it seems you would need to have some kind of deadband, so the condition at the threshold of qualifying would not send multiple message as the number fluctuated around the threshold.

Never a waste of time to refresh your memory or discover the concept of hysteresis.

In this case meaning the condition has to very much go away before the mechanism would be allowed to recognize the condition again.

So it is like the offered example at the link I provided… think of the switch like your condition qualifying, then not qualifying.

Send the message if the distance goes below 55.
Don’t send the message again until/unless the distance has gone above, say, 100.

HTH or at least amuses you for some time.

a7

1 Like

If you truly want to only send a message once, move all your code into setup() and leave loop() empty.

@thisura_1 I assume you also don't want the other message to be repeated.

Try this in the wokwi

It really helps with hare raisin code to separate the logic from the other stuff, get the logic correct first.

// hysteresis demo sketch

void setup() {
  Serial.begin(9600);
  Serial.println("HJello World!\n");
}

unsigned char sendLowMessage = 1;		// yes, we need to send the low message
unsigned char sendHighMessage = 1;		// yes, we need to send the high message

void loop() {

  int distance_in_cm = getTheDistance();		// Write that function. Unclutter your loop!

  if (distance_in_cm <= 333) {
    if (sendLowMessage) {

      // call the function that sends the Red Light Message
      sendRedLightMessage();	// Write another function. Unclutter your loop!

      sendLowMessage = 0;			// disable the Red Light Message
      sendHighMessage = 1;		// enable the Green Light Message
    }
  }

  if (distance_in_cm >= 666) {
    if (sendHighMessage) {

      // call the function that sends the Green Light Message
      sendGreenLightMessage();	// Write yet another function. Unclutter your loop!

      sendHighMessage = 0;		  // disable the Green Light Message
      sendLowMessage = 1;		    // enable the Red Light Message

    }
  }

  Serial.println("idle, no need to do nothing");
}

int getTheDistance()
{
  return analogRead(0);
}

void sendRedLightMessage()
{
  Serial.println("\nsending RED message boss!\n");
  delay(777);
}

void sendGreenLightMessage()
{
  Serial.println("\nsending GREEN message boss!\n");
  delay(777);
}

HTH

a7

title of thread

Please Help Me.

You are very short on words. Beeing so short on words is the reason why the opposite of "helping" happends.

you have to show some own effort. The mimimum things you can do is:

  • write a guessing how it might work
  • asking for a link where you can read yourself about such functionalities
  • asking for a link with demo-code that has such a functionality

last but not least following at least the most basic forum-rules
by reading the explanation

best regards Stefan

Learn to code in C++ for starters is what you should do. Next implement your solution.

If something is less than or equal to 55 then do the thing.

if( something<=55)
{
mow the grass
Shine the light
make a smoothie
} else {
do the other thing
}

Simple.

But then the thing gets done over and over…

I only combed through the OP's mess of a post because I hvae an obsession a special interest in hysteresis.

I have no idea what s/he wants or needs, just saw that both the if and the else would make spam as the loop ran.

I was typing a further description, the thread I pointed out not having found purchase in the infertile soil of the OP's brain, when I figured it would just be easier to code the damn thing and head for the beach.

The wokwi I posted upthread is a straight ahead tots beginner level demonstration of how to do one thing once, one thing on each side of the swing. if it lands, fine. Otherwise, I just had some fun (yes, I have no life) and gave wokwi a bit more publicity.

It is also a model for how to write code that is made easier to read, debug and maintain, and so may be of value to a few of the 13 or 14 ppl who have read this far.

a7

Thanks, I appreciated it. this is what I needed but the thing is how should I apply this to my project

If you read the comments, it should be obvious where to transplant the code you have into my demo.

Your if clause does one thing.

You else clause does the other.

You have some code to initialise the other stuff my demo does not attempt to do.

Read the demo closely.

Try to come to an understanding of the structure and function of the demo.

I left functions that get called executed which only print a message, fill them in with the details of what really must happen.

Leave the print statements in there so you can see that the code is flowing according to design.

Srsly, it’s a straightforward matter of cutting and pasting. If it is beyond your ability, you need to take a few steps backwards and learn some basic programming skills.

Give it a try, post the results here in your next posting. Post code that compiles and runs, even if it doesn’t yet do what you want, and describe what it is doing that it shouldn’t, or isn’t doing what it should.

You got this.

a7

2 Likes

I kinda figured that out that I should learn more C++

Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

2 Likes

thank you and about that you told earlier I am sorry

1 Like

You are welcome. Ask as many specific questions as you like.
if the questions are specific most users here enjoy answering them.

best regards Stefan

2 Likes