LED Counter Issue from Arduino Example

Hello All,

I'm trying to get into the Arduino IoT and I'm using the cloud web editor to build a dashboard for an Arduino OPTA that I have installed some 300 km away, so I am using over the air option.

I started to test my hardware by using the Opta example codes, the second example HERE which is controlling the PLC LED through the PLC button. I have created a dashboard button and I copied the same code as for the physical button, but it seems that every time I press the "virtual" button, the number of LED's increases by 2 and not by one. I have a WiFi Video Camera and I can see it happening. I do not understand why is increasing the "counter" by two instead of one the dashboard button.

Code is below:

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/4ea6bfa3-1544-41ca-94a5-db8e1217a10b 

  Arduino IoT Cloud Variables description

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

  String mesaj;
  String mesaj2;
  bool buton;
  bool push1;
  bool tensiune;

  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 buttonState = 0;
int counter = 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); 

  // 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();
  
  
  pinMode(LED_D0, OUTPUT);
  pinMode(LED_D1, OUTPUT);
  pinMode(LED_D2, OUTPUT);
  pinMode(LED_D3, OUTPUT);
  pinMode(BTN_USER, INPUT);
  pinMode(A7, INPUT);
  
}



void loop() {
  ArduinoCloud.update();
  // Your code here 
  
  buttonState = digitalRead(BTN_USER);
  if(buttonState == LOW){
    if(counter < 4){
      counter++;
    }
    else{
      counter = 0;
    }
    delay(100);
  }
  
  switch(counter){
    case 0:
      digitalWrite(LED_D0, LOW);
      digitalWrite(LED_D1, LOW);
      digitalWrite(LED_D2, LOW);
      digitalWrite(LED_D3, LOW);
      push1 = false;
      mesaj = "0";
  
      break;
      
    case 1:
      digitalWrite(LED_D0, HIGH);
      push1 = false;
      mesaj = "1";

      break;
    case 2:
      digitalWrite(LED_D1, HIGH);
      push1 = false;
      mesaj = "2";

      break;
    case 3:
      digitalWrite(LED_D2, HIGH);
      push1 = false;
      mesaj = "3";

      break;
    case 4:
      digitalWrite(LED_D3, HIGH);
      push1 = true;
      mesaj = "4";

      break;
  }
  
  int pin8State = digitalRead(A7);
  if (pin8State == LOW) {
    tensiune = true;
  }
  else{
    tensiune = false;
  }
  
  
  delay(100);
  
}


/*
  Since Buton is READ_WRITE variable, onButonChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onButonChange()  {
  // Add your code here to act upon Buton change
  
      if(counter < 4){
      counter++;
    }
    else{
      counter = 0;
    }
    delay(100);
  }

	

Thanks in advance,
Marian

In your loop() function you have the following code that increments counter.

  buttonState = digitalRead(BTN_USER);
  if(buttonState == LOW){
    if(counter < 4){
      counter++;
    }
    else{
      counter = 0;
    }
    delay(100);
  }

And then in onButonChange you have this code that also increments counter.

      if(counter < 4){
      counter++;
    }
    else{
      counter = 0;
    }
    delay(100);

That seems odd to me.

Hello and thanks for your reply! Well, on button change code executes what happens when the virtual dashboard button is pressed. In the loop I have executed the same code in the if function triggered by the physical button. All in all I do have the same counter incremented two times but triggered by two different actions: virtual dashboard OR physical button.

Does onButonChange get called when your virtual button is either pressed or released?

Surely the event is fired twice because there are two changes in the state of the button, one from released to pressed and another from pressed to released, and that is your "problem"

Thanks for helping! If I keep the button pressed then it increments with 1, and after I release it indeed it's incremented the second time. So that's the problem. I will think of a code to fix it and come back with the final fix for future guidance! Thanks!

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