Value Selector not working

Hello, I struggle with the code to understand Value Selector widget. So on the dashboard I have value selector 1,2,3 and green and red led. When I click on value 1, turn green light on red light off. When I click on value 2, turn red light on green light off. very basic. I am confused on to where to put that code. In the loop or in the OnValuechange section. Thank you.

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/xxxxxxxxxxxxxxxxxxxxxxxxxxf 

  Arduino IoT Cloud Variables description

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

  int selection;
  bool green;
  bool red;

  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"

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  
  digitalWrite(2,HIGH);
  
  // 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();
  
  selection=0;
  green= 0;
  red=0;
}

void loop() {
  ArduinoCloud.update();
  // Your code here
  if (selection =1) {
     green = 1;
      red = 0;

  } 
  
  if (selection =2) {
     red = 1;
     green = 0;

  } 
  
}


/*
  Since Selection is READ_WRITE variable, onSelectionChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onSelectionChange()  {
  // Add your code here to act upon Selection change
          }
      

/*
  Since Green is READ_WRITE variable, onGreenChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onGreenChange()  {
  // Add your code here to act upon Green change
}

/*
  Since Red is READ_WRITE variable, onRedChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onRedChange()  {
  // Add your code here to act upon Red change
}


= is used for assignment
== is used for comparing

thank you! now I really feel like a beginner! So this code belong in the on value change section or loop?

It would make sense indeed to have the callback actions implemented in onSelectionChange() since that’s what gets triggered

Indeed makes sense! I will try to first set red and green light off in the setup loop then put the code in the OnSelection Change. I’ll report back. Thank you

Sorry me again. Can you see what is wrong in my code? red and green light are not changing at all. Thank you.


#include "thingProperties.h"

void setup() {
 
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  digitalWrite(2,LOW);
  delay(2000);
  digitalWrite(2,HIGH);

  initProperties();

  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
 
}

void loop() {
  ArduinoCloud.update();
  } 
 
void onSelectionChange()  {
  
       if (selection ==1) {
       green = 1;
       red = 0;  } 
  
       if (selection ==2) {
        red = 1;
       green = 0;
        }
       if (selection ==3) {
        red = 1;
       green = 1;
        }
}

void onGreenChange()  {
  
}

void onRedChange()  {
  
}
  
  
  
  

Ok. I found what was wrong. So I will post this maybe it helps others. LED light and other item you turn on, don't turn off by themself. So inside the OnSelectionChange I first start by turning off red and green light. Then it works. 1 is green, 2 is red, 3 is nothing. Case closed.

/*
  Since Selection is READ_WRITE variable, onSelectionChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onSelectionChange()  {
  
  green =0;
  red = 0;
  
  if(selection==1)
     {
    green = 1;
     }
     
     if(selection==2)
     {
    green =0;
    red = 1;
     }
     
     if(selection==3)
     {
    green =0;
    red = 0;
     }
}

![selection|574x496](upload://zN7wNEMUfFNpbM8rVvQIjXcgnkH.png)

selection

Or you needed to update both values in each if (use else in between your if or use a switch case)

It would be interesting to see what selection is worth - could it take other values in case of a bug?

If you keep your code, this part is not needed

if(selection==3)
     {
    green =0;
    red = 0;
     }

Since your already are at 0

Good point. Thank you Jackson for your help.