• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Button Callback code.

Q&A Forum › Category: ESP32 › Button Callback code.
0 Vote Up Vote Down
Fernando Taddei asked 10 months ago

Could you please explain for the next code segment, how is that the counter variable is initialized to zero, and later incremented. Shouldn’t this result in a counter value always equals to one?
(Page 102 of the book).
Thanks.

if(code == LV_EVENT_CLICKED) {
static uint8_t counter = 0;
counter++;
lv_label_set_text_fmt(label, “Counter: %d”, counter);
LV_LOG_USER(“Counter: %d”, counter);
}

Question Tags: LVGL
3 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 10 months ago

Hi.
It is because the counter variable is initialized as static.
Learn more about the features of static variables here: https://www.arduino.cc/reference/es/language/variables/variable-scope-qualifiers/static/

I hope this helps.

Regards,
Sara

0 Vote Up Vote Down
Fernando Taddei answered 10 months ago

Yes, it sure did. Thanks.

0 Vote Up Vote Down
Sara Santos Staff answered 10 months ago

Great.
I’ll mark this issue as resolved. If you need further help, you just need to open a new question in our forum.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn LVGL: Build GUIs for ESP32 Projects – Version 1.5 July 7, 2025
  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.