Switch doesn't work with LED Widget

Hi,
I would like to create an application where I sequentially have 3 LED Widgets light up.
Basically every minute LED1 should light for 10 seconds, then in the next minute LED2 and finally in the other minute LED3.
As an example I started to use and modify the "Scheduler" sketch.
In the Dashboard I inserted the Scheduler Widget, the Time Picker Widget, the Value Widget and 3 LED Widgets.
This is the Dashboard:
Dashboard

As a Device I use a NANO33 IoT.
For the variables I use the ones that are in the table of the scheduler sketch, as well as the association of the variable to the Widget.
The only difference is that I could not find the value variable to associate with the Widget Value.
In fact I had to assign to the 3 LED Widgets the variable light, it does not let me associate another variable.
So I associated the counter variable to the Widget Value and it works but not the way I want.
The modified sketch is as follows:

#include "arduino_secrets.h"
/*
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/08b27dbe-a5e2-4b6d-b9b4-647b5227a6ce 

  Arduino IoT Cloud Variables description

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

  int counter;
  CloudSchedule scheduler_test;
  bool light;
  CloudTime time_read;

  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"
#include <RTCZero.h>

bool toggle;
int LED1;   
int LED2; 
int LED3;

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
  
  counter = 0;  // erogazioni crocchette
  light = false;
  //Serial.print("counter setup = ");
  //Serial.println(counter);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);
}

void loop() {
  ArduinoCloud.update();
  if(scheduler_test.isActive()) {
    light = true;
    digitalWrite(LED_BUILTIN, HIGH);
    
  if(toggle) {
    counter = ++counter;
    Serial.print("counter toggle = ");
    Serial.println(counter);
    toggle = false;
    }
  } 
  else {
    light = false;
    digitalWrite(LED_BUILTIN, LOW);
    toggle = true;
    }
  
  if(ArduinoCloud.connected()) {
    time_read = ArduinoCloud.getLocalTime();
    }
  
 }

void onSchedulerTestChange()  {
}

/*
  Since Counter is READ_WRITE variable, onCounterChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onCounterChange()  {
    Serial.print("light change = ");
    Serial.print(light);
    Serial.print("  counter = ");
    Serial.println(counter);
    }

/*
  Since Light is READ_WRITE variable, onLightChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onLightChange()  {
}

/*
  Since ValoreLed is READ_WRITE variable, onValoreLedChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onValoreLedChange()  {
   switch (counter) {
    case 2:
      digitalWrite(LED1, HIGH);
      break;
    case 4:
      digitalWrite(LED2, HIGH);
      break;
    case 6:
      digitalWrite(LED3, HIGH);
      break;
    case 8:
      digitalWrite(LED1, LOW);
      digitalWrite(LED2, LOW);
      digitalWrite(LED3, LOW);
      break;
  } 
}

This is my first attempt to create something and learn how to use the Arduino Cloud.
I welcome advice

EzioGi

Hi!

According to your sketch, the callback function onValoreLedChange is never going to be called, as there is no variable defined for it. Can you please double check if it has a variable associated or if the name of the function is wrong?

Hi,
I am using the variable "counter" that I generated via the "Things" and which is associated with the "Value" widget.
The function "onValueLedChange is automatically generated and I think it is correct as it appears to me in the sketch.

EzioGi

I solved it by editing the sketch again.
As a project development I need to have three alerts daily, at 8 a.m., 2 p.m. and 8 p.m. to enable an engine.
How do I insert a variable that reads me the hours so that I can then decide what to do at the chosen times ( 8,14,20) ?

EzioGi

You can use the ArduinoCloud.getLocalTime() function.

You can find more information here.

I read the information you recommended but for the knowledge I have, it is too little.
I read that you can use the function "time.h" which is not very explanatory to me because I don't know which internal function to use to transform Unix time into standard time (hours, minutes, seconds, etc.).
I've also searched the internet but I haven't found any explanatory and simple examples to insert in my sketch.

EzioGi

That's quite straighforward.

Try the following:

unsigned long now = ArduinoCloud.getLocalTime();
time_t now_time = (time_t)now;
struct tm* timeinfo = localtime(&now_time);
// One example
Serial.println("now:" +
   String(timeinfo->tm_year + 1900) + "-" +String(timeinfo->tm_mon + 1) + "-" + String(timeinfo->tm_mday) +
   " " + String(timeinfo->tm_hour) + ":" + String(timeinfo->tm_min) + ":" + String(timeinfo->tm_sec));

There are many pages describing how to use time.h conversion functions. Check this one for instance: C Library -.

I will try it in the sketch as soon as I can.
Thanks

EzioGi

Solved with the help dbeamonte gave me.
Thank you

EzioGi

1 Like

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