One button toggle in C

Status
Not open for further replies.

Erythryo

New Member
Recently I have been building a robot, but I have been thinking of a way to make a one button toggle. I was hypothesizing of using a for loop and adding a bit everytime it was pressed, then it would execute the code inside the for bracket.

Put simply, how could I have the mcu check for an input, delay x amount of time then if the button was still on it would execute the code?
 
Well, the two ways would be to either have the entire program include checking for the button inside the main loop, or you could tie the button to an interrupt pin. When the input toggles, it would interrupt the MCU and execute a specific set of instructions (in this case, a delay follow by reading the state of the pin again to see if it is still pressed).
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…