![]() |
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 |
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? |
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 |
Re: Single Click On/Off Using the Trigger in EasyC
Quote:
|
Re: Single Click On/Off Using the Trigger in EasyC
Quote:
|
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. |
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) |
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. |
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. |
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!
|
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) |
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. |
Re: Single Click On/Off Using the Trigger in EasyC
Quote:
|
Re: Single Click On/Off Using the Trigger in EasyC
Ok, I got the code to work, bit it took a little bit of tweaking. It may be how I set it up though. Either way, I'm able to have the cathodes toggle on and off with the trigger. This is the code I used:
Code:
if(p1_sw_trig && toggle_state == 0) |
Re: Single Click On/Off Using the Trigger in EasyC
Even though I'm not a C programmer, I've done this with my TI-89 before when making a program...
It would be something like this I guess Code:
//NOTE: THIS IS NOT C CODING AT ALL AND IF YOU USE THIS, YOU WILL GET ERRORS!!! |
Re: Single Click On/Off Using the Trigger in EasyC
Quote:
IF (1 && 0 == 0) IF (0 && 0 == 0) IF (0 && 1 == 0) IF (1 && 1 == 0) Instead of what you really want which is more like this IF (0 = 1) && (0 = 0) False IF (1 = 1) && (0 = 0) True IF (0 = 1) && (1 = 0) False IF (1 = 1) && (1 = 0) False Try this syntax instead and take out the delay and see what happens. if((p1_sw_trig == 1 ) && (toggle_state == 0)) |
| All times are GMT -5. The time now is 10:22. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi