how to send Alert and avoid doing it second time (loop)?

Hello, here is my code to monitor electricity and send sms on state change

boolean electricity_status = 1; //remember if light goes we want every voltage so it is 1 - light prevents off state and gives 5v when light goes the relay goes 0 
boolean device1_status = 0; 
boolean device2_status = 0; 

void setup(){
  pinMode(5, INPUT); //get the status of electricity 1/0
  pinMode(6, OUTPUT); //LED to change on status

}

void loop() {
  electricity_status = digitalRead(5);
if (electricity_status == 0){ //if electricity [b]goes[/b]
    //electricity gone inform via sms - also Do something when electricity goes
 sms(1);  // the one is to inform exactly electricity changed status IT MUST DO THIS JUST ONE TIME UNTILL STATUS CHANGE
digitalWrite(6, LOW);
}
if (electricity_status == 1){ //if electricity [b]comes[/b]
sms(2);  // the one is to inform exactly electricity changed status IT MUST DO THIS JUST ONE TIME UNTILL STATUS CHANGE
digitalWrite(6, HIGH);
}

//other devices with same thing
if (device1_status == 1){ 
sms(3);  // the one is to inform exactly electricity changed status IT MUST DO THIS JUST ONE TIME UNTILL STATUS CHANGE
}
if (device1_status == 0){ 
sms(4);  // the one is to inform exactly electricity changed status IT MUST DO THIS JUST ONE TIME UNTILL STATUS CHANGE
}


}

void sms() {
//read the variable value and act according to it..
//send sms to my number
}

Want i want is to inform me of the state change of device (e.g electricity) via sms only 1 time in 1 loop do not continue doing so in the next loop ... the above code sends me continues sms.... i know because of the if condition but to explain that i have to write the code here...Do remember i have other devices too so i want to do the same with them too...Kindly help

i hope i explained it properly...

you must keep a global variable that holds the last state + last time you send an SMS.

If you go through loop you will see the last known state is still 'alarm' and you can check how long ago you send the SMS. If that is less than say 10 minutes you do not need to send a new SMS.

After 10 minutes you can decide to send an SMS to a second number etc

i am sorry but i am not a programmer guy i am much like interested in this things....

So what you mean to say is

include this thing


boolean electricity_state = 0; void loop() { electricity_status = digitalRead(5); if (electricity_status == 0){ //if electricity goes //electricity gone inform via sms - also Do something when electricity goes sms(1); // the one is to inform exactly electricity changed status IT MUST DO THIS JUST ONE TIME UNTILL STATUS CHANGE digitalWrite(6, LOW); electricity_state = 1; // do you mean this }

}

void sms() { //read the variable value and act according to it.. //send sms to my number

//set time how to do that ? if suppose the electricity FLIP within 30 sec than ? }

some snippet to give you the idea

boolean prevState;;
boolean alarm = false;

uint32_t lastSMS  = 0;

void setup()
{
....
  prevState= getElectricityStateOK();  // init
}

void loop()
{
   boolean stateOK = getElectricityStateOK(); // you know how to do that

   if (stateOK  !=  True)  // there is an alarm state
  {
    if (millis() - lastSMS  > threshold)  sendSMS();      // send an SMS every threshold milliseconds.
    lastSMS = millis();

    if (prevState != stateOK)   // has state changed since last time?
    {
        alarm = true;
        ... do additional things when alarm goes of here (only one)
    }
   ... do things here while in alarm state ... every loop...

  }
  else
  {
    // reset the alarm 
     alarm = false;  
  }
  prevState = stateOK ; // remember the last state


  // rest of the program
  ...
  if (alarm) digitalWrite(LED, HIGH);
  else digitalWrite(LED, LOW);
  ...
}

i am missing something i guess sorry i tried this code but it is not working

boolean prevState;; boolean alarm = false; int myreturn = 0; uint32_t lastSMS = 0; uint32_t threshold = 0; void setup() { Serial.begin(9600); pinMode(3, INPUT); //get the status of electricity 1/0 pinMode(6, OUTPUT); //LED to change on status prevState= getElectricityStateOK(); // init }

void loop() { boolean stateOK = getElectricityStateOK(); // you know how to do that

if (stateOK != [u]1[/u]) // there is an alarm state { if (millis() - lastSMS > threshold) sendSMS(); // send an SMS every threshold milliseconds. // the threshhold gave me an error so i defined it above lastSMS = millis();

if (prevState != stateOK) // has state changed since last time? { alarm = true; // ... do additional things when alarm goes of here (only one)

} // do things here while in alarm state ... every loop... // you mean to say i must include all the code inside this section ? } else { // reset the alarm alarm = false; } prevState = stateOK ; // remember the last state

// rest of the program ** //or do you mean all the program here ?** if (alarm) digitalWrite(6, HIGH); else digitalWrite(6, LOW);

delay(3000);

}

int getElectricityStateOK() { Serial.println("getting electric state function triggered"); myreturn = digitalRead(5); return myreturn;

} void sendSMS(){ Serial.println("send sms function triggered"); }

the above code as tested doesn't work i tried to set the input 3 low high no use... i do not know why the led didn't even started.... sorry for ignorance... it is too much info to chew for me....

please use code tags when you post code: The # buttons above the smileys

i tried this code but it is not working

define not working - what do you get (or not) - what did you expect

please post output or so

You have to set threshold as the number of milliseconds you want to delay sending the second SMS. You've set it as 0, so there won't be any delay! I suggest you set it to 10,000 to give a 10 second delay.

Okay i was unable to understand the above provided code (sorry it was too complex for me) so i tried search the forum for a little guidance... i found one door open and close alarm so i used it. It is perfect and i tweaked it to work in my condition.... But i want to make it short and good so can anyone help me to optimize it because i have a lot of code So here it is what i want now..

1) Optimize and minimize the code...because i have other functions and procedures too this is only one part of the arduino... i am using all the digital ports and analogue ports of arduino currently the code is (without this function) - program space - 72% (without adding the electricity monitor code) Sram space - 62% (without adding the electricity monitor code) still there is more code to add like function to match date/time and execute according to it... if any direction for that it would save my time thank you... 2) Add more devices to check same things as electricity i mean like door,window open/close on/off state with minimizing the code...same alert as electricity is doing SMS once on ONLY state change. 3) This code might help others....

int electricityContactPin = 5; // pin electricity contact is attached to
int ledPin = 6; // led connected to pin 6 thru resistor to gnd
unsigned long electricityOpenTime = 0;
unsigned long electricityClosTime = 0;
unsigned long time_threshold = 5000; //time in ms => 5 sec.
int currelectricityState = digitalRead(electricityContactPin);
int prevelectricityState = digitalRead(electricityContactPin); // Assumes that LOW means closed and electricity is gone when the Arduino starts
boolean electricityIsClosed = true; // Let's assume it's gone for starting
boolean SMSSent = true;   // Tells us whether we've sent an SMS for the latest instance of the electricity being present

void setup()
{
  Serial.begin(9600);
  pinMode(electricityContactPin, INPUT);
  pinMode(ledPin, OUTPUT);
}


//Send sms (replaced with led status)
void sendSMS()
{
  digitalWrite(ledPin, HIGH);
  delay(3000);
  digitalWrite(ledPin, LOW);
  Serial.println("SMS SENT");
  }

void loop()
{

 //1
  currelectricityState = digitalRead(electricityContactPin);
  
  
  //2
  if (currelectricityState != prevelectricityState)  // electricity was closed and is now open or electricity was open and is now closed
  {
    Serial.println("!!!DETECTED STATE CHANGE!!!");
    delay(1000);
    if (currelectricityState == LOW)
    {
      // electricity is now gone
      electricityIsClosed = true;
      electricityClosTime = millis();
      delay(2000);
    }
    else
    {
      // electricity is now present
      electricityIsClosed = false;
      electricityOpenTime = millis();
      //       Serial.println("electricity is OPEN");
      delay(2000);
    }
    SMSSent = false; // electricity state changed, we may have a new opportunity to send SMS
  }
  
  //3
  prevelectricityState = currelectricityState;

//4
  // Now see if the electricity is open
  if (!electricityIsClosed && !SMSSent)
  {
    Serial.println("Present");
    if (millis() - electricityOpenTime >= time_threshold)
    {
      Serial.println("Present SMS");
      sendSMS();
      SMSSent = true;
    }
  }

  if (electricityIsClosed && !SMSSent)
  {

    Serial.println("GONE");
    if (millis() - electricityClosTime >= time_threshold)
    {
      Serial.println("GONE SMS");
      sendSMS();
      SMSSent = true;
    }
  }
}

Any one

int timesSend = 0;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  if (timesSend == 0)  // can also be a boolean
  {
    timesSend++;
      Serial.print("1");
   }
  Serial.print("0");
  delay(1000);
}

this is the essence

themedplant:
Any one

I’d start with two “if” conditions," alarm" and “noalarm” that check for the physical alarm state, and use a status variable “new” and “old” to track the alarm status. In the noalarm condition set the status to new. In the alarm condition send the message and set the status as old. In the loop, check the alarm status in the alarm conditional loop. If the status is old, do nothing. If the status is new, send the message and change the status to old.