Send Single SMS only when triggered

Hello, this is a water level indicator. I have found the code in the internet, modified it to suit my needs. but the problem now is that the serial print/SMS is looping on the current status. How do I make the system only send a single message when it detects a water change or the switch turns on. The LED works well. Ps. I change mySerial to Serial for simulation.


#include <SoftwareSerial.h>
SoftwareSerial mySerial(7,8);

byte sensorPin[] = {11, 12, 13};
byte ledPin[] = {8, 9, 10}; // number of leds = numbers of sensors
const byte sensors = 3;
int level = 0;
char phonenum[] = "0";
int a=0;


void setup()
  {
    mySerial.begin(9600); // Setting the baud rate of GSM Module
    Serial.begin(9600); // Setting the baud rate of Serial Monitor (Arduino)
    for(int i = 0; i < sensors; i++)
      {
      pinMode(sensorPin[i], INPUT);
      pinMode(ledPin[i], OUTPUT);
      }
  }
void loop()
  {
    level = 0;
    for(int i = 0; i < sensors; i++)
      {
        if(digitalRead(sensorPin[i]) == LOW)
          {
            digitalWrite(ledPin[i], HIGH);
            level = sensors - i;
          }
        else
          {
            digitalWrite(ledPin[i], LOW);
          }
       }
    Serial.println("Water level Warning");
    switch(level)
      {
        case 1:
         Serial.println("AT+CMGF=1");
         delay(500);
         Serial.println("AT+CMGS=\"your no.\"");//Change the receiver phone number
         delay(500);
         Serial.print("Aler Warning: High Level."); //the message you want to send
         delay(500);
         Serial.write(26);
         delay(500);
        break;
        case 2:
         Serial.println("AT+CMGF=1");
         delay(500);
         Serial.println("AT+CMGS=\"your no.\"");//Change the receiver phone number 
         delay(500);
         Serial.print("Aler Warning: Moderate Level."); //the message you want to send
         delay(500);
         Serial.write(26);
         delay(500);
        break;
        case 3:
         Serial.println("AT+CMGF=1");
         delay(500);
         Serial.println("AT+CMGS=\"your no.\"");//Change the receiver phone number 
         delay(500);
         Serial.print("Aler Warning: Low Level."); //the message you want to send
         delay(500);
         Serial.write(26);
         delay(500);
        break;
        default:
          Serial.println("No water");
        break;
        }
      delay(50);
  }

image

In general the approach to doing something once only is to have a boolean flag, called say SMS_was_sent in your case, and initialise it to false.

Check the flag before you send the sms, and only send if it's false. Then set it true so it won't send again.

Then when the level is back in the safe range, set the flag to false, ready for next time.

I have tried this method. It actually work. tho it is not that efficient

    switch(level)
      {
        case 1:
      		if(a==0)
            {
              Serial.println("AT+CMGF=1");  
              delay(500);
              Serial.println("AT+CMGS=\"your no.\"");//Change the receiver phone number
              delay(500);
              Serial.print("Aler Warning: High Level."); //the message you want to send
              delay(500);
              Serial.write(26);
              delay(500);
              a++; b=0, c=0;
            }
        break;
        case 2:
      		if(b==0)
            {
              Serial.println("AT+CMGF=1");  
              delay(500);
              Serial.println("AT+CMGS=\"your no.\"");//Change the receiver phone number
              delay(500);
              Serial.print("Aler Warning: Moderate Level."); //the message you want to send
              delay(500);
              Serial.write(26);
              delay(500);
              b++; a=0, c=0;
            }
        break;
        case 3:
      		if(c==0)
            {
              Serial.println("AT+CMGF=1");  
              delay(500);
              Serial.println("AT+CMGS=\"your no.\"");//Change the receiver phone number
              delay(500);
              Serial.print("Aler Warning: Low Level."); //the message you want to send
              delay(500);
              Serial.write(26);
              delay(500);
              c++; a=0, b=0;
            }
        break;
        default:
      		if(d==0)
            {
              a=0; b=0; c=0;
            }
        break;

        }

image

A word from the wise... use meaningful variable names, especially if you're sharing code for other folk to comment on so it's easier to follow. a? b? c? d?

oh yes, am so sorry. I did not think of that as I have included a comment when I declared the variables. Peace. But thanks. I appreciate it

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