Can you point out where exactly the problem is occurring?
Think about what you are trying to do. Doing one thing per one press of the button requires that you detect TWO button actions:It looks to me like it toggles the LED while the button is held then leaves it on or off at random when the button is released.
1) the closing and debouncing of the switch - you are doing that
2) it also requires detecting when the finger is removed from the button too.
Those two actions make up ONE press of the button.
So after you detect the button pushed (BUTTON_x==1)and toggle the LED you have to next detect the button open again (BUTTON_x==0) before you can test for the button closed again. There are several ways to implement that action but there are some potential problems:
You can not just wait for the switch to open - the goal non-blocking code
Your loop must keep calling Run_Debounce() at 1ms intervals to update the BUTTON_x flag.
Give it some thought and let us know what you are thinking before you code it. The goal right now is to learn about some of the challenges presented by this implementation so far.
Last edited: