View Single Post
  #12   Spotlight this post!  
Unread 31-05-2007, 09:11
Phalanx's Avatar
Phalanx Phalanx is offline
Formerly Team 1089 (Mercury)
AKA: Michael Reffler
FRC #5431 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jun 2005
Rookie Year: 1999
Location: Lewisville, TX (previously NJ)
Posts: 384
Phalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond repute
Re: Single Click On/Off Using the Trigger in EasyC

This is how we do it, although we have since developed and function call method that is a little more sophisticated than this sample. But this sample does work.

static unsigned char p1_sw_trig_prev = 0;

if ((p1_sw_trig == 1) && (p1_sw_trig_prev == 0))
relay1_fwd = !relay1_fwd;

p1_sw_trig_prev = p1_sw_trig;

So what is all of this and what does it mean...

1) We define a static variable to hold the contents of the state of the button to be compared with on the next pass through the program loop.

2) We say if the trigger is being pressed and was previously in a released state then change the state of relay1_fwd. But if not don't do anything.

3) The relay1_fwd = !relay1_fwd statement causes relay1_fwd to change states. If relay1_fwd initially was zero, it becomes 1 and vice versa.

4) We then save the state of the trigger in our static variable to use in the comparison next time through.

What will happen now is every time you pull the trigger and release it will case the relay1_fwd to toggle between on and off.

What you are accomplishing here is making your trigger into a toggle switch of sorts. Click once it's on, Click again it's off, click again it's on, click again it's off and so on....

I hope this helps you to understand better what is happening.
__________________
Don't just ask the experts, become one!
Leadership is not about ability. It's about responsibility!
Diagonally Parked in a Parallel Universe. It's okay we do Quantum Physics



Last edited by Phalanx : 31-05-2007 at 09:13.