# Cannot flash core when main loop code is long or busy

**URL:** <https://community.particle.io/t/cannot-flash-core-when-main-loop-code-is-long-or-busy/8576>\
**Category:** Troubleshooting\
**Created:** [December 3, 2014, 8:30pm UTC](https://community.particle.io/t/cannot-flash-core-when-main-loop-code-is-long-or-busy/8576 "2014-12-03T20:30:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![digixx](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/digixx/32/2831_2.png) [@digixx](https://community.particle.io/u/digixx)\
**Post date:** [December 3, 2014, 8:30pm UTC](https://community.particle.io/t/cannot-flash-core-when-main-loop-code-is-long-or-busy/8576/1 "2014-12-03T20:30:35Z")

</div>

I have two projects having problems when it comes to reflash the core. One is my [8x8x8 cube](https://community.spark.io/t/spark-io-is-a-great-replacement-for-arduino-projects/7711), running code to display 3D patterns. The main loop takes several seconds to display all patterns.

When I try to flash new code, the core stops somewhere, the LED is blinking cyan, and 2 seconds later the software continuous. At the end of the void(loop) procedure I call SPARK\_WLAN\_Loop() during 20ms.

I understand that the code inside void(loop) is just part of a bigger system. But what is the right strategy to keep the core responsive to the cloud and reflash?

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [December 3, 2014, 8:44pm UTC](https://community.particle.io/t/cannot-flash-core-when-main-loop-code-is-long-or-busy/8576/2 "2014-12-03T20:44:44Z")

</div>

I don't quite get this behaviour

> [@digixx](#):
>
> the core stops somewhere, the LED is blinking cyan, and 2 seconds later the software continuous

But the common practice is to ensure that `Spark.process()` or `SPARK_WLAN_Loop()` or `delay()` is called at least once every 10sec, to keep the cloud connection alive and the Core responsive for cloud actions - more often would be better and more responsive the Core will be.

---

<div class="post-metadata">

**Author:** ![harrisonhjones](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/harrisonhjones/32/4132_2.png) [@harrisonhjones](https://community.particle.io/u/harrisonhjones)\
**Post date:** [December 3, 2014, 9:35pm UTC](https://community.particle.io/t/cannot-flash-core-when-main-loop-code-is-long-or-busy/8576/3 "2014-12-03T21:35:29Z")

</div>

@ScruffR is correct. Perhaps you would consider adding a button or a 5ish second delay in the setup which would call SPARK\_WLAN\_Loop() so you would have some time to flash the core?

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [December 3, 2014, 9:40pm UTC](https://community.particle.io/t/cannot-flash-core-when-main-loop-code-is-long-or-busy/8576/4 "2014-12-03T21:40:35Z")

</div>

@digixx, you could also call SPARK\_WAN\_Loop() at various points in your loop(), or like @harrisonhjones eluded to, add a simple jumper (to GND) on a pin and in setup(), set the pin as an INPUT, read it and if it is LOW, go into an timed or infinite SPARK\_WAN\_Loop() loop to allow for OTA programming. I think someone had some code for that in another topic. 🙂

---

<div class="post-metadata">

**Author:** ![digixx](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/digixx/32/2831_2.png) [@digixx](https://community.particle.io/u/digixx)\
**Post date:** [December 4, 2014, 8:49pm UTC](https://community.particle.io/t/cannot-flash-core-when-main-loop-code-is-long-or-busy/8576/5 "2014-12-04T20:49:17Z")

</div>

I thought that the breathing LED means good connection to the WiFi / Cloud. So I changed the code and put a 10 second wait in setup() and run SPARK\_WLAN\_Loop() between every pattern. Now its no problem to reflash.

The other project is a bit different because the core is battery powered and it goes immediately to sleep when it has send the weather data to the webservice. If the response of the webservice is ‘$’, the core stays awake and run SPARK\_WLAN\_Loop() for the next 5 minutes. Unfortunately it does not recognize the ‘$’ anymore. The project stays in my backyard and I have to screw off the electronics for investigation. I have to say that the core now runs for more than 3 month without power off.

---

<div class="post-metadata">

**Author:** ![harrisonhjones](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/harrisonhjones/32/4132_2.png) [@harrisonhjones](https://community.particle.io/u/harrisonhjones)\
**Post date:** [December 5, 2014, 3:56am UTC](https://community.particle.io/t/cannot-flash-core-when-main-loop-code-is-long-or-busy/8576/6 "2014-12-05T03:56:27Z")

</div>

That’s fantastic (the battery life)!

When you say “it does not recognize the ‘$’ anymore” would you mind elaborating? Have you had a chance to see what the Core is getting back?

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [December 5, 2014, 2:32pm UTC](https://community.particle.io/t/cannot-flash-core-when-main-loop-code-is-long-or-busy/8576/7 "2014-12-05T14:32:32Z")

</div>

@digixx, when you say it runs for 3 months on battery, what is your battery setup - LiPo, solar?

---

<div class="post-metadata">

**Author:** ![digixx](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/digixx/32/2831_2.png) [@digixx](https://community.particle.io/u/digixx)\
**Post date:** [December 5, 2014, 11:50pm UTC](https://community.particle.io/t/cannot-flash-core-when-main-loop-code-is-long-or-busy/8576/8 "2014-12-05T23:50:18Z")

</div>

[This](https://community.spark.io/t/battery-powered-weather-station-with-solar-charging/6835) is the project I am talking about. You can find the code [here](https://github.com/digixx/Spark-Weather-Station)

Look at weather\_station.ino. At line 97 starts function updateDATA() which concatenates all sensor data to a string and send it with a GET to the webservice. By default, the webservice does not send any data back. I enable it manualy in the code of the webservice. If it sends a $ back, the core does not execute the sleep function and stays awake. At the moment this seams not to work. When I send sensor data from a browser, the $ is send back. Tomorrow, I will get the electronic on the bench and connect the serial debugger.

The 4 cell NiCd Battery is powered by solar cells. A power up/down converter from Pololu then produces 3.3V voltage for the core and the sensors.

Since several days without direct sunlight, the voltage from the battery has dropped from 5.6V to 5.0V. Also with very poor daylight, the solar cells produces still some milliamps. The voltage drop is about 0.08V per day. If the voltage get lower than 4.3V, sleeping interval is extended to 20min.

---

<div class="post-metadata">

**Author:** ![digixx](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/digixx/32/2831_2.png) [@digixx](https://community.particle.io/u/digixx)\
**Post date:** [December 7, 2014, 3:17pm UTC](https://community.particle.io/t/cannot-flash-core-when-main-loop-code-is-long-or-busy/8576/9 "2014-12-07T15:17:45Z")

</div>

I can flash the core now. I am not shure what the problem was.

The weather station now sends the brightness level into the cloud via spark.publish(). My MessageTorch receives the event and switches the lamp on/off if the brightness cross a certain level.
