Arduino IoT - simplest way to let LED or lamp STAY on instead of blinking on/off

Thread Starter

hbasile91

Joined Dec 6, 2020
62
I've seen plenty of solutions to this problem online, but I can't find a way to do it that doesn't involve using a physical button...my goal is to switch on and off via dashboard widget in Arduino IoT Cloud...not a physical switch...(so there's no input pin)

I have been able to code my MKR WiFi 1010 and LED to turn and off via the dashboard widget (switch) but only as a blink function...meaning that when the "delay" time is over, the LED turns off

I can't figure out how to make it STAY on, and only turn off when I hit the on/off switch on my dashboard widget...

my goal is to switch a lamp on/off with my phone or computer. I started with an LED to write the code first.
THANKS IN ADVANCE!
 
Last edited:

Yaakov

Joined Jan 27, 2019
2,358
Showing your code would help. Use code tags to make sure the formatting is preserved.

The </> code option...

1617662410232.png
 

Thread Starter

hbasile91

Joined Dec 6, 2020
62
Code:
/*
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/74a37c64-d1a9-4e93-b650-038c342e6224

  Arduino IoT Cloud Variables description

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

  CloudLight lEDSwitch;

  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"
const int LEDpin = 6;

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);
  pinMode (LEDpin, OUTPUT);
  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
 
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();
  // Your code here
  digitalWrite (LEDpin, LOW);
}
void onLEDSwitchChange()
{
  // Do something
  digitalWrite (LEDpin, HIGH);
  delay (6000);

}
 

Yaakov

Joined Jan 27, 2019
2,358
Code:
/*
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/74a37c64-d1a9-4e93-b650-038c342e6224

  Arduino IoT Cloud Variables description

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

  CloudLight lEDSwitch;

  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"
const int LEDpin = 6;

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);
  pinMode (LEDpin, OUTPUT);
  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
*/
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();
  // Your code here
  digitalWrite (LEDpin, LOW);
}
void onLEDSwitchChange()
{
  // Do something
  digitalWrite (LEDpin, HIGH);
  delay (6000);

}
The LED turns off because loop() executes forever and you have digitalWrite (LEDpin, LOW) at the beginning of it. You should only have the loop execute things, on or off, in your onLEDSwitchChange() function.
 

Thread Starter

hbasile91

Joined Dec 6, 2020
62
Ok, thank you. that makes sense, simple, although I wouldn't have thought of it! I knew there had to be a simple solution...

I have read a short intro to arduino programming book, and the rest im sort of improvising. are there any sites or books you would recommend?
 

Yaakov

Joined Jan 27, 2019
2,358
Ok, thank you. that makes sense, simple, although I wouldn't have thought of it! I knew there had to be a simple solution...

I have read a short intro to arduino programming book, and the rest im sort of improvising. are there any sites or books you would recommend?
There are quite a few channels on YouTube with decent content. This is pretty comprehensive for a starter:

 

Thread Starter

hbasile91

Joined Dec 6, 2020
62
So what should the proper code read next to the "void onLEDSwitchChange()" section...or loop
 
Last edited:

Yaakov

Joined Jan 27, 2019
2,358
So what should the proper code read next to the "void onLEDSwitchChange() section"
C++:
void onLedSwitchChange () {
if (led_switch) {  // executes if true
digitalWrite (LEDpin, HIGH);
} else {           //executes if false
digitalWrite (LEDpin, LOW);
}
}
 

Thread Starter

hbasile91

Joined Dec 6, 2020
62
Just says, "Error: Verifying Untitled_apr05a"



Start verifying /tmp/539389984/Untitled_apr05a/Untitled_apr05a.ino: In function 'void onLedSwitchChange()': /tmp/539389984/Untitled_apr05a/Untitled_apr05a.ino:50:5: error: 'led_switch' was not declared in this scope if (led_switch) { // executes if true ^~~~~~~~~~ /tmp/539389984/Untitled_apr05a/Untitled_apr05a.ino:50:5: note: suggested alternative: 'lEDSwitch' if (led_switch) { // executes if true ^~~~~~~~~~ lEDSwitch exit status 1 /tmp/539389984/Untitled_apr05a/Untitled_apr05a.ino: In function 'void onLedSwitchChange()': /tmp/539389984/Untitled_apr05a/Untitled_apr05a.ino:50:5: error: 'led_switch' was not declared in this scope if (led_switch) { // executes if true ^~~~~~~~~~ /tmp/539389984/Untitled_apr05a/Untitled_apr05a.ino:50:5: note: suggested alternative: 'lEDSwitch' if (led_switch) { // executes if true ^~~~~~~~~~ lEDSwitch exit status 1
 

Thread Starter

hbasile91

Joined Dec 6, 2020
62
Just says, "Error: Verifying Untitled_apr05a"



Start verifying /tmp/539389984/Untitled_apr05a/Untitled_apr05a.ino: In function 'void onLedSwitchChange()': /tmp/539389984/Untitled_apr05a/Untitled_apr05a.ino:50:5: error: 'led_switch' was not declared in this scope if (led_switch) { // executes if true ^~~~~~~~~~ /tmp/539389984/Untitled_apr05a/Untitled_apr05a.ino:50:5: note: suggested alternative: 'lEDSwitch' if (led_switch) { // executes if true ^~~~~~~~~~ lEDSwitch exit status 1 /tmp/539389984/Untitled_apr05a/Untitled_apr05a.ino: In function 'void onLedSwitchChange()': /tmp/539389984/Untitled_apr05a/Untitled_apr05a.ino:50:5: error: 'led_switch' was not declared in this scope if (led_switch) { // executes if true ^~~~~~~~~~ /tmp/539389984/Untitled_apr05a/Untitled_apr05a.ino:50:5: note: suggested alternative: 'lEDSwitch' if (led_switch) { // executes if true ^~~~~~~~~~ lEDSwitch exit status 1
oh and I did change the LED-switch to LEDswitch, went back to show you...but same error message plus this:


Start verifying /tmp/441215139/Untitled_apr05a/Untitled_apr05a.ino: In function 'void onLedSwitchChange()': /tmp/441215139/Untitled_apr05a/Untitled_apr05a.ino:50:5: error: 'LEDswitch' was not declared in this scope if (LEDswitch) { // executes if true ^~~~~~~~~ /tmp/441215139/Untitled_apr05a/Untitled_apr05a.ino:50:5: note: suggested alternative: 'lEDSwitch' if (LEDswitch) { // executes if true ^~~~~~~~~ lEDSwitch exit status 1 /tmp/441215139/Untitled_apr05a/Untitled_apr05a.ino: In function 'void onLedSwitchChange()': /tmp/441215139/Untitled_apr05a/Untitled_apr05a.ino:50:5: error: 'LEDswitch' was not declared in this scope if (LEDswitch) { // executes if true ^~~~~~~~~ /tmp/441215139/Untitled_apr05a/Untitled_apr05a.ino:50:5: note: suggested alternative: 'lEDSwitch' if (LEDswitch) { // executes if true ^~~~~~~~~ lEDSwitch exit status 1
 

Yaakov

Joined Jan 27, 2019
2,358
Just says, "Error: Verifying Untitled_apr05a"



Start verifying /tmp/539389984/Untitled_apr05a/Untitled_apr05a.ino: In function 'void onLedSwitchChange()': /tmp/539389984/Untitled_apr05a/Untitled_apr05a.ino:50:5: error: 'led_switch' was not declared in this scope if (led_switch) { // executes if true ^~~~~~~~~~ /tmp/539389984/Untitled_apr05a/Untitled_apr05a.ino:50:5: note: suggested alternative: 'lEDSwitch' if (led_switch) { // executes if true ^~~~~~~~~~ lEDSwitch exit status 1 /tmp/539389984/Untitled_apr05a/Untitled_apr05a.ino: In function 'void onLedSwitchChange()': /tmp/539389984/Untitled_apr05a/Untitled_apr05a.ino:50:5: error: 'led_switch' was not declared in this scope if (led_switch) { // executes if true ^~~~~~~~~~ /tmp/539389984/Untitled_apr05a/Untitled_apr05a.ino:50:5: note: suggested alternative: 'lEDSwitch' if (led_switch) { // executes if true ^~~~~~~~~~ lEDSwitch exit status 1
Ah, led_switch is meant to hold the state of the switch. I don't know the Arduino cloud stuff so the onLEDSwitchChange() function is a bit of a mystery to me. You need led_switch to hold the state.

I tried to find the function but I couldn't find a reference, the code that uses it uses that variable, I had hoped it was defined in the library. The cloud thing adds a layer of confusion for me because I don't use it.
 

Yaakov

Joined Jan 27, 2019
2,358
Basically, I have no idea how onLEDSwitchChange() gets called or what is passed to it. The led_switch variable was the only thing I could find.
 

Thread Starter

hbasile91

Joined Dec 6, 2020
62
Ah, led_switch is meant to hold the state of the switch. I don't know the Arduino cloud stuff so the onLEDSwitchChange() function is a bit of a mystery to me. You need led_switch to hold the state.

I tried to find the function but I couldn't find a reference, the code that uses it uses that variable, I had hoped it was defined in the library. The cloud thing adds a layer of confusion for me because I don't use it.

I made the variable "LEDswitch". There is a set your own variables page in the setup for the sketch...then the cloud writes that part of the code for you and you use "void onLEDSwitchChange ()" to write the code for what your online widgets do
 

Yaakov

Joined Jan 27, 2019
2,358
I made the variable "LEDswitch". There is a set your own variables page in the setup for the sketch...then the cloud writes that part of the code for you and you use "void onLEDSwitchChange ()" to write the code for what your online widgets do
Then you just need LEDSwitch to contain the state of the switch. It has to evaluate true for on and false for off. Then that code should do what you want.
 

Thread Starter

hbasile91

Joined Dec 6, 2020
62
you go to the dashboards page of the Arduino IoT Cloud, or onto the IoT remote app on your smartphone

then there is a widget that has already been paired to your LEDSwitch variable. its an on/off switch.

I would like the LED to turn on/off according to when I change the on/off switch
 

Yaakov

Joined Jan 27, 2019
2,358
you go to the dashboards page of the Arduino IoT Cloud, or onto the IoT remote app on your smartphone

then there is a widget that has already been paired to your LEDSwitch variable. its an on/off switch.

I would like the LED to turn on/off according to when I change the on/off switch
Then as long as that variable exists, the code should work. There are still compilation errors?
 
Top