OCCRA
Go to Post Even worse, you could have a "Kiss the Paul" contest. I heard he squeals when you pick him up too. - AndyB [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 01-16-2006, 03:02 PM
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
toggle switch

We were setting up a toggle switch so that when you press a button (p1_sw_trig) it activates a spike (relay1_fwd) and when you press the button again it deactivates it. I will also use this for other things in the programming code, such as a manual overide for the auto tracking.


int switch1, switch2, initVar, printCount, switch1Count, switch2Count;

switch1 = SWITCH1; // port1 trig
switch2 = SWITCH2; // port1 top

if(initVar !=1) // initialize variables
{
printCount = 0;
}

printf("switch1 = %d", switch1);
printf("switch2 = %d", switch2);

if(switch1 == 1) // if switch1 pressed
{
if(switch1Count != 1) // not forward, so set forward
{
switch1Count = 1;
}
else // forward, so set back
{
switch1Count = 0;
}
}

printCount++;
if(printCount > 100) // decreases amount of printfs
{
printf("switch1 = %d", switch1);
printf("switch1Count = %d", switch1Count);
printCount = 0;
}
if(switch1Count == 1)
{
relay1_fwd = 1;
switch1Count = 1;
if1 = 1;
}
if(switch1Count == 0)
{
relay1_rev = 1;
switch1Count = 2;
}
  #2   Spotlight this post!  
Unread 01-16-2006, 03:28 PM
Mike's Avatar
Mike Mike is offline
Registered User
FRC #0237 (Sie-H2O-Bots (See-Hoe-Bots) [T.R.I.B.E.])
Team Role: Programmer
 
Join Date: Dec 2004
Rookie Year: 2004
Location: Watertown, CT
Posts: 1,002
Mike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond repute
Re: toggle switch

Remember KISS.
Code:
char activated = 0;

if(switch == 1) // If we told it to change states
{
	if(activated == 0)
	{
		relay1_fwd = 1;
		relay1_rev = 0;
		activated = 1;
	}
	else
	{
		relay1_fwd = 0;
		relay1_rev = 1;
		activated = 0;
	}
}
Voila.
  #3   Spotlight this post!  
Unread 01-16-2006, 03:51 PM
devicenull devicenull is offline
Robot? We need a robot?
no team
 
Join Date: Sep 2004
Rookie Year: 1234
Location: n/a
Posts: 359
devicenull is just really nicedevicenull is just really nicedevicenull is just really nicedevicenull is just really nicedevicenull is just really nice
Re: toggle switch

Quote:
Originally Posted by Mike
Remember KISS.
Code:
char activated = 0;

if(switch == 1) // If we told it to change states
{
	if(activated == 0)
	{
		relay1_fwd = 1;
		relay1_rev = 0;
		activated = 1;
	}
	else
	{
		relay1_fwd = 0;
		relay1_rev = 1;
		activated = 0;
	}
}
Voila.
Not quite.. that code requires a very fast trigger finger..

Basically:
Code:
char oldstate;

if (p1_sw_top && !oldstate) {
 relay1_fwd = !relay1_fwd;
 relay1_rev = !relay1_rev;
}
if (p1_sw_top != oldstate) oldstate = p1_sw_top;
Should work. It requires you to release the button and press it again to re-activate the change.
  #4   Spotlight this post!  
Unread 01-17-2006, 05:47 PM
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 Mike
Remember KISS.
Code:
char activated = 0;

if(switch == 1) // If we told it to change states
{
	if(activated == 0)
	{
		relay1_fwd = 1;
		relay1_rev = 0;
		activated = 1;
	}
	else
	{
		relay1_fwd = 0;
		relay1_rev = 1;
		activated = 0;
	}
}
Voila.
Thanks,
do you mean "int activated?"
Thanks again.
  #5   Spotlight this post!  
Unread 01-17-2006, 05:58 PM
devicenull devicenull is offline
Robot? We need a robot?
no team
 
Join Date: Sep 2004
Rookie Year: 1234
Location: n/a
Posts: 359
devicenull is just really nicedevicenull is just really nicedevicenull is just really nicedevicenull is just really nicedevicenull is just really nice
Re: toggle switch

Nope, char will work fine. In that case he's referring to the size of the data, char uses up less memory then an int. unsigned chars range from 0-255, while ints have a lot bigger range. Since theres no bool or a 0-1 value, char is the smallest
  #6   Spotlight this post!  
Unread 01-17-2006, 06:07 PM
Mike's Avatar
Mike Mike is offline
Registered User
FRC #0237 (Sie-H2O-Bots (See-Hoe-Bots) [T.R.I.B.E.])
Team Role: Programmer
 
Join Date: Dec 2004
Rookie Year: 2004
Location: Watertown, CT
Posts: 1,002
Mike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond repute
Re: toggle switch

Quote:
Originally Posted by Windward
Thanks,
do you mean "int activated?"
Thanks again.
Err, my code wasn't exactly perfect. You would have to hit the button very fast or else it would execute multiple times. I didn't test Devicenull's code, but I'd go with that. I'm sure it works better than mine.
  #7   Spotlight this post!  
Unread 01-17-2006, 06:22 PM
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 Mike
Err, my code wasn't exactly perfect. You would have to hit the button very fast or else it would execute multiple times. I didn't test Devicenull's code, but I'd go with that. I'm sure it works better than mine.
Devicenull's code? Is that on the forums?
  #8   Spotlight this post!  
Unread 01-17-2006, 06:58 PM
Francis-134's Avatar
Francis-134 Francis-134 is offline
Lifer
FRC #0190 (Gompei and the Herd)
Team Role: Mentor
 
Join Date: Jan 2003
Rookie Year: 2003
Location: Worcester, MA
Posts: 661
Francis-134 has a reputation beyond reputeFrancis-134 has a reputation beyond reputeFrancis-134 has a reputation beyond reputeFrancis-134 has a reputation beyond reputeFrancis-134 has a reputation beyond reputeFrancis-134 has a reputation beyond reputeFrancis-134 has a reputation beyond reputeFrancis-134 has a reputation beyond reputeFrancis-134 has a reputation beyond reputeFrancis-134 has a reputation beyond reputeFrancis-134 has a reputation beyond repute
Re: toggle switch

Quote:
Originally Posted by Windward
Devicenull's code? Is that on the forums?
He is referring to the gentleman that posted a few posts above you, specifilcally post number 3.
__________________

Email | Twitter | Facebook | YouTube | Twitch
Snapchat

A proud alumnus of teams 134 and 40 || Mentor of Team 190 || WPI Manager of Robotics Initiatives
  #9   Spotlight this post!  
Unread 01-17-2006, 07:08 PM
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 Francis-134
He is referring to the gentleman that posted a few posts above you, specifilcally post number 3.
oops. My bad. All this work is already starting to take a toll on me (especially since my only experienced programming peer is on a 8 day trip to the other coast.)
  #10   Spotlight this post!  
Unread 01-21-2006, 01:31 PM
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 devicenull
Not quite.. that code requires a very fast trigger finger..

Basically:
Code:
char oldstate;

if (p1_sw_top && !oldstate) {
 relay1_fwd = !relay1_fwd;
 relay1_rev = !relay1_rev;
}
if (p1_sw_top != oldstate) oldstate = p1_sw_top;
Should work. It requires you to release the button and press it again to re-activate the change.
Would you put the toggle switch code after the initialization of the default relay switches? If not where? Would you want to comment out the default relay switch setup?

I am editing the default camera files for the toggle switch. I think it is possible that the camera files won't let the toggle switch work (until I change something at least) because it won't printfs won't print (the camera ones do though)

Last edited by Windward : 01-21-2006 at 01:39 PM.
  #11   Spotlight this post!  
Unread 01-21-2006, 01:50 PM
KenWittlief KenWittlief is offline
.
no team
Team Role: Engineer
 
Join Date: Mar 2003
Location: Rochester, NY
Posts: 4,206
KenWittlief has a reputation beyond reputeKenWittlief has a reputation beyond reputeKenWittlief has a reputation beyond reputeKenWittlief has a reputation beyond reputeKenWittlief has a reputation beyond reputeKenWittlief has a reputation beyond reputeKenWittlief has a reputation beyond reputeKenWittlief has a reputation beyond reputeKenWittlief has a reputation beyond reputeKenWittlief has a reputation beyond reputeKenWittlief has a reputation beyond repute
Re: toggle switch

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.

Last edited by KenWittlief : 01-21-2006 at 01:59 PM.
  #12   Spotlight this post!  
Unread 01-21-2006, 02:17 PM
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?
  #13   Spotlight this post!  
Unread 01-21-2006, 03:27 PM
KenWittlief KenWittlief is offline
.
no team
Team Role: Engineer
 
Join Date: Mar 2003
Location: Rochester, NY
Posts: 4,206
KenWittlief has a reputation beyond reputeKenWittlief has a reputation beyond reputeKenWittlief has a reputation beyond reputeKenWittlief has a reputation beyond reputeKenWittlief has a reputation beyond reputeKenWittlief has a reputation beyond reputeKenWittlief has a reputation beyond reputeKenWittlief has a reputation beyond reputeKenWittlief has a reputation beyond reputeKenWittlief has a reputation beyond reputeKenWittlief has a reputation beyond repute
Re: toggle switch

HW =Hardware solution (using a capacitor). the cap could be wired right across the switch, but the value of the cap depends on several factors in your system, and is best determined by testing the results with an oscilloscope (digital scope preferrably).

SW= Software (programming). The method I described accepts the button push immediately (as long as the 'debounce' flag has not been previously set), and it starts a timer when the button is released. The SW switches state immediately when the button is pushed (because that is when the driver wants something to happen: ............NOW!) Then it watches the button to see when it is released, and makes sure it stays released for the debounce time (100mS) until it will accept another NOW! from the driver.

The best way to visualize a bouncing switch is when someone pushes the button slowly, very slowly till it just makes contact, and they hold it there. What will the code see on the input? ( a mess!) With a one-button solution your drivers will have to be trained to push the button cleanly, not holding their finger on the button and sort-of pressing it. But even when you push it quickly and cleanly, a pushbutton or toggle switch will still bounce.

Two buttons makes everything easier for the SW to handle the inputs, but if you are stuck with one button for ON and OFF, you gotta take what the HW guys gave you and make it work in the code.
  #14   Spotlight this post!  
Unread 01-21-2006, 03:32 PM
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
HW =Hardware solution (using a capacitor). the cap could be wired right across the switch, but the value of the cap depends on several factors in your system, and is best determined by testing the results with an oscilloscope (digital scope preferrably).

SW= Software (programming). The method I described accepts the button push immediately (as long as the 'debounce' flag has not been previously set), and it starts a timer when the button is released. The SW switches state immediately when the button is pushed (because that is when the driver wants something to happen: ............NOW!) Then it watches the button to see when it is released, and makes sure it stays released for the debounce time (100mS) until it will accept another NOW! from the driver.

The best way to visualize a bouncing switch is when someone pushes the button slowly, very slowly till it just makes contact, and they hold it there. What will the code see on the input? ( a mess!) With a one-button solution your drivers will have to be trained to push the button cleanly, not holding their finger on the button and sort-of pressing it. But even when you push it quickly and cleanly, a pushbutton or toggle switch will still bounce.

Two buttons makes everything easier for the SW to handle the inputs, but if you are stuck with one button for ON and OFF, you gotta take what the HW guys gave you and make it work in the code.

Thanks so much. This will help a lot.
  #15   Spotlight this post!  
Unread 01-21-2006, 05:57 PM
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,163
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: toggle switch

I've never found contact bounce to be a problem on any joystick button, or on most separate pushbuttons we use. However, we did have to add debounce code for one of our foot pedals a couple of years ago.
Closed Thread


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 12-01-2005 10:20 AM
Toggle switch with pneumatic The yellowdart Programming 2 02-21-2005 02:08 PM
Pneumatics on a toggle switch Idaman323 Programming 3 02-15-2005 06:42 PM
LEDs in switch box. ZZII 527 Electrical 5 03-06-2004 07:39 PM
3 way toggle switch programming LeadRiccardoT Programming 3 02-17-2003 02:02 AM


All times are GMT -5. The time now is 07:12 PM.

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


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi