New to Arduino IOT, simple button problem

Hi, I have just found the Arduino IOT after loosing my way with Blynk 2.0. This is looking easier I have to say so I want to persevere with this system. I am not new to Arduino, I just enjoy making things work. But I have a dilema. I am using a nodeMCU, which so far I have managed to turn a led on and off using the Arduino Remote app and of course the PC that I create the 'Thing' with. What I am trying to achieve now is, having a button connected to the nodeMCU, either show a virtual led or update the status led variable in the app (or PC). I have got the button to light the on board led when pressed (nodeMCU) but nothing happens to the virtual led or status led. The board is online according to the Device info. Please see the code I am currently using, many thanks, Alistair.

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/ff597e92-5ab7-4134-b6f4-57fe634001ac 

  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 Button = 2;
 int statusVariable = 0;
 int buttonState = 0;
 
 
 
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(Button, INPUT_PULLUP);
  // 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();
  statusVariable = false;
  
  }
  
  
/*
  Since Button is READ_WRITE variable, onButtonChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onButtonChange()  {
  
  statusVariable = true;
  
    
    
  }

END

You should do something like

void loop() {
  ArduinoCloud.update();
  button = digitalRead(Button);
}
1 Like

Hi Fabrizio, thank you for replying, your code didn't work at first, but an hour or so later something clicked into place. The only slight thing now is, with the statusVariable, when the button is pushed (D4 to ground) the status reads False and I would like it to read True. I have played with the code by moving the True and False variables around. I even found online the ! (reverse boolean) before digitalRead and the code just stops working, have you any ideas please? Here's the code that's current working: Thank you.

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/ff597e92-5ab7-4134-b6f4-57fe634001ac 

  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 Button = 2;
 int ledVariable  = 0;
 
 
 
 
 
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(Button, INPUT);
  // 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();
  button = digitalRead(Button);
  
  ledVariable = true;
  
  }

  

  
  
  
  
  
  
/*
  Since Button is READ_WRITE variable, onButtonChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onButtonChange()  {
  
  ledVariable = false;

  }

button is pushed (D4 to ground) the status reads False and I would like it to read True

Try doing

button = !digitalRead(Button);

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