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

  • Thread starter Deleted member 750607
  • Start date

Thread Starter

Deleted member 750607

Joined Dec 31, 1969
0
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 by a moderator:

Thread Starter

Deleted member 750607

Joined Dec 31, 1969
0
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);

}
 

Ya’akov

Joined Jan 27, 2019
9,070
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

Deleted member 750607

Joined Dec 31, 1969
0
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?
 

Ya’akov

Joined Jan 27, 2019
9,070
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

Deleted member 750607

Joined Dec 31, 1969
0
So what should the proper code read next to the "void onLEDSwitchChange()" section...or loop
 
Last edited by a moderator:

Thread Starter

Deleted member 750607

Joined Dec 31, 1969
0
C++:
void onLedSwitchChange () {
if (led_switch) {  // executes if true
digitalWrite (LEDpin, HIGH);
} else {           //executes if false
digitalWrite (LEDpin, LOW);
}
}
I copy and pasted...its not compiling
 

Thread Starter

Deleted member 750607

Joined Dec 31, 1969
0
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

Deleted member 750607

Joined Dec 31, 1969
0
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
 

Ya’akov

Joined Jan 27, 2019
9,070
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.
 

Ya’akov

Joined Jan 27, 2019
9,070
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

Deleted member 750607

Joined Dec 31, 1969
0
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
 

Ya’akov

Joined Jan 27, 2019
9,070
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

Deleted member 750607

Joined Dec 31, 1969
0
im a little confuse, could you show me the proper code?
 

Thread Starter

Deleted member 750607

Joined Dec 31, 1969
0
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
 

Ya’akov

Joined Jan 27, 2019
9,070
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