Same code doesn't work when using IoT Cloud

Hello all,

I have a small code which makes a LED ring light up in rainbow colors as long as a sensor is touched.

// FastLED - Version: Latest 
#include <FastLED.h>
#define NUM_LEDS 24
#define DATA_PIN 12
CRGB leds[NUM_LEDS];

// CapacitiveSensor - Version: Latest 
#include <CapacitiveSensor.h>

CapacitiveSensor sensor = CapacitiveSensor(2,10);
uint8_t hue;

void setup() {
  
  Serial.begin(9600);
  delay(1500); 
  
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  FastLED.clear();
  FastLED.show();
}

void loop() {
  while(isTouching()) {
    Serial.println(hue);
    fill_solid(leds, NUM_LEDS, CHSV(hue, 255, 255));
    FastLED.show();
    hue++;
    delay(50);
  }
  delay(50);
}

boolean isTouching() {
  if(sensor.capacitiveSensor(30) > 5000) return true;
  return false;
}

This code works 100% when uploaded to the Nano 33 IoT using the Arudino IDE.

Now I tried to implement the same code using the IoT Cloud (to match colors later for example).

// FastLED - Version: Latest 
#include <FastLED.h>
#define NUM_LEDS 24
#define DATA_PIN 12
CRGB leds[NUM_LEDS];

// CapacitiveSensor - Version: Latest 
#include <CapacitiveSensor.h>

#include "thingProperties.h"

CapacitiveSensor sensor = CapacitiveSensor(2,10);
uint8_t hue;

void setup() {
  
  Serial.begin(9600);
  delay(1500); 
  initProperties();
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  setDebugMessageLevel(4);
  ArduinoCloud.printDebugInfo();
  
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  FastLED.clear();
  FastLED.show();
}

void loop() {
  ArduinoCloud.update();
  
  while(isTouching()) {
    Serial.println(hue);
    fill_solid(leds, NUM_LEDS, CHSV(hue, 255, 255));
    FastLED.show();
    hue++;
    delay(50);
  }
  delay(50);
}

boolean isTouching() {
  if(sensor.capacitiveSensor(30) > 5000) return true;
  return false;
}

Unfortunately, this code no longer works when uploaded to the Nano 33 IoT via the IoT Cloud.

The output in the Serial Monitor shows numbers from 0-255, then from 0 to about 20 and then the Arduino just restarts.

Why does the Nano 33 IoT restart every time it uses the IoT Cloud? How can I work around this?

Hi @alondattner
I would suggest you remove the while and delays from your code.
You are effectively stopping the Nano communicating with the Cloud as it cannot do anything whilst being locked in delays and while loops.
I guess it is crashing because of buffer or stack overflow due to it not able to service the cloud communictions

Hi @mick3000,
thank you very much for your tips. I have removed all while loops and delays and the code works now. I was not aware that these would cause such problems. Using the millis() function makes the code a bit more complex, but at least everything works now.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.