ruffrecords
Well-known member
I have a couple of momentary action push buttons I wan to turn into toggle switches in software. There are loads of debouncing examples on line, some better than others, but none about toggling. Here's what I have so far in psudocode. This all takes place in a once every 60mS timer interrupt.
oldswitch = newswitch // move last switch value to old
newswitch = read_switch
if newswitch != oldswitch exit / / not debounced
// here if switch debounced - now need to kook for high to low transition
oldtoggle = new toggle
newtoggle = newswitch
if newtoggle == oldtoggle exit // no transition
if newtoggle = high exit // 0 to 1 transition - not interested
switchvalue = !switchvalue // toggle switchvalue which is what we want
return
Can someone check my logic is not flawed.
Cheers
Ian
oldswitch = newswitch // move last switch value to old
newswitch = read_switch
if newswitch != oldswitch exit / / not debounced
// here if switch debounced - now need to kook for high to low transition
oldtoggle = new toggle
newtoggle = newswitch
if newtoggle == oldtoggle exit // no transition
if newtoggle = high exit // 0 to 1 transition - not interested
switchvalue = !switchvalue // toggle switchvalue which is what we want
return
Can someone check my logic is not flawed.
Cheers
Ian