# Managing void loop

**URL:** <https://forum.arduino.cc/t/managing-void-loop/427556>\
**Category:** Programming\
**Created:** [December 29, 2016, 5:20am UTC](https://forum.arduino.cc/t/managing-void-loop/427556 "2016-12-29T05:20:56Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mfonobong](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@mfonobong](https://forum.arduino.cc/u/mfonobong)\
**Post date:** [December 29, 2016, 5:20am UTC](https://forum.arduino.cc/t/managing-void-loop/427556/1 "2016-12-29T05:20:56Z")

</div>

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

```cpp
#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](https://forum.arduino.cc/uploads/short-url/vaTlsX91iahJWfAHNUKMnGIRjrc.ino) (1.42 KB)

---

<div class="post-metadata">

**Author:** ![Power\_Broker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/power_broker/32/226104_2.png) [@Power\_Broker](https://forum.arduino.cc/u/Power_Broker)\
**Post date:** [December 29, 2016, 5:26am UTC](https://forum.arduino.cc/t/managing-void-loop/427556/2 "2016-12-29T05:26:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [December 29, 2016, 5:30am UTC](https://forum.arduino.cc/t/managing-void-loop/427556/3 "2016-12-29T05:30:03Z")

</div>

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 '=='.

```cpp
If (new_state = old_state)

```

You are assigning the value of old\_state to new\_state, you're not comparing.

---

<div class="post-metadata">

**Author:** ![mfonobong](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@mfonobong](https://forum.arduino.cc/u/mfonobong)\
**Post date:** [December 29, 2016, 5:36am UTC](https://forum.arduino.cc/t/managing-void-loop/427556/4 "2016-12-29T05:36:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [December 29, 2016, 8:26am UTC](https://forum.arduino.cc/t/managing-void-loop/427556/5 "2016-12-29T08:26:21Z")

</div>

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

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

}

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

}

```

Tom.. 🙂

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 29, 2016, 8:28am UTC](https://forum.arduino.cc/t/managing-void-loop/427556/6 "2016-12-29T08:28:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [December 29, 2016, 8:35am UTC](https://forum.arduino.cc/t/managing-void-loop/427556/7 "2016-12-29T08:35:21Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [December 29, 2016, 8:50am UTC](https://forum.arduino.cc/t/managing-void-loop/427556/8 "2016-12-29T08:50:55Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![mfonobong](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@mfonobong](https://forum.arduino.cc/u/mfonobong)\
**Post date:** [December 29, 2016, 7:11pm UTC](https://forum.arduino.cc/t/managing-void-loop/427556/9 "2016-12-29T19:11:21Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![MarkT](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@MarkT](https://forum.arduino.cc/u/MarkT)\
**Post date:** [December 29, 2016, 9:48pm UTC](https://forum.arduino.cc/t/managing-void-loop/427556/10 "2016-12-29T21:48:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [December 29, 2016, 10:09pm UTC](https://forum.arduino.cc/t/managing-void-loop/427556/11 "2016-12-29T22:09:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![PaulMurrayCbr](https://avatars.discourse-cdn.com/v4/letter/p/74df32/32.png) [@PaulMurrayCbr](https://forum.arduino.cc/u/PaulMurrayCbr)\
**Post date:** [December 30, 2016, 3:08am UTC](https://forum.arduino.cc/t/managing-void-loop/427556/12 "2016-12-30T03:08:53Z")

</div>

> 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 🙂

---

<div class="post-metadata">

**Author:** ![mfonobong](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@mfonobong](https://forum.arduino.cc/u/mfonobong)\
**Post date:** [December 30, 2016, 6:01pm UTC](https://forum.arduino.cc/t/managing-void-loop/427556/13 "2016-12-30T18:01:08Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [December 30, 2016, 6:03pm UTC](https://forum.arduino.cc/t/managing-void-loop/427556/14 "2016-12-30T18:03:46Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![mfonobong](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@mfonobong](https://forum.arduino.cc/u/mfonobong)\
**Post date:** [December 30, 2016, 6:11pm UTC](https://forum.arduino.cc/t/managing-void-loop/427556/15 "2016-12-30T18:11:09Z")

</div>

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

OK thanks

---

<div class="post-metadata">

**Author:** ![mfonobong](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@mfonobong](https://forum.arduino.cc/u/mfonobong)\
**Post date:** [December 31, 2016, 6:13pm UTC](https://forum.arduino.cc/t/managing-void-loop/427556/16 "2016-12-31T18:13:04Z")

</div>

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.

```cpp
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:

```cpp
#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.

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [December 31, 2016, 8:00pm UTC](https://forum.arduino.cc/t/managing-void-loop/427556/17 "2016-12-31T20:00:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mfonobong](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@mfonobong](https://forum.arduino.cc/u/mfonobong)\
**Post date:** [December 31, 2016, 8:41pm UTC](https://forum.arduino.cc/t/managing-void-loop/427556/18 "2016-12-31T20:41:14Z")

</div>

> 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!

---

<div class="post-metadata">

**Author:** ![jimLee](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jimlee/32/214865_2.png) [@jimLee](https://forum.arduino.cc/u/jimLee)\
**Post date:** [January 1, 2017, 2:38am UTC](https://forum.arduino.cc/t/managing-void-loop/427556/19 "2017-01-01T02:38:05Z")

</div>

```cpp
{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:

```cpp
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

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [January 1, 2017, 5:35am UTC](https://forum.arduino.cc/t/managing-void-loop/427556/20 "2017-01-01T05:35:21Z")

</div>

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

```cpp
  {
    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

```cpp
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.

[Next page](https://forum.arduino.cc/t/managing-void-loop/427556.md?page=2)
