Taking value from a slider and save it as an int in main sketch

I’m trying to do something that I thought would be simple but for some reason I’m stuck.
I have a slider on my touchscreen.
I want the value from the slider to be sent to an int variable on my main.c to be used in my loop. but for some reason I can’t figure out how. I created the ui and slider with SquareLine and my slider moves when I touch the screen but I can’t figure out what to do from there…

Hi,

Without seeing your code I cant exactly tell what your problem is. Maybe you didnt create the callback?

Have a look at the docs for the slider, here:
https://docs.lvgl.io/latest/en/html/widgets/slider.html

The really basic version shows:

static void event_handler(lv_obj_t * obj, lv_event_t event)
{
    if(event == LV_EVENT_VALUE_CHANGED) {
        printf("Value: %d\n", lv_slider_get_value(obj));
    }
}

void lv_ex_slider_1(void)
{
    /*Create a slider*/
    lv_obj_t * slider = lv_slider_create(lv_scr_act(), NULL);
    lv_obj_align(slider, NULL, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_event_cb(slider, event_handler);
}

But having the value shown on screen is better:

void lv_ex_slider_2(void)
{
    /* Create a slider in the center of the display */
    lv_obj_t * slider = lv_slider_create(lv_scr_act(), NULL);
    lv_obj_set_width(slider, LV_DPI * 2);
    lv_obj_align(slider, NULL, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_event_cb(slider, slider_event_cb);
    lv_slider_set_range(slider, 0, 100);
    
    /* Create a label below the slider */
    slider_label = lv_label_create(lv_scr_act(), NULL);
    lv_label_set_text(slider_label, "0");
    lv_obj_set_auto_realign(slider_label, true);
    lv_obj_align(slider_label, slider, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    
    /* Create an informative label */
    lv_obj_t * info = lv_label_create(lv_scr_act(), NULL);
    lv_label_set_text(info, "Welcome to the slider+label demo!\n"
                            "Move the slider and see that the label\n"
                            "updates to match it.");
    lv_obj_align(info, NULL, LV_ALIGN_IN_TOP_LEFT, 10, 10);
}

static void slider_event_cb(lv_obj_t * slider, lv_event_t event)
{
    if(event == LV_EVENT_VALUE_CHANGED) {
        static char buf[4]; /* max 3 bytes for number plus 1 null terminating byte */
        snprintf(buf, 4, "%u", lv_slider_get_value(slider));
        lv_label_set_text(slider_label, buf);
    }
}

Hope that helps.

If you create it in SQS and no temporary screen, then dont require store it.
Simply in c code ask value of it on place you need.

value = lv_slider_get_value(ui_Slider1) ;

Other think is reaction to changes. SQS for this have event part, learn howto use it.

1 Like