Programmatically change switch state in Arduino IOT Cloud

Hello everybody

I'm new to Arduino IOT Cloud and I have a question please

I created my first dashboard with only 1 switch and I would like to know if it is possible to change the state of this switch through the sketch, that is, if my switch is in "on", how would the command inside the sketch to change the switch to "off" or if my switch is "off" how to change through sketch to "on" ?

I want to change the switch programmatically through the sketch and not using the dashboard

My switch is declared like this:

CloudSwitch lamp;

Thank you all for the attention

Declare the lamp variable in your Thing setup. The switch widget takes a Boolean.

If the lamp variable is configured as Read/Write and OnChange a simple assignment to it within your sketch will be reflected in the state of the switch in the dashboard shortly afterwards.

Here is a modified Blink that will change the switch every second.

  currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

    lamp = !lamp; // This assignment will be reflected in the dashboard switch state
  }