Change variable with a button depending on switch

Description

I have two variables (uint) that I want to change with just one button depending on the position of a switch button. The button should add a certain numerical value to the variable. This value is shown on the display using a label. I have tried a wide variety of methods. If statement for calling two different events. Calling the event and querying what the switch button is in. I manage to get one numerical value to change and be displayed. If I then change the switch, the label changes from the previous numerical value to the numerical value that is actually assigned to the other label.

What MCU/Processor/Board and compiler are you using?

Arduino IDE (latest), Arduino Giga R1 WiFi with Display Shield

What LVGL version are you using?

9.1.0

What do you want to achieve?

That I can use a switch and a button to change two variables based on the switch state.

What have you tried so far?

see above.

Code to reproduce


int setpressurecrucible = 234;
int setpressurespraychamber = 52;
bool switch_state = false;
uint helper_uint = 0;
uint cpressure_uint = 0;
uint spressure_uint = 0;

lv_obj_t* tile = lv_tileview_add_tile(tv, 0, 0, LV_DIR_BOTTOM);

  lv_obj_t* lbl_cpressure = lv_label_create(tile);

  lv_obj_t* arc = lv_arc_create(tile);
  lv_obj_set_size(arc, 200, 200);
  lv_arc_set_angles(arc, 150, 45);
  lv_arc_set_value(arc, 62);
  lv_arc_set_range(arc, 0, 2500);
  lv_obj_remove_style(arc, NULL, LV_PART_KNOB);
  lv_obj_align(arc, LV_ALIGN_TOP_LEFT, 30, 30);

  lv_label_set_text_fmt(lbl_cpressure, "%d\nmbar", 1276);
  lv_obj_set_width(lbl_cpressure, 100);
  lv_obj_set_style_text_align(lbl_cpressure, LV_TEXT_ALIGN_CENTER, 0);
  lv_obj_align_to(lbl_cpressure, arc, LV_ALIGN_CENTER, 0, -45);
  lv_obj_set_style_text_font(lbl_cpressure, &lv_font_montserrat_32, 0);

  lv_obj_add_event_cb(arc, value_changed_event_cb, LV_EVENT_VALUE_CHANGED, lbl_cpressure);

  /*Manually update the label for the first time*/
  lv_obj_send_event(arc, LV_EVENT_VALUE_CHANGED, NULL);

  lv_obj_t* lbl_setcpressure = lv_label_create(tile);
  lv_label_set_text_fmt(lbl_setcpressure, "(%d)", setpressurecrucible);
  lv_obj_set_width(lbl_setcpressure, 100);
  lv_obj_set_style_text_align(lbl_setcpressure, LV_TEXT_ALIGN_CENTER, 0);
  lv_obj_align_to(lbl_setcpressure, arc, LV_ALIGN_CENTER, 0, 20);
  lv_obj_set_style_text_font(lbl_setcpressure, &lv_font_montserrat_28, 0);

lv_obj_t* lbl_spressure = lv_label_create(tile);

  lv_obj_t* arcs = lv_arc_create(tile);
  lv_obj_set_size(arcs, 200, 200);
  lv_arc_set_angles(arcs, 170, 310);
  lv_arc_set_value(arcs, 62);
  lv_arc_set_range(arcs, 0, 1500);
  lv_obj_remove_style(arcs, NULL, LV_PART_KNOB);
  lv_obj_align(arcs, LV_ALIGN_TOP_RIGHT, -30, 30);

  lv_label_set_text_fmt(lbl_spressure, "%d\nmbar", 106);
  lv_obj_set_width(lbl_spressure, 100);
  lv_obj_set_style_text_align(lbl_spressure, LV_TEXT_ALIGN_CENTER, 0);
  lv_obj_align_to(lbl_spressure, arcs, LV_ALIGN_CENTER, 0, -45);
  lv_obj_set_style_text_font(lbl_spressure, &lv_font_montserrat_32, 0);

  lv_obj_add_event_cb(arcs, value_changed_event_cb_s, LV_EVENT_VALUE_CHANGED, lbl_spressure);

  /*Manually update the label for the first time*/
  lv_obj_send_event(arcs, LV_EVENT_VALUE_CHANGED, NULL);

  lv_obj_t* lbl_setspressure = lv_label_create(tile);
  lv_label_set_text_fmt(lbl_setspressure, "(%d)", setpressurespraychamber);
  lv_obj_set_width(lbl_setspressure, 100);
  lv_obj_set_style_text_align(lbl_setspressure, LV_TEXT_ALIGN_CENTER, 0);
  lv_obj_align_to(lbl_setspressure, arcs, LV_ALIGN_CENTER, 0, 20);
  lv_obj_set_style_text_font(lbl_setspressure, &lv_font_montserrat_28, 0);

  //---------------------------------
  lv_obj_t* label;

  static lv_obj_t* btn_5mbar = lv_button_create(tile);
    lv_obj_add_event_cb(btn_5mbar, plus_5_event_handler, LV_EVENT_CLICKED, lbl_setcpressure);
  lv_obj_align(btn_5mbar, LV_ALIGN_OUT_TOP_MID, 250, 30);
  lv_obj_remove_flag(btn_5mbar, LV_OBJ_FLAG_PRESS_LOCK);

  label = lv_label_create(btn_5mbar);
  lv_label_set_text(label, "+5");
  lv_obj_center(label);
  lv_obj_set_style_text_font(label, &lv_font_montserrat_32, 0);

//----------------------------------
  lv_obj_t* sw;

  sw = lv_switch_create(tile);
  lv_obj_add_event_cb(sw, sw_event_handler, LV_EVENT_VALUE_CHANGED, lbl_toggle);
  lv_obj_add_flag(sw, LV_OBJ_FLAG_EVENT_BUBBLE);
  lv_obj_align(sw, LV_ALIGN_CENTER, 0, -78);

  lv_obj_t* label7;
  label7 = lv_label_create(tile);
  lv_label_set_text(label7, "Crucible");
  lv_obj_center(label7);
  lv_obj_align(label7, LV_ALIGN_CENTER, -70, -78);

  lv_obj_t* label8;
  label8 = lv_label_create(tile);
  lv_label_set_text(label8, "Spraychamber");
  lv_obj_center(label8);
  lv_obj_align(label8, LV_ALIGN_CENTER, 90, -78);

static void plus_5_event_handler(lv_event_t* e) {
  lv_event_code_t code = lv_event_get_code(e);
  if (code == LV_EVENT_SHORT_CLICKED) {
    if (!switch_state) {
      lv_obj_t* lbl_setcpressure = (lv_obj_t*)lv_event_get_param(e);
      setpressurecrucible = pressure_set(5);
      lv_label_set_text_fmt(lbl_setcpressure, "(%d)", setpressurecrucible);
    } else if (switch_state) {
      lv_obj_t* lbl_setspressure = (lv_obj_t*)lv_event_get_param(e);
      setpressurespraychamber = pressure_set(5);
      lv_label_set_text_fmt(lbl_setspressure, "(%d)", setpressurespraychamber);
    }
  }
}

static void value_changed_event_cb(lv_event_t* e) {
  lv_obj_t* arc = (lv_obj_t*)lv_event_get_target(e);
  lv_obj_t* lbl_cpressure = (lv_obj_t*)lv_event_get_user_data(e);

  helper_uint = lv_arc_get_value(arc);
  cpressure_uint = helper_uint - 1000;

  lv_label_set_text_fmt(lbl_cpressure, "%d\nmbar", cpressure_uint);
}

static void value_changed_event_cb_s(lv_event_t* e) {
  lv_obj_t* arcs = (lv_obj_t*)lv_event_get_target(e);
  lv_obj_t* lbl_spressure = (lv_obj_t*)lv_event_get_user_data(e);

  helper_uint = lv_arc_get_value(arcs);
  spressure_uint = helper_uint - 1000;

  lv_label_set_text_fmt(lbl_spressure, "%d\nmbar", spressure_uint);
}

Screenshot and/or video

Not at hand right now

Your code seems not complete sw_event_handler? and use switch_state is waste variable and … Why you dont simple get switch value in IF ?

But ofcourse objects required for use in code you all create as local , then you require in handlers get pointers and …

Thank you for your answer. I searched a lot of how i get the objects global but i didn’t find a solution or understand it. Can you explain me this please?
I try to search about information for the pointers maybe this will work better.

Maybe start with show real right code. for example up showed dont end function and start new …

  lv_obj_center(label8);
  lv_obj_align(label8, LV_ALIGN_CENTER, 90, -78);

static void plus_5_event_handler(lv_event_t* e) {
  lv_event_code_t code = lv_event_get_code(e);
  if (code == LV_EVENT_SHORT_CLICKED) {

this is nonsense

#include "Arduino_H7_Video.h"
#include "lvgl.h"
#include "Arduino_GigaDisplayTouch.h"
#include <RPC.h>

//------------------------------------------------------------------------------
int meltingpressure = 60;
int spraychamberpressure = 20;
int differencepressurestart = 200;
int differencepressureend = 250;
float differencepressuretime = 2.5;
int setpressurecrucible = 234;
int setpressurespraychamber = 52;
bool switch_state = false;

uint helper_uint = 0;
uint cpressure_uint = 0;
uint spressure_uint = 0;

//------------------------------------------------------------------------------

Arduino_H7_Video Display(800, 480, GigaDisplayShield);
Arduino_GigaDisplayTouch TouchDetector;

LV_FONT_DECLARE(lv_font_montserrat_20)
LV_FONT_DECLARE(lv_font_montserrat_22)
LV_FONT_DECLARE(lv_font_montserrat_28)
LV_FONT_DECLARE(lv_font_montserrat_30)
LV_FONT_DECLARE(lv_font_montserrat_32)

static void value_changed_event_cb(lv_event_t* e);

void setup() {
  Display.begin();
  TouchDetector.begin();
  RPC.begin();
  //------------------------------------------------------------------------------
  /*Create a Tab view object*/
  lv_obj_t* tabview;
  tabview = lv_tabview_create(lv_screen_active());

  /*Add 3 tabs (the tabs are page (lv_page) and can be scrolled*/
  lv_obj_t* tab1 = lv_tabview_add_tab(tabview, "Main");
  lv_obj_t* tab2 = lv_tabview_add_tab(tabview, "Modus");
  lv_obj_t* tab3 = lv_tabview_add_tab(tabview, "Parameters");
  //-----------------------------------------------------------------------------
  lv_obj_t* tv = lv_tileview_create(tab1);

  lv_obj_t* tile = lv_tileview_add_tile(tv, 0, 0, LV_DIR_BOTTOM);

  lv_obj_t* lbl_cpressure = lv_label_create(tile);

  lv_obj_t* arc = lv_arc_create(tile);
  lv_obj_set_size(arc, 200, 200);
  lv_arc_set_angles(arc, 150, 45);
  lv_arc_set_value(arc, 62);
  lv_arc_set_range(arc, 0, 2500);
  lv_obj_remove_style(arc, NULL, LV_PART_KNOB);
  lv_obj_align(arc, LV_ALIGN_TOP_LEFT, 30, 30);

  lv_label_set_text_fmt(lbl_cpressure, "%d\nmbar", 1276);
  lv_obj_set_width(lbl_cpressure, 100);
  lv_obj_set_style_text_align(lbl_cpressure, LV_TEXT_ALIGN_CENTER, 0);
  lv_obj_align_to(lbl_cpressure, arc, LV_ALIGN_CENTER, 0, -45);
  lv_obj_set_style_text_font(lbl_cpressure, &lv_font_montserrat_32, 0);

  lv_obj_add_event_cb(arc, value_changed_event_cb, LV_EVENT_VALUE_CHANGED, lbl_cpressure);

  /*Manually update the label for the first time*/
  lv_obj_send_event(arc, LV_EVENT_VALUE_CHANGED, NULL);

  lv_obj_t* lbl_setcpressure = lv_label_create(tile);
  lv_label_set_text_fmt(lbl_setcpressure, "(%d)", setpressurecrucible);
  lv_obj_set_width(lbl_setcpressure, 100);
  lv_obj_set_style_text_align(lbl_setcpressure, LV_TEXT_ALIGN_CENTER, 0);
  lv_obj_align_to(lbl_setcpressure, arc, LV_ALIGN_CENTER, 0, 20);
  lv_obj_set_style_text_font(lbl_setcpressure, &lv_font_montserrat_28, 0);

  lv_obj_t* lbl_toggle = lv_label_create(tile);
  lv_label_set_text_fmt(lbl_toggle, "%d", switch_state);
  lv_obj_center(lbl_toggle);
  lv_obj_set_style_text_font(lbl_toggle, &lv_font_montserrat_32, 0);
  lv_obj_align_to(lbl_toggle, arc, LV_ALIGN_CENTER, 0, 45);

  lv_obj_t* lbl_spressure = lv_label_create(tile);

  lv_obj_t* arcs = lv_arc_create(tile);
  lv_obj_set_size(arcs, 200, 200);
  lv_arc_set_angles(arcs, 170, 310);
  lv_arc_set_value(arcs, 62);
  lv_arc_set_range(arcs, 0, 1500);
  lv_obj_remove_style(arcs, NULL, LV_PART_KNOB);
  lv_obj_align(arcs, LV_ALIGN_TOP_RIGHT, -30, 30);

  lv_label_set_text_fmt(lbl_spressure, "%d\nmbar", 106);
  lv_obj_set_width(lbl_spressure, 100);
  lv_obj_set_style_text_align(lbl_spressure, LV_TEXT_ALIGN_CENTER, 0);
  lv_obj_align_to(lbl_spressure, arcs, LV_ALIGN_CENTER, 0, -45);
  lv_obj_set_style_text_font(lbl_spressure, &lv_font_montserrat_32, 0);

  lv_obj_add_event_cb(arcs, value_changed_event_cb_s, LV_EVENT_VALUE_CHANGED, lbl_spressure);

  /*Manually update the label for the first time*/
  lv_obj_send_event(arcs, LV_EVENT_VALUE_CHANGED, NULL);

  lv_obj_t* lbl_setspressure = lv_label_create(tile);
  lv_label_set_text_fmt(lbl_setspressure, "(%d)", setpressurespraychamber);
  lv_obj_set_width(lbl_setspressure, 100);
  lv_obj_set_style_text_align(lbl_setspressure, LV_TEXT_ALIGN_CENTER, 0);
  lv_obj_align_to(lbl_setspressure, arcs, LV_ALIGN_CENTER, 0, 20);
  lv_obj_set_style_text_font(lbl_setspressure, &lv_font_montserrat_28, 0);

  //---------------------------------
  lv_obj_t* label;

  static lv_obj_t* btn_5mbar = lv_button_create(tile);
    lv_obj_add_event_cb(btn_5mbar, plus_5_event_handler, LV_EVENT_CLICKED, lbl_setcpressure);
  lv_obj_align(btn_5mbar, LV_ALIGN_OUT_TOP_MID, 250, 30);
  lv_obj_remove_flag(btn_5mbar, LV_OBJ_FLAG_PRESS_LOCK);

  label = lv_label_create(btn_5mbar);
  lv_label_set_text(label, "+5");
  lv_obj_center(label);
  lv_obj_set_style_text_font(label, &lv_font_montserrat_32, 0);

  //----------------------------------
  lv_obj_t* sw;

  sw = lv_switch_create(tile);
  lv_obj_add_event_cb(sw, sw_event_handler, LV_EVENT_VALUE_CHANGED, lbl_toggle);
  lv_obj_add_flag(sw, LV_OBJ_FLAG_EVENT_BUBBLE);
  lv_obj_align(sw, LV_ALIGN_CENTER, 0, -78);

  lv_obj_t* label7;
  label7 = lv_label_create(tile);
  lv_label_set_text(label7, "Crucible");
  lv_obj_center(label7);
  lv_obj_align(label7, LV_ALIGN_CENTER, -70, -78);

  lv_obj_t* label8;
  label8 = lv_label_create(tile);
  lv_label_set_text(label8, "Spraychamber");
  lv_obj_center(label8);
  lv_obj_align(label8, LV_ALIGN_CENTER, 90, -78);

  //---------------------------------
}

static void sw_event_handler(lv_event_t* e) {
  lv_event_code_t code = lv_event_get_code(e);
  lv_obj_t* lbl_toggle = (lv_obj_t*)lv_event_get_user_data(e);
  if (code == LV_EVENT_VALUE_CHANGED) {
    if (switch_state == false) {
      switch_state = true;
    } else switch_state = false;
  }
  lv_label_set_text_fmt(lbl_toggle, "%d", switch_state);
}


static void plus_5_event_handler(lv_event_t* e) {
  lv_event_code_t code = lv_event_get_code(e);
  if (code == LV_EVENT_SHORT_CLICKED) {
    if (!switch_state) {
      lv_obj_t* lbl_setcpressure = (lv_obj_t*)lv_event_get_param(e);
      setpressurecrucible = pressure_set(5);
      lv_label_set_text_fmt(lbl_setcpressure, "(%d)", setpressurecrucible);
    } else if (switch_state) {
      lv_obj_t* lbl_setspressure = (lv_obj_t*)lv_event_get_param(e);
      setpressurespraychamber = pressure_set(5);
      lv_label_set_text_fmt(lbl_setspressure, "(%d)", setpressurespraychamber);
    }
  }
}

static void value_changed_event_cb(lv_event_t* e) {
  lv_obj_t* arc = (lv_obj_t*)lv_event_get_target(e);
  lv_obj_t* lbl_cpressure = (lv_obj_t*)lv_event_get_user_data(e);

  helper_uint = lv_arc_get_value(arc);
  cpressure_uint = helper_uint - 1000;

  lv_label_set_text_fmt(lbl_cpressure, "%d\nmbar", cpressure_uint);
}

static void value_changed_event_cb_s(lv_event_t* e) {
  lv_obj_t* arcs = (lv_obj_t*)lv_event_get_target(e);
  lv_obj_t* lbl_spressure = (lv_obj_t*)lv_event_get_user_data(e);

  helper_uint = lv_arc_get_value(arcs);
  spressure_uint = helper_uint - 1000;

  lv_label_set_text_fmt(lbl_spressure, "%d\nmbar", spressure_uint);
}

static int pressure_set(unsigned int btn_value) {
  if (!switch_state) {
    setpressurecrucible = setpressurecrucible + btn_value;
    return setpressurecrucible;
  } else {
    setpressurespraychamber = setpressurespraychamber + btn_value;
    return setpressurespraychamber;
  }
}

void loop() {
  lv_timer_handler();
}

This is the complete code. Sorry, in my first post I only included the code snippets that I thought were relevant.

to create global simply move this before void setup line and try rewrite code more simple for example

static void sw_event_handler(lv_event_t* e) {
 
  lv_label_set_text_fmt((lv_obj_t*)lv_event_get_user_data(e), "%d", lv_obj_has_state(sw, LV_STATE_CHECKED));
}