Hi Sir!
I have been working on a simple water level monitoring system which i have integrated with a sim900 gsm shield.
The setup uses Arduino Uno r3.
Everything works just fine, The program is recursive, it constantly gets input and provide output, the programming is done using simple nested if-else.
Problem: Every time the if condition is true it will call the functions, three functions for now, and continues to execute since it is in the loop function. The functions gets string input and sends text messages to the mobile number. The only problem is that when the condition becomes true it continuously starts sending messages until other conditions are met and then it starts sending messages according to other conditions.
What I want: It only calls the function which sends text messages only once in the program startup cycle but the program should run continuously.
I searched run once functions but my programming language is very limited, so i ask you Sir, to help me achieve what i want from it.
Thanking you in advance.
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
SoftwareSerial SIM900(0, 1);
int sump=A0;
int qut=A1;
int hlf=A2;
int thf=A3;
int ful=A4;
int motor=8;
int buz=7;
int s;
int q;
int h;
int t;
int f;
int i; //motor status flag
int v=100; //comparison variable(needs some adjustment)
int b=0; //buzzer flag
int m=0; //motor flag
int c=0; //sump flag
String outHello = " Hello Message Hello! Welcome to Water Monitoring System";
String outlow="Sump is Low";
String fourquarters="Water Level is Fourth Quarter";
String mobnumber = "+XXXXXXXXXXX";
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
smshello(); // sends hello message in first run, no condtions needed for it
pinMode(qut,INPUT);
pinMode(hlf,INPUT);
pinMode(qut,INPUT);
pinMode(ful,INPUT);
pinMode(sump,INPUT);
pinMode(motor,OUTPUT);
pinMode(buz,OUTPUT);
lcd.begin(16, 2);
digitalWrite(buz,LOW);
}
void loop()
{
i=digitalRead(motor);
s=analogRead(sump);
q=analogRead(qut);
h=analogRead(hlf);
t=analogRead(thf);
f=analogRead(ful);
lcd.clear();
if(f>v && t>v && h>v && q>v )
{
lcd.setCursor(0,0);
lcd.print(char(219));
lcd.print(char(219));
lcd.print(char(219));
lcd.print(char(219));
lcd.setCursor(5,0);
lcd.print("FULL");
m=0;
b=0;
}
else
{
if(f<v && t>v && h>v && q>v)
{
lcd.setCursor(0,0);
lcd.print(char(219));
lcd.print(char(219));
lcd.print(char(219));
lcd.print("_");
lcd.setCursor(5,0);
lcd.print("3/4th");
b=0;
}
else
{
if(f<v && t<v && h>v && q>v)
{
lcd.setCursor(0,0);
lcd.print(char(219));
lcd.print(char(219));
lcd.print("_");
lcd.print("_");
lcd.setCursor(5,0);
lcd.print("HALF");
m=1;
b=0;
}
else
if(f<v && t<v && h<v && q>v)
{
lcd.setCursor(0,0);
lcd.print(char(219));
lcd.print("_");
lcd.print("_");
lcd.print("_");
lcd.setCursor(5,0);
lcd.print("1/4th");
b=0;
sms4thquarter();// call messaging function to send message, but we want that functions should be called once as per conditions in every start up.
}
else
{
if(f<v && t<v && h<v && q<v)
{
lcd.setCursor(0,0);
lcd.print("_");
lcd.print("_");
lcd.print("_");
lcd.print("_");
lcd.setCursor(5,0);
lcd.print("LOW");
b=0;
}
else
{
digitalWrite(motor,LOW);
lcd.setCursor(0,0);
lcd.print("ERROR!");
b=1;
}
}}}
if(i==HIGH)
{
lcd.setCursor(0,1);
lcd.print("Motor ON");
}
else
{
lcd.setCursor(0,1);
lcd.print("Motor OFF");
}
if(s>v && m==1)
{
digitalWrite(motor,HIGH);
}
if(s<v)
{
digitalWrite(motor,LOW);
lcd.setCursor(11,0);
lcd.print("Low");
lcd.setCursor(11,1);
lcd.print("Sump");
c=1;
smssumplow();
}
if(s>v)
{
c=0;
}
if(m==0)
{
digitalWrite(motor,LOW);
}
}
void smshello()
{
SIM900.begin(19200);
delay(20000); // give time to log on to network.
SIM900.print("AT+CMGF=1\r");
delay(1000);
SIM900.println("AT + CMGS = \"" + mobnumber +"\"");
delay(1000);
SIM900.print(outHello);
delay(1000);
SIM900.write((char)26); //ctrl+z
delay(1000);
}
void smssumplow()
{
SIM900.begin(19200);
delay(1000); // give time to log on to network.
SIM900.print("AT+CMGF=1\r");
delay(1000);
SIM900.println("AT + CMGS = \"" + mobnumber +"\"");
delay(1000);
SIM900.print(outlow);
delay(1000);
SIM900.write((char)26); //ctrl+z
delay(1000);
}
void sms4thquarter()
{
SIM900.begin(19200);
delay(1000); // give time to log on to network.
SIM900.print("AT+CMGF=1\r");
delay(1000);
SIM900.println("AT + CMGS = \"" + mobnumber +"\"");
delay(1000);
SIM900.print(fourquarters);
delay(1000);
SIM900.write((char)26); //ctrl+z
delay(1000);
}