In the past I declared variables inside the main loop which worked just fine. In a new project I did the same:
counter = 0;
if (someCondition == true)
But then the output of the variable counter shows that for some reason the counter variable is reset to 0 instead of counting up. This happens even after the button is released.
The scope of the variable counter is the main loop and I just assumed that it will keep its value and count up.
What might be the reason for this variable to reset to 0? Is it that other functions are called within the main loop? Is it that an interrupt handler is called every now and then?
The solution I've found is to declare the variable as static:
static uint8_t counter;
Then it increments as expected.
Even though I've found a solution I'd really like to understand what the problem with my first approach was.