View Single Post
  #13   Spotlight this post!  
Unread 31-05-2007, 10:58
IraJason IraJason is offline
In Search of "Bang Head Here" Sign
AKA: Ira Goldberg
FRC #3632 (D-Tecs); FRC #1712 (Dawgma)
Team Role: Mentor
 
Join Date: Nov 2005
Rookie Year: 2005
Location: Lower Merion, PA
Posts: 283
IraJason has a brilliant futureIraJason has a brilliant futureIraJason has a brilliant futureIraJason has a brilliant futureIraJason has a brilliant futureIraJason has a brilliant futureIraJason has a brilliant futureIraJason has a brilliant futureIraJason has a brilliant futureIraJason has a brilliant futureIraJason has a brilliant future
Send a message via AIM to IraJason Send a message via MSN to IraJason
Re: Single Click On/Off Using the Trigger in EasyC

Quote:
Originally Posted by Phalanx View Post
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.
Thank you very much for the full breakdown! I'm pretty sure I can do this now. Thanks to everyone for all their help and I'll post about my results once I get the code loaded on the robot.
__________________
Many respectable physicists said that they weren't going to stand for this - partly because it was a debasement of science, but mostly because they didn't get invited to those sort of parties."
-Douglas Adams The Hitchhiker's Guide to the Galaxy

2007 Galileo #6 seed, 5th Alliance Captain, Semi-finalist
2007 Philly Regional #4 Seed, Philly Regional GM Industrial Design Award Winner
2006 Philly Regional Highest Rookie Seed,Philly Regional Rookie All Star