|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
||||
|
||||
|
Single Click On/Off Using the Trigger in EasyC
I've posted a few pictures of my senior project, and before my presentation I've been working with Easy C to get the cathodes that are attached to my robot to light up and stay lit after a single click of the left trigger. I then wanted the cathodes to turn off once the trigger was clicked again. The cathodes are connected to the RC using a Spike Relay. I have been using EasyC to program my robot. I do not have the laptop that is used for programing with me but I believe my current code is:
if(p1_sw_trig && n == 0) { OItoRelay(1,1,1,1); n = 1; } if(p1_sw_trig && n == 1) { n = 0; } This turns the light on with a single button click, but the cathodes do not always turn off with the next click. I was wondering if anyone knew how to make this work, rather than have it work half the time or using two buttons to turn it on and off. Thanks in advance! -Ira |
|
#2
|
||||
|
||||
|
Re: Single Click On/Off Using the Trigger in EasyC
I've never written C where && means both values are equal to the value on the right of the ==. I've always done it if (p1_sw_trig == 0 && n == 0)...
Maybe try that? |
|
#3
|
||||
|
||||
|
Re: Single Click On/Off Using the Trigger in EasyC
Since the value of a trigger is already essentially a boolean you can just use it as itself, no == needed. So what he has is the same thing as (p1_sw_trig == 1 && n == 0)
|
|
#4
|
||||
|
||||
|
Re: Single Click On/Off Using the Trigger in EasyC
Yep, I have the variable p1_sw_trig set when the trigger is pressed.
|
|
#5
|
||||
|
||||
|
Re: Single Click On/Off Using the Trigger in EasyC
I dunno if this is causing the problem, but I'd put in a little time delay before it can switch states, because, as coded, if you held the trigger down too long the system would 'flicker' between the two states.
I have no experience with WPIlib, so I do not know if this could be the issue, but you don't set the relay to zero in your second if statement, so I would think it might just stay at that value set earlier. Again, I have no experience with WPIlib, so I don't know if this could be the issue. |
|
#6
|
||||
|
||||
|
Re: Single Click On/Off Using the Trigger in EasyC
Quote:
I also tried setting the relay to zero in the second statement, and the results were not too good. I don't remember the exact response though. This current code does work, but I am missing a step in turning it back off. It does turn off on some presses, but sometimes it does not for a few or many clicks. |
|
#7
|
|||||
|
|||||
|
Re: Single Click On/Off Using the Trigger in EasyC
You don't really want to toggle the state of the lights when the trigger is active. You want to toggle when the trigger becomes active. You need to keep track of whether or not the trigger is pressed, and only do the toggle action when it is pressed now, but was not pressed last time.
The code bear24rw posted is a good example of how to do it. |
|
#8
|
||||
|
||||
|
Re: Single Click On/Off Using the Trigger in EasyC
I'm still very much a beginner at programming for a robot, and haven't really coded anything in the last few months. If it is at all possible, can someone give a breakdown of the code that Bear24rw posted. I just want to make sure that I understand what is going on, as I am still a bit fuzzy. Thanks to everyone who's helped me so far!
|
|
#9
|
||||
|
||||
|
Re: Single Click On/Off Using the Trigger in EasyC
When I need to do something like this I write the program so that it waits until the first cycle after I let go of the button to toggle it. This way it is like:
Code:
if (pressed)
{
if (not pressed)
toggle;
}
|
|
#10
|
||||
|
||||
|
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. Last edited by Phalanx : 31-05-2007 at 09:13. |
|
#11
|
||||
|
||||
|
Re: Single Click On/Off Using the Trigger in EasyC
Quote:
|
|
#12
|
||||
|
||||
|
Re: Single Click On/Off Using the Trigger in EasyC
This how we did a click on click off
Code:
if(CLAW_BUTTON == 1 && claw_toggle_state == 0) // Code to handle toggle
{
CLAW_OPEN = !CLAW_OPEN;
CLAW_CLOSED = !CLAW_CLOSED;
}
claw_toggle_state = CLAW_BUTTON;
|
|
#13
|
||||
|
||||
|
Re: Single Click On/Off Using the Trigger in EasyC
Quote:
This means in my code I should have: Code:
if(p1_sw_trig && cathode_toggle_state == 0)
{
OItoRelay(1,1,1,1);
}
cathode_toggle_state = p1_sw_trig;
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| multiple autonomous modes using easyc | cerberus170 | Programming | 3 | 20-02-2007 15:01 |
| Using printf() as UserCode in EasyC 2.0 | tacman1123 | Programming | 2 | 31-05-2006 10:01 |
| Using SMC Single Selenoid Valve from last year? | Sachiel7 | Pneumatics | 3 | 20-01-2005 21:31 |
| Is Anyone Using Motors to Hoist themselves off the platform on the bar? | xxlshortys | Motors | 36 | 25-02-2004 01:06 |
| using single solenoid like a double | SteveO | Programming | 14 | 02-02-2004 22:51 |