Notifications
Clear all

Creating a loop in Arduino IOT Cloud

10 Posts
4 Users
0 Likes
769 Views
(@bob2bot)
Member
Joined: 5 years ago
Posts: 5
Topic starter  

I enjoyed your tutorial on how to use the IOT cloud and created a simple dashboard that turns an LED on and off using the switch in the dashboard. I want the LED to turn on/off repeatedly but it only cycles once. How do I get it to loop continuously until the switch is turned off. Here is my code.

#include "arduino_secrets.h"
/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
   https://create.arduino.cc/cloud/things/789c16a8-a3fb-46f5-a10e-17215af4a0a4  

  Arduino IoT Cloud Variables description

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

  bool bUTTON;

  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"
int LEDPIN = 8;

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 
  
  


}
/*
  Since BUTTON is READ_WRITE variable, onBUTTONChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onBUTTONChange()  {
  // Add your code here to act upon BUTTON change
  Serial.println(bUTTON);
  if(bUTTON){
    digitalWrite(LEDPIN,HIGH);
    delay(2000);
    digitalWrite(LEDPIN,LOW);
    delay(2000);
  }else{
  digitalWrite(LEDPIN,LOW);
}
  
}
  




   
Quote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6963
 

@bob2bot You will need to change the logic so that the onButtonChange loops using a standard do while 1 type of construct (see arduino.cc docs) If you are going to use the same button to turn off the leds, then you need a standard state change statement like leds = -leds upon entry to the button change event.

You probably already know that code will get overwritten if you allow the automatic tools to have their way. Once you get it working as I described, you should decouple the onchange event and the bulk of the code. What I mean is put the looping and led on off code in it's own procedure NOT connected to any dashboard parts and the pushbutton code then just calls that new procedure each time it is pressed. Make the led state a local static init to off.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2527
 

Posted by: @bob2bot

I enjoyed your tutorial on how to use the IOT cloud and created a simple dashboard that turns an LED on and off using the switch in the dashboard. I want the LED to turn on/off repeatedly but it only cycles once. How do I get it to loop continuously until the switch is turned off. Here is my code.

 

As Ron explained, you'll need to change your code to perform the blinking while it waits for the next press of the button.

Add the following to the beginning of your sketch (with the other declarations)

bool            LED_FLASH = false;        // Is LED flashing (on)
unsigned long   lastFlash=0;              // Time flash started/ended
unsigned long   flashDuration = 500;      // Duration of each flash

Add this routine between setup() and loop()

void flashLED() {
  if (!bUTTON)                            // If button is off
    return;                               // Just go away
  //
  if (millis()-lastFlash>flashDuration) { // If state existed for duration
    LED_FLASH = !LED_FLASH;               // Change state
    digitalWrite(LEDPIN,LED_FLASH);       // Reset LED pin
    lastFlash = millis();                 // Reset time for new state
  }
}

Change your loop() to 

void loop() {
  flashLED();  
}

Change your button change routine to

/*
  Since BUTTON is READ_WRITE variable, onBUTTONChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onBUTTONChange() {
  // Add your code here to act upon BUTTON change
  Serial.println(bUTTON);
  if (bUTTON) {                             // If button is on
    lastFlash = millis();                   // Force flash to start
    LED_FLASH = HIGH;                       // Start with LED HIGH
  } else {
    digitalWrite(LEDPIN,LOW);
  }
}

And see if that does what you want. You can change the flash duration by setting flashDuration to the time delay you need.

 

 

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2527
 

@bob2bot

Ignore my post, Ron has explained to me that the onBUTTONChange routine is auto-generated and so can't be replaced as I had suggested 🙁

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
MrRemedy
(@mrremedy)
Developer, Engineer
Joined: 3 years ago
Posts: 24
 

@bob2bot 

Yup. basically Arduino has two main functions.  1 to set up and initialize everything and 1 to loop through a set of code.  "setup()" and "loop()".

Th On onBUTTONChange albeit part of a libray can be copied out into your loop() section where you can more control of the button presses.  There should be plenty of example in Googleland on how to create this functionality.  I'm not sure, but I'm not entirely sure if the micro controller actually has Interrupt control, but may be mimicked. 

But, I'd be curious what you find.   My lab is is undergoing renovations, so I'm separated from my toys.

 

-d


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6963
 

@mrremedy The onBUTTONChange procedure is NOT code the OP wrote, it is generated from the Arduino Cloud. Moving it will simply mean a second copy will then get created. He needs to change the object type to a simple bool and then in the loop do the led manipulation etc as I previously said.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@bob2bot)
Member
Joined: 5 years ago
Posts: 5
Topic starter  

Thanks guys, I will check out Google and other sources. -


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6963
 

@bob2bot Best to read the Arduino IOT docs first. This is a very different dev environment.

BTW, when replying, use the reply link at the bottom right of the post you are replying to, that way that person gets an email that they have a response. You can also add additional recipients via the standard @name paradigm.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@bob2bot)
Member
Joined: 5 years ago
Posts: 5
Topic starter  

I copied the if/else code from the onBUTTONChange function generated by the IOT cloud and pasted it into the loop function and it works fine. Thanks for your help guys. - Bob


   
ReplyQuote
MrRemedy
(@mrremedy)
Developer, Engineer
Joined: 3 years ago
Posts: 24
 

@Ron I stand corrected.  🙂

-d


   
ReplyQuote