![]() |
Forum Index : Microcontroller and PC projects : Software debounce
Author | Message | ||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1993 |
@ Disco4now Back in Jan 2023 you posted some code for debounce, I can't get my head around what is happening here. Can you or someone explain to me how this is debouncing the switch. "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
phil99![]() Guru ![]() Joined: 11/02/2018 Location: AustraliaPosts: 2640 |
The way I read it is if the interrupts are more than 10mS apart it is a valid transition, if less it is a bounce and ignored. |
||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1993 |
Yes that makes sense, thanks. "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
PeteCotton![]() Guru ![]() Joined: 13/08/2020 Location: CanadaPosts: 543 |
DIM INTEGER RainPin=15,rain_count,rain_last Setpin RainPin, Intl,rainguageClick,Pullup . . 'rainguage sensor interrupt Sub rainguageClick local thistime thistime=Timer-rain_last rain_last=Timer If thistime>10 Then rain_count=rain_count+1 Print "Rain click "+str$(rain_count) End If End Sub Also, if you change the variable thisTime to millisecondsSinceLastClick, and rain_last to timeOfLastClick then it might be a bit easier to read. DIM INTEGER RainPin=15,rain_count,timeOfLastClick Setpin RainPin, Intl,rainguageClick,Pullup . . 'rainguage sensor interrupt Sub rainguageClick local millisecondsSinceLastClick millisecondsSinceLastClick =Timer-timeOfLastClick timeOfLastClick=Timer If millisecondsSinceLastClick >10 Then rain_count=rain_count+1 Print "Rain click "+str$(rain_count) End If End Sub But to be really picky - I think this code resets timeOfLastClick every time you click - i.e. if you kept spamming the click faster than 10ms it would always reset timeOfLastClick and never increment rain_count. So personally I would make one last change and rename timeOfLastClick to timeOfLastValidClick and move it into the IF statement. But that is being really really picky. It almost certainly wouldn't make a difference in the real world. However it does allow you to standardise on your maximum measurement speed which might be useful. i.e. The code below will always max out at 100Hz. If you increase the number in the IF statement to 25, then you move to 40Hz maximum etc. DIM INTEGER RainPin=15,rain_count,timeOfLastValidClick Setpin RainPin, Intl,rainguageClick,Pullup . . 'rainguage sensor interrupt Sub rainguageClick local millisecondsSinceLastClick millisecondsSinceLastClick =Timer-timeOfLastValidClick If millisecondsSinceLastClick >10 Then timeOfLastValidClick =Timer rain_count=rain_count+1 Print "Rain click "+str$(rain_count) End If End Sub Edited 2024-03-15 02:22 by PeteCotton |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
I have used variations of the rainguageClick for many years. When I changed my weather station to emulate a Davis station, I changed to hardware debounce. I read the rain counter every two seconds and if the count has advanced 5 or more clicks, assume a false reading and ignore, otherwise treat the count as valid. This allows for a downpour and reduces false reads cause by wind etc. Jim VK7JH MMedit |
||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1993 |
Thanks for all of that, I totally understand what is happening now. "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
PhenixRising Guru ![]() Joined: 07/11/2023 Location: United KingdomPosts: 1383 |
FWIW: I have some cases where I need to be absolutely sure that I have a valid signal: Inspired by a FOTS example: 40ms of steady state. pintst% = 19 retcode% = 0 retmem% = 0 ctr% = 0 Do:Loop While Inkey$ <>"" SetTick 5, scanin Do ctr% = 0 Do : Loop Until ctr% = 8 If retcode% <> retmem% Then If retcode% = &HFF Then Print "Down", Hex$(retcode%) EndIf If retcode% = 0 Then Print "Up", Hex$(retcode%) EndIf retmem% = retcode% EndIf Loop Sub scanin Static accum% Select Case ctr% Case 0 accum% = Pin(pintst%)<<7 Inc ctr% Case 1 accum% = accum% + (Pin(pintst%)<<6) Inc ctr% Case 2 accum% = accum% + (Pin(pintst%)<<5) Inc ctr% Case 3 accum% = accum% + (Pin(pintst%)<<4) Inc ctr% Case 4 accum% = accum% + (Pin(pintst%)<<3) Inc ctr% Case 5 accum% = accum% + (Pin(pintst%)<<2) Inc ctr% Case 6 accum% = accum% + (Pin(pintst%)<<1) Inc ctr% Case 7 retcode% = accum% + Pin(pintst%) ctr% = 8 Case 8 End Select End Sub |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |