Thread: toggle switch
View Single Post
  #3   Spotlight this post!  
Unread 16-01-2006, 15:51
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.