|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
||||
|
||||
|
If there is a thread about this please redirect me to it.
I have been fiddling around with this piece of code for quite a while (to me 1 week during build season is much too long). I have come up with an alternative but I found it way too messy for the drivers of the bot. Here is the unhappy code. //somewhere at the top of user_routines.c unsigned static int countify = 0; //else where in the file if (p1_sw_top == 1){ ++countify; if (countify % 2 == 0){ relay1_fwd = 1; relay1_rev = 0; } if (countify % 2 != 0){ relay1_fwd = 0; relay1_rev = 1; } What the above code is supposed to do is have the driver have to only hit the button once for it to do something then the driver hits it again for it to reset itself or do something else. Idealy we don't want the driver to have to tap the top switch on the joystick. I only get a spaztastic numatic cylinder. I already know the cause is due to the fact that the value for p1_sw_top is read about every 26.2 ms so it goes through this code like a hot knife through butter. Is there a better way to do this? The alternate code I am using is this if ((p1_sw_top == 1) && (p1_sw_trig == 1)){ relay1_fwd = 1; relay1_rev = 0; } if ((p1_sw_top == 1) && (p1_sw_trig == 0)){ relay1_fwd = 0; relay1_rev = 1; } This makes it so I can have multiple buttons set up this way. This code does what its supposed to which is when someone hits the trigger then the button and something happens then they hit the button without the trigger and the inverse happens. |
|
#2
|
|||||
|
|||||
|
Re: One button wonder
how about this
Code:
//somewhere at the top of user_routines.c
int buttonstate=0;
//then later....
if(buttonstate==0 && p1_sw_trig==1)
{
//do some stuff here
buttonstate=1;
}
if(p1_sw_trig==0)
{
buttonstate=0
}
![]() |
|
#3
|
||||
|
||||
|
Re: One button wonder
You want to switch the solonoids once, then skip over the code each following loop, untill you let go of the trigger and push it down again, right?
Our team is doing exactly that with our pneumatics. Here's how we're doing it. (blatantly stolen from the v2.4 default code )Code:
//somewhere at the top of user_routines.c
int latch = 0;
//somewhere in default_routine()
if(p1_sw_trig)
{
latch++;
if(latch==1)
{
relay1_fwd=!relay1_fwd;
relay1_rev=!relay1_fwd;
}
latch=1;
}
else
{
relay1_fwd=relay1_rev=0;
latch=0;
}
![]() |
|
#4
|
|||||
|
|||||
|
Re: One button wonder
Yeah, that looks good. Except what is with the latch++ in there? I would think it would work better if you took that out... Then again like I said, Im sleepy. I think I will go to bed now.
[disregard]We might get a snow day tomorrow here in Alaska. Theyre pretty rare. We have to have a good six inches everywhere before they even consider it. Yeah I know that was irrelevant . Sorry.[/disregard] |
|
#5
|
||||
|
||||
|
Re: One button wonder
I dunno about that it seems a bit iffy to me but I'll try it out tonight when I can get my hands on the programing computer for the teams robot. Thanks.
Scratch that... It looks good to me except for the latch = 1; at the end of the if statement but other than that it looks good. 5 ish AM is not exactily my prime for thinking. Last edited by Fourevilmonkies : 10-02-2005 at 04:56. |
|
#6
|
||||
|
||||
|
Re: One button wonder
Here's what seems to be the problem, at least to me. You're doing this on your slow loop, every time, right? Because it seems to me that, with the if statement how it is, it will re-analyze every time it goes through the slow loop, and keep turning the value "on" and "off" until you let go of the joystick button. This makes it basically random, which value it's going to end up with when you let go. I think in most cases, the slow loop still runs faster than a player can reasonably press and release the joystick button.
So. You want to activate this routine only when you have _just_ turned on the button, and not again and again as long as you have the button held. Better would be to define a global variable unsigned char ButtonON = 0; and then check to see if the button was already on during the last loop before you act on it. So inside your slow loop, here's what you would do: unsigned char Countify = 0; if (p1_sw_top == 1 && ButtonON = 0){ ButtonOn = 1; if (relay1_fwd == 0 || relay1_rev == 1){ relay1_fwd = 1; relay1_rev = 0; } else{ relay1_fwd = 0; relay1_rev = 1; } } else if (ButtonON == 1 && p1_sw_top == 0) ButtonON = 0; This means, that if your button was on the last time you came through the slow loop and it's still on now, it doesn't count as another press of the button. Good luck! --Zarf |
|
#7
|
|||||
|
|||||
|
Re: One button wonder
Thats the same thing that Bleric's code does, except I am still wondering about the latch++ and I am thinking you might want to change the if(latch==1) to say if(latch==0). Then again maybe I am just reading it wrong.
|
|
#8
|
||||
|
||||
|
Re: One button wonder
Any of the above code segments will work =P
Hmm... come to think of it, you're right russell. latch++ doesn't really do anything ![]() In the original code, we didn't have "latch=1" at the end of the first if statement. Meaning, latch would count the number of cycles we'd held the button down. "if(latch==1)" made sure we only executed that bit of code on the first loop we'd held the button. Later, we added latch=1 to make sure that variable wouldn't wrap around to zero after 32,767 loops That being said... I'll take out that "latch++" stuff as soon as I can get my hands on the code. ![]() |
|
#9
|
||||
|
||||
|
Re: One button wonder
Ask any of my students what my favorite question is and they'll answer "What is the definition of a button tap?". I grilled them with that question until they finally understood it. A button tap is when the button is currently pressed and it wasn't pressed the last time you looked at it. That's exactly what you want. You want to transition the relay only once per button press.
Code:
static char p1_sw_top_prev = 0;
// check if the button is pressed and last loop it wasn't pressed
if ((p1_sw_top == 1) && (p1_sw_top_prev == 0))
{
// now transition your relay
if (relay1.fwd == 1)
{
relay1.fwd = 0;
relay1.rev = 1;
}
else
{
relay1.fwd = 1;
relay1.rev = 0;
}
}
p1_sw_top_prev = p1_sw_top;
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Looking to borrow a button maker... | 1685isalive | General Forum | 0 | 31-01-2005 14:03 |
| disabling a button | gomez6760 | Programming | 1 | 12-08-2004 11:54 |
| Button Programing | BobcatProgramer | Programming | 6 | 25-02-2004 16:43 |
| which button machine do you use and do you like it? | KenWittlief | General Forum | 10 | 16-10-2003 00:01 |
| Select button and user mode | n[ate]vw | Programming | 2 | 20-01-2003 16:33 |