Impossible to switch off a Cloud Switch

Hi everybody,
I'm new to the Arduino IoT Cloud part and need help with my project.
I'm setting up a NeoPixel ring of 21 elements connected to an ESP-01 (Generic ESP8266 module) to use with Alexa.
Also setting up 2 way to use it:

  1. RGB lamp via the Color Led widget (works quite fine) (variable "sale", it's a salt lamp with salt translated in italian);
  2. Sunset simulator to vocally activate via Alexa before sleep (got problems) (variable buonanotte, goodnight in italian).
    Pasting the code before explaining problems.
// Adafruit NeoPixel - Version: 1.8.4
#include <Adafruit_NeoPixel.h>

/*
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/e00f4460-a39d-4f1b-873a-01bbeb18adbc

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  CloudColoredLight sale;
  CloudSwitch buonanotte;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
#define PIN 2
#define NUMPIXELS 21  // Popular NeoPixel ring size
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
unsigned long timerBuonanotte;

void setup() {
  initProperties();
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  ArduinoCloud.printDebugInfo();
  pixels.begin();
  pixels.fill(pixels.Color(0, 0, 0), 0, NUMPIXELS);
  pixels.show();
}

void loop() {
  ArduinoCloud.update();
  if (millis() - timerBuonanotte >= 10000){
    buonanotte = false;
  }
}

void onBuonanotteChange() {
  if (buonanotte == true) {
    timerBuonanotte = millis();
    Buonanotte();
    buonanotte =  false;
  } else {
    //the following code simply turns everything off
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.fill(pixels.Color(0, 0, 0), 0, NUMPIXELS);
    pixels.show();  // This sends the updated pixel color to the hardware.
  }
}

int j = 0;

void Buonanotte() {
  j = 0;
  int g = 50;
  int r = 255;
  pixels.setBrightness(255);
  buonanotte = false;
  for (j; j < 100; j++) {
    g = map(j, 0, 100, 50, 0);
    pixels.fill(pixels.Color(r, g, 0), 0, NUMPIXELS);
    pixels.show();
    delay(235); //will be 2350, reduced for testing
    Serial.println(j);
  }
  for (j; j < 255; j++) {
    r = map(j, 100, 255, 255, 0);
    pixels.fill(pixels.Color(r, 0, 0), 0, NUMPIXELS);
    pixels.show();
    delay(235); //will be 2350, reduced for testing
    Serial.println(j);
  }
  pixels.fill(pixels.Color(0, 0, 0), 0, NUMPIXELS);
  pixels.show();
  delay(10000);
}

void onSaleChange() {
  uint8_t r, g, b;
  sale.getValue().getRGB(r, g, b);
  if (sale.getSwitch()) {
    Serial.println("R:" + String(r) + " G:" + String(g) + " B:" + String(b));  //prints the current R, G, B values
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.fill(pixels.Color(r, g, b), 0, NUMPIXELS);
    pixels.show();
  } else {
    Serial.println("Lamp Off");
    //the following code simply turns everything off
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.fill(pixels.Color(0, 0, 0), 0, NUMPIXELS);
    pixels.show();  // This sends the updated pixel color to the hardware.
  }
}

Problem n.1: buonanotte don't stop regardless the redundancy of buonanotte = false; in the sketch.
Problem n.2: I'd like to switch off "sale" when I switch on "buonanotte", but I don't know the command. I already tried to add sale.setSwitch(false); in the void Buonanotte() but nothing happened

I could solve both with an Alexa routine, but I'd like to have the sketch completely working on itself.

Thanks for help

Your Buonanotte() function contains "blocking code" (code which won't let other parts of your sketch run, for example delay(), especially delay() inside for-loops). Could that be the cause?

Actually there is delay(), but I previously tried with the

If (millis() - last >= 235) {
  last = millis();
//…
}

the only change I found is that manual switchOff is not (obviously) working, but the aim is to fall asleep during Buonanotte()
No differences

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