Struggled with a Bug on My Energy Meter Project

Hello everyone, Good day.
I have been battling a bug for almost 24 hours on my Energy meter project and discovered the bug had to do with the "volatile" keyword. So basically there were some variables I declared to be used as counters, I observed that there were times when the system could transmit data over UART and then blink my LED once the counter reached a particular value. Something like
if(timer2_count > 50)
{

}
The condition becomes true and the block of code gets executed. Other times the block of code does not execute. After doing some research and consulting chat GPT, I discovered that the volatile keyword was not used, which was the issue. Funny enough, while I was using STM32 cube ide I never experienced such an issue, but it became an issue while using vs code. I just declared the variable using the volatile keyword, which has been consistent. Has anyone had such an issue before? I"'d like to hear about our experiences with keywords like volatile
Was this page helpful?