Go to Post Unconventional isn't necessarily innovation. - AdamHeard [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #6   Spotlight this post!  
Unread 21-01-2006, 14:17
Windward's Avatar
Windward Windward is offline
Registered User
FRC #1452
Team Role: Programmer
 
Join Date: Nov 2005
Rookie Year: 2004
Location: So Cal
Posts: 88
Windward is an unknown quantity at this point
Re: toggle switch

Quote:
Originally Posted by KenWittlief
all of the code listed so far has missed something: switch bounce

when you open or close a simple switch the contacts do not immediately change state and then stay there, they 'bounce'.

If you looked at the switch on a scope it would look like a squarewave for up to 100mS.

There are two ways basic ways to debounce a switch: HW and SW.

In HW you would need to put a cap across the switch, so that the cap will charge or discharge when the switch is closed or open, and the RC time constant will absorb the bouncing. Instead of a squarewave signal you will see an exponential ramp that (with the right cap) will not cross the On and Off levels more than once. To do this correctly you really need an oscilloscope to see the waveform coming off the switch.

To debounce a switch in SW you use a SW timer. The easy way is, when you see a switch close you immediately accept that as a user input (state = closed) and do whatever you need to do (turn a motor on, turn a motor off... whatever)

but you then set a 'debounce' flag. You will not accept another switch closure until that debounce flag is cleared.

When you see the switch open again you decrement a debounce counter (one that will add up to 100mS or more) and you keep reading the switch on each loop. If you see the switch closed again while the debounce flag is still set, you restart the counter to its initial value (still bouncing, start the countdown over).

Once you have polled the switch for 100mS, and it stayed open each time, your debounce counter will reach 0, and you can clear the debounce flag.

This SW method gives you an instant response when you push the button, and it debounces when you take your finger off.

BTW, if you dont want to debounce your switches, the other way is to use two switches, one for ON and one for OFF. That way your outputs dont toggle while the switch is bouncing.

Edited to add: The really annoying thing about switch bounce is it happens so fast that you may not see it. If the switch bounce is toggling your output, then sometimes your system will end up in the correct state (on when you wanted on....) and randomly it will end up in the wrong state. It will act flakey, and if you dont immediately say to yourself "Ah! SwitchBounce!" it will drive you crazy trying to debug your code.

I have some experience with programming, but I'm completely lost. What is HW and SW? Also I kinda have to use a toggle swich because our electrician is completely obsessed with it and it would be inconvenient to have switches everywhere at the competition (like we did last year somewhat).

For HW are you physically putting a cap over the switch? Seems a little impractical.

So is SW basically it switches the state of the switch when you take your finger off it (sort of)? Or is it when you press a switch, it doesn't accept that switch being pressed for a certain amount of time?
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PRESSURE SWITCH Mr. Steve Pneumatics 20 01-12-2005 10:20
Toggle switch with pneumatic The yellowdart Programming 2 21-02-2005 14:08
Pneumatics on a toggle switch Idaman323 Programming 3 15-02-2005 18:42
LEDs in switch box. ZZII 527 Electrical 5 06-03-2004 19:39
3 way toggle switch programming LeadRiccardoT Programming 3 17-02-2003 02:02


All times are GMT -5. The time now is 21:42.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi