Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 07:15 28 Apr 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Software debounce

Author Message
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1801
Posted: 04:04am 14 Mar 2024
Copy link to clipboard 
Print this post

@ Disco4now
Back in Jan 2023 you posted some code for debounce,
  Quote  Here is what I used for the tipping bucket to de-bounce it. I probably got it from TBS somewhere.

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

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: Australia
Posts: 1783
Posted: 04:17am 14 Mar 2024
Copy link to clipboard 
Print this post

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: Australia
Posts: 1801
Posted: 04:19am 14 Mar 2024
Copy link to clipboard 
Print this post

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: Canada
Posts: 316
Posted: 04:16pm 14 Mar 2024
Copy link to clipboard 
Print this post

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: Australia
Posts: 5905
Posted: 08:32pm 14 Mar 2024
Copy link to clipboard 
Print this post

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   MMBasic Help
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1801
Posted: 08:56pm 14 Mar 2024
Copy link to clipboard 
Print this post

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
Senior Member

Joined: 07/11/2023
Location: United Kingdom
Posts: 284
Posted: 07:26am 15 Mar 2024
Copy link to clipboard 
Print this post

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

 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024