Arduino If statement for if a toggle is checked or not

What do you want to achieve?

I want to create an If statement within my Arduino sketch that let’s me return whether a toggle is checked.

What have you tried so far?

Complete noob to Arudino IDE and Squarline, so I tried this

if (ui_myToggle1 == 0) {
   Serial.print("Disabled");
}

Others

  • SquareLine Studio V1.4.2. Project settings using version V1.1.2 and LVGL 8.3.11
  • Running on Arduino Giga R1 Wifi with Mbed 4.1.5

If the ui_myToggle1 is a switch widget or a checkbox, anything that can be toggled (has ‘checkable’ flag), you can get its ‘checked’/‘unchecked’ state by lv_obj_has_state( ui_myToggle1, LV_STATE_CHECKED ) (Simply checking the widget’s pointer might only tell if it was created or not, i.e. it’s a NULL/undefined pointer or a valid lv_obj_t* widget-pointer.)

1 Like

Ah, interesting.

And how would I put that into an if statement? I tried something like this, but didn’t get it working.

toggleState = lv_obj_has_state(ui_myToggle1, LV_STATE_CHECKED);

if (toggleState == checked){
   Serial.print("Toggled");
}

You can put it directly into an if/else clause, no need for an intermediate variable. I don’t know whether you need this checking occasionally or cyclically (in main loop), but if you’d like to trigger events when the widget is toggled (event-driven behaviour) you should create an ‘event’ for it in SquareLine Studio, which creates callback-function in the exported source code.