Managing void loop

Hello guys... please am new to programming. I wrote a code but due to void loop activities am receiving unending SMS alerts. Please how can I manage the number of times a function is called by void loop. For instance, at every input i should receive just one sms (High or Low inputs at an input pin should give a corresponding output sms).

So please how to limit the SMS to be sent once at every change in input.

please find code attached.
this is the code

#include<SoftwareSerial.h>
SoftwareSerial mySerial(9,10);
int NEPA=7;
int NEPA_oldvalue=0;
int NEPA_newvalue,NEPA_value;
int check_NEPAvalue;



void setup()
{
  pinMode(NEPA,INPUT);
  mySerial.begin(9600);
  Serial.begin(9600);
  delay(1000); 
}

void loop()
{
  NEPA_newvalue=digitalRead(NEPA);
  delay(500);
  check();
  delay(1000);
}

void check() 
{
  NEPA_newvalue=NEPA_value;
    if (NEPA_value == NEPA_oldvalue)
  {
    CheckNEPA();
  }
  else
  {
    CheckNEPA();
  }
}

void CheckNEPA()
{
   if(NEPA_value == NEPA_oldvalue)
  {
    Serial.println("message started");
    delay(500);
    mySerial.println("AT+CMGF=1");
    delay(500);
    mySerial.println("AT+CMGS=\"+2347033670228\"\r");
    delay(500);
    mySerial.println("NEPA_ON!");
    delay(500);
    Serial.println("NEPA_ON!");
    delay(500);
    Serial.println("message completed");
    delay(500);
    mySerial.println((char)26);
    delay(500);
    }
  }
  else
  {
    Serial.println("message started");
    delay(500);
    mySerial.println("AT+CMGF=1");
    delay(500);
    mySerial.println("AT+CMGS=\"+2347033670228\"\r");
    delay(500);
    mySerial.println("NEPA_OFF!");
    delay(500);
    Serial.println("NEPA_OFF");
    delay(500);
    Serial.println("message completed");
    delay(500);
    mySerial.println((char)26);
    delay(500);
    }
}

STUPID_TEST_2.ino (1.42 KB)

Nobody will look at your code until you post the ENTIRE code with code tags.

Please use code tags when posting code
Edit your post and
type
** **[code]** **
before the code
type
** **[/code]** **
after the code

I guess you typed this on a cellphone and hence all the errors with capitalisation. Please try to make an effort to post correct code, even if you use a cellphone or tablet.

There is a difference between '=' and '=='.

If (new_state = old_state)

You are assigning the value of old_state to new_state, you're not comparing.

OK guys... please am sorry am using a phone. It means I have to use a system to upload the entire code for proper understanding. Thanks

Hi,
you use void not Void, what does the error message say when you try to compile?
Also { and }
for example:

void setup() 
{
  // put your setup code here, to run once:

}

void loop() 
{
  // put your main code here, to run repeatedly:

}

Tom.. :slight_smile:

Please make sure you post your actual code, not something you just made up.
That simply wastes time.

So please how to I limit the SMS to be sent once at every change in input

Once the SMS has been sent set a boolean variable, let's name it smsHasBeenSent, to true and only send SMSs when the variable is false. When you want to send another SMS set the variable to false

TomGeorge:
Hi,
you use void not Void, what does the error message say when you try to compile

I think op first needs to install the compiler on his/her cellphone.

UKHeliBob:
Once the SMS has been sent set a boolean variable, let's name it smsHasBeenSent, to true and only send SMSs when the variable is false. When you want to send another SMS set the variable to false

Please... can you give a little example or use my code...? I am new here so I don't get what you just said but it appears you understand what I want.

There an example in the examples called StateChangeDetection that shows how to debounce and perform a
single action on a button press.

Let's start with what your code does.

In loop(), you assign a value to NEPA_newvalue. Next you call check() and in check the first thing you do is that you overwrite NEPA_newValue. Why?

Next you have some test ('if') and regardless of the result you call CheckNEPA(); you can just as well leave the 'if' out. That condition will anyway always evaluate to true because NEPA_value and NEPA_oldvalue are always 0.

Next in CheckNEPA, you send one of two messages based on the same condition that will always evaluate to true (again NEPA_value and NEPA_oldvalue are always 0).

So the question is what you want to achieve? Do you want to send the status of a pin only when the pin's status changes?

PS
Never change a post after you have had replies; just add additional information (in this case your correct code) in a new reply.

sterretje:
PS
Never change a post after you have had replies; just add additional information (in this case your correct code) in a new reply.

Except to add code tags :slight_smile:

sterretje:
Let's start with what your code does.

In loop(), you assign a value to NEPA_newvalue. Next you call check() and in check the first thing you do is that you overwrite NEPA_newValue. Why?

Next you have some test ('if') and regardless of the result you call CheckNEPA(); you can just as well leave the 'if' out. That condition will anyway always evaluate to true because NEPA_value and NEPA_oldvalue are always 0.

Next in CheckNEPA, you send one of two messages based on the same condition that will always evaluate to true (again NEPA_value and NEPA_oldvalue are always 0).

So the question is what you want to achieve? Do you want to send the status of a pin only when the pin's status changes?

PS
Never change a post after you have had replies; just add additional information (in this case your correct code) in a new reply.

Yes... I want to send the status(SMS) only when there's a change in pin status

mfonobong:
Yes... I want to send the status(SMS) only when there's a change in pin status

OK, see reply #9. Write something based on that and if you get stuck, post your new code.

sterretje:
OK, see reply #9. Write something based on that and if you get stuck, post your new code.

OK thanks

Hello, please someone help check this codes because the first responds as expected but the second gives continuous (infinite) sms which is not what is desired.

const int NEPA = A0;
const int GEN = A1;
const int SOLAR = A2;

int NEPA_state = 0;
int GEN_state = 0;
int SOLAR_state = 0;
int Last_NEPAstate = 0;
int Last_GENstate = 0;
int Last_SOLARstate = 0;
int sms_count = 0;

void setup()
{
   pinMode(NEPA, INPUT);
   pinMode(GEN, INPUT);
   pinMode(SOLAR, INPUT);
   Serial.begin(9600);
}

void loop()
{
    {NEPA_state = digitalRead(NEPA);
       if (NEPA_state != Last_NEPAstate)
       {check_NEPA();}
     }

    {GEN_state = digitalRead(GEN);
       if (GEN_state != Last_GENstate)
         {check_GEN();}
       }

      {SOLAR_state = digitalRead(SOLAR);
         if (SOLAR_state != Last_SOLARstate)
           {check_SOLAR();}
         }
    }
 
void check_NEPA()
{
   if (NEPA_state == HIGH)
      {
         sms_count++;
         Serial.println("NEPA_ON!");
          delay(500);
       
  }
     else
        {
           Serial.println("NEPA_OFF!");
           delay(500);
         
    }
      Last_NEPAstate = NEPA_state;
}

void check_GEN()
{
   if (GEN_state == HIGH)
      {
        sms_count++;
        Serial.println("GEN_ON!");
     
  }
    else
      {
          Serial.println("GEN_OFF!");
          delay(500);
         
    }
     Last_GENstate = GEN_state;
   
}

void check_SOLAR()
{
   if (SOLAR_state == HIGH)
      {
         sms_count++;
         Serial.println("SOLAR_ON!");
       
  }
     else
        {
           Serial.println("SOLAR_OFF!");
           delay(500);
         
    }
     Last_SOLARstate = SOLAR_state;
}

Code with infinite sms as output:

#include<SoftwareSerial.h>
SoftwareSerial mySerial(9, 10);

const int NEPA = A0;
const int GEN = A1;
const int SOLAR = A2;

int NEPA_state = 0;
int GEN_state = 0;
int SOLAR_state = 0;
int Last_NEPAstate = 0;
int Last_GENstate = 0;
int Last_SOLARstate = 0;
int sms_count = 0;

void setup()
{
   pinMode(NEPA, INPUT);
   pinMode(GEN, INPUT);
   pinMode(SOLAR, INPUT);
   Serial.begin(9600);
}

void loop()
{
    {NEPA_state = digitalRead(NEPA);
       if (NEPA_state != Last_NEPAstate)
       delay(3000);
       {check_NEPA();}
     }

    {GEN_state = digitalRead(GEN);
       if (GEN_state != Last_GENstate)
         delay(3000);
         {check_GEN();}
       }

      {SOLAR_state = digitalRead(SOLAR);
         if (SOLAR_state != Last_SOLARstate)
           delay(3000);
           {check_SOLAR();}
         }
    }
 
void check_NEPA()
{
   if (NEPA_state == HIGH)
      {
         sms_count++;
         Serial.println("NEPA_ON!");
         delay(1000);
         mySerial.println("AT+CMGF=1");
         delay(1000);
         mySerial.println("AT+CMGS=\"+2349073284790\"\r");
         delay(1000);
         mySerial.println("NEPA_ON!");
         delay(1000);
         mySerial.println((char)26);
         delay(3000);
       
  }
     else
        {
           Serial.println("NEPA_OFF!");
           delay(1000);
           mySerial.println("AT+CMGF=1");
           delay(1000);
           mySerial.println("AT+CMGS=\"+2349073284790\"\r");
           delay(1000);
           mySerial.println("NEPA_OFF!");
           delay(1000);
           mySerial.println((char)26);
           delay(3000);
         
    }
      Last_NEPAstate = NEPA_state;
}

void check_GEN()
{
   if (GEN_state == HIGH)
      {
         sms_count++;
        Serial.println("GEN_ON!");
        delay(1000);
        mySerial.println("AT+CMGF=1");
        delay(1000);
        mySerial.println("AT+CMGS=\"+2349073284790\"\r");
        delay(1000);
        mySerial.println("GEN_ON!");
        delay(1000);
        mySerial.println((char)26);
        delay(1000);
     
  }
    else
      {
          Serial.println("GEN_OFF!");
          delay(1000);
          mySerial.println("AT+CMGF=1");
          delay(1000);
          mySerial.println("AT+CMGS=\"+2349073284790\"\r");
          delay(1000);
          mySerial.println("GEN_OFF!");
          delay(5000);
          mySerial.println((char)26);
          delay(1000);
         
    }
     Last_GENstate = GEN_state;
   
}

void check_SOLAR()
{
   if (SOLAR_state == HIGH)
      {
         sms_count++;
         Serial.println("SOLAR_ON!");
         delay(1000);
         mySerial.println("AT+CMGF=1");
         delay(1000);
         mySerial.println("AT+CMGS=\"+2349073284790\"\r");
         delay(1000);
         mySerial.println("SOLAR_ON!");
         delay(5000);
         mySerial.println((char)26);
         delay(1000);
       
  }
     else
        {
           Serial.println("SOLAR_OFF!");
           delay(1000);
           mySerial.println("AT+CMGF=1");
           delay(1000);
           mySerial.println("AT+CMGS=\"+2349073284790\"\r");
           delay(1000);
           mySerial.println("NEPA_ON!");
           delay(1000);
           mySerial.println((char)26);
           delay(1000);
         
    }
     Last_SOLARstate = SOLAR_state;
}

The whole behaviour (infinite sms - undesired) when i included the sms codes to the first set of code.

Use the autoformat option to properly indent your code. I think it will make clear what is wrong.

sterretje:
Use the autoformat option to properly indent your code. I think it will make clear what is wrong.

i have just done that please!

{NEPA_state = digitalRead(NEPA);
       if (NEPA_state != Last_NEPAstate)
       delay(3000);
       {check_NEPA();}
     }

OK, learn what the { and the } are for. They are for grouping lines of code..

example:

if (something true or false) {

DO these lines in between the curly brackets if it was TRUE.
SKIP these lines in between the curly brackets if it was FALSE

}


or..

for (int i=1;1<=4;i++) {

   DO these lines in between the curly brackets 4 times.

}


void loop(void) {


   DO these lines in between the curly brackets when void is called.

}

Your brackets are scattered all around causing nothing but madness for the compiler. Use what you need and use them correctly.

-jim lee

Look at the first part of loop() after you use the autoformat option

  {
    NEPA_state = digitalRead(NEPA);
    if (NEPA_state != Last_NEPAstate)
      delay(3000);
    {
      check_NEPA();
    }
  }

The only line that has an extra indent is the delay line. That means that that is the only line that is conditionally executed; check_NEPA() is always executed.

So if the NEPA state has changed, you delay 3 seconds and next call check_NEPA(). If the NEPA state has not changed, you don't delay but still call check_NEPA(). You could just as well have left out the '{' and '}' before and after check_NEPA().

The solution is simple; move the delay(3000) to before check_NEPA(). Same applies to all your if's in loop().

Further your first '{' and last '}' are not needed. They do not harm (the compiler will handle it correctly) but they are confusing.

The revised version will look like

void loop()
{
  NEPA_state = digitalRead(NEPA);
  if (NEPA_state != Last_NEPAstate)
  {
    delay(3000);
    check_NEPA();
  }

Now both delay and check_NEPA() are inside the block that is executed if the condition is true. It can also clearly be seen from the indentation.