# Delay() needed in loop?

**URL:** <https://community.particle.io/t/delay-needed-in-loop/4860>\
**Category:** Firmware\
**Created:** [June 5, 2014, 9:07am UTC](https://community.particle.io/t/delay-needed-in-loop/4860 "2014-06-05T09:07:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zach\_l](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/zach_l/32/1346_2.png) [@zach\_l](https://community.particle.io/u/zach_l)\
**Post date:** [June 5, 2014, 9:07am UTC](https://community.particle.io/t/delay-needed-in-loop/4860/1 "2014-06-05T09:07:35Z")

</div>

I’m running a cloud disabled system and had a small doubt.

Does my loop() need some sort of delay? I can’t seem to trace how functions like SPARK\_WLAN\_Loop() get called otherwise once I am connected (I see them getting called while it’s being setup). I’m reading through the main.cpp and how the application loop() gets called.

To be fair I am seeing the light change with connection and disconnection, but I just don’t see how this SPARK\_WLAN\_Loop is getting called in a connected mode with a tight (no delay) loop.

---

<div class="post-metadata">

**Author:** ![kennethlimcp](https://avatars.discourse-cdn.com/v4/letter/k/c0e974/32.png) [@kennethlimcp](https://community.particle.io/u/kennethlimcp)\
**Post date:** [June 5, 2014, 10:16am UTC](https://community.particle.io/t/delay-needed-in-loop/4860/2 "2014-06-05T10:16:10Z")

</div>

Nope you don’t have to worry.

The actual program is actually more than just setup() and loop() where there are a couple of flags and checks done invisible to the user

Basically, it gets called automatically depending on what you do with the connections.

Hope this helps! 😉

---

<div class="post-metadata">

**Author:** ![zach\_l](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/zach_l/32/1346_2.png) [@zach\_l](https://community.particle.io/u/zach_l)\
**Post date:** [June 5, 2014, 11:31am UTC](https://community.particle.io/t/delay-needed-in-loop/4860/3 "2014-06-05T11:31:56Z")

</div>

thanks - I’ve been following those flags (I’m reading through main) but I still don’t see how if I am connected and not in setup SPARK\_WLAN\_Loop gets called:

[https://github.com/spark/core-firmware/blob/master/src/main.cpp#L172-L207](https://github.com/spark/core-firmware/blob/master/src/main.cpp#L172-L207)

can you help me understand that? is loop here not calling application loop?

---

<div class="post-metadata">

**Author:** ![Moors7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/moors7/32/588_2.png) [@Moors7](https://community.particle.io/u/Moors7)\
**Post date:** [June 5, 2014, 11:42am UTC](https://community.particle.io/t/delay-needed-in-loop/4860/4 "2014-06-05T11:42:03Z")

</div>

I’m not entirely sure, so please correct me if I’m wrong. The way I understood it was as following: the whole Spark firmware runs as a continuous loop. Inside that loop is your sketch, which can loop as well. By defining whether or not to enable/disable wifi (or other settings for that matter) inside your sketch, you’re actually toggling them on/off inside the firmware loop.  
That’s my general concept of how it works, but as I’ve said, I could be wrong.

---

<div class="post-metadata">

**Author:** ![zach\_l](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/zach_l/32/1346_2.png) [@zach\_l](https://community.particle.io/u/zach_l)\
**Post date:** [June 5, 2014, 11:55am UTC](https://community.particle.io/t/delay-needed-in-loop/4860/5 "2014-06-05T11:55:12Z")

</div>

@Moors7 thanks - I figured as much too.

I think I might have misread this SPARK\_WLAN\_SETUP as a variable showing if we are in a sort of “setup” mode, it seems on closer reading to represent that we are already setup so main makes more sense. I think these logic variable can be a little tricky to parse as some seems to be flags, others are state.

---

<div class="post-metadata">

**Author:** ![BDub](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bdub/32/5137_2.png) [@BDub](https://community.particle.io/u/BDub)\
**Post date:** [June 5, 2014, 8:46pm UTC](https://community.particle.io/t/delay-needed-in-loop/4860/6 "2014-06-05T20:46:09Z")

</div>

If you are wondering how the RGB led is updated if you are Cloud disabled, it gets updated via a background Interrupt Service Routine. This has nothing to do with SPARK\_WLAN\_Loop();

Speaking of that though… here’s some code that’s useful if you are doing serial debugging and bouncing back and fourth between cloud enabled and cloud disabled:

```cpp
// Uncomment to disable the WiFi and ultimately the Cloud connection as well
//#include "spark_disable_wlan.h"

void setup(void) {
  Serial.begin(115200); // Make sure your serial terminal is closed before power the Core.
  while(!Serial.available()) { // Open serial terminal and Press ENTER.
    #ifdef SPARK_WLAN_SETUP
      SPARK_WLAN_Loop(); // Only process b/g tasks if the WiFi is enabled
    #endif
  }
  Serial.print("Hello, Sparky!");
}

```
