Joystick Buttons

if(vexRT[Btn7U] == 1){
motor[port2] = 127;
else
motor[port2] = 0;
}
if(vexRT[Btn7D] == 1){
motor[port2] = -127;
else
motor[port2] = 0;

The code of above is what my team is using to drive the robot. Some problems we noticed would be that in a blank template the program runs fine. While in the competition program it glitches. When I mean by glitch it runs fine then a slight delay happens. This continues when we press either button. Why is this happening? How should we fix this problem, and how do you guys program to do the same task we are doing? Our task is that when 7u is pressed it goes clockwise. If we wanted to go counter clockwise then we would press 7D.

Firstly, is there a reason why you have the “else motor[port7]” bits when your other lines are referring to motor[port2] ?

I am by no means an expert programmer, but I know RobotC gets a bit pedantic about syntax some times, so perhaps it might be worth trying:


if (vexRT[Btn7U] == 1) {
motor[port2] = 127; 
} else if(vexRT[Btn7D] == 1) {
motor[port2] = -127;
} else {
motor[port7] = 0;
}

This code would also mean that if you happened to push both 7U and 7D at the same time, 7U would take precedence and it would spin clockwise. I would also probably have motor[port2] = 0; as well in the last set of brackets so that if you are not pushing either 7U or 7D, then the motor is not spinning.

No doubt someone else will ask for more information and then be able to give you a better answer, but that is as much as I can think of given the information provided. The main question I would ask is to describe this ‘delay’ in more detail - how long is it stopping for, does it then continue spinning afterwards, or is it stopping completely, etc.

The motor times out for about 1 second then goes back to running. Also we never tried pressing both at the same time.

I don’t understand the different ports 7 vs 2 either, but in the first sample of code the second if/else condition would set the port output to 0 after the first condition set it to 127. I’m not sure why these logic conditions seem to work sometimes and then give the results you are describing i.e. the difference between the standalone template and competition. I have set up code several times in the first method and found my error later.

The second snippit should give you control as you desire to control the motor with the caution as pointed out that one button takes priority if both are pressed.

Kb

Whenever I use the buttons, I use this line of code, which can be easily adapted to most situations.


motor[port2] = vexRT(Btn7U) * 127 + vexRT(Btn7D) * -127;

This also ensures that if both buttons are pressed, it will send a value of zero.

any questions?

Very elegant and worth remembering. - Cheers Kb

You can also write :


motor[port2] = vexRT[Btn7u] ? 127 : vexRT[Btn7D] ? -127 : 0;

what is the purpose of the question mark? I never thought of writing it that way though. I always just did:

if Btn7D = 1 
{
  motor = 127;
}

It’s a shorthand way for if then else.

if (a > b ) {
    result = x;
}
else {
    result = y;
}

can be written as 

result = (a > b) ? x : y;

google for ternary operation.

magicode uses a second one as part of the “else” condition. It’s elegant code but compiles to the same instructions as using if-then-else.

This is a shorthand syntax called a ternary operation that is (more or less) equivalent to the following:

if (vexRT[Btn7u]) {
    motor[port2] = 127;
} else {
    if (vexRT[Btn7D]) {
        motor[port2] = -127;
    } else {
        motor[port2] = 0;
    }
}

Some people find them hard to read, but I like them because they can express selection logic in very compact code.

Cheers,

  • Dean

Lol practically the same response from both of you. Anyway thanks, I LOVE elegant code. I practically made a whole library of functions so that my main program is nice and simple.

wait you don’t have to say if(Btn7U = 1)? you can just say if(Btn7U)?

Interesting. I will probably use this from now on. I love elegant code. I pretty much have a dictionary of functions to keep my main tasks very simple. Also, I never new you could just say:

if(Btn7U)
{
  motor = 127;
}

I always thought you had to say:

if(Btn7U == 1
{
  motor = 127;
}

or am i mistaken?

In the older controllers, the buttons used to return values of 127, 0, or -127. Now, you can use them as if the buttons return true or false. So this:


if(vexRT[Btn5U]) motor[port1] = 30;

works fine.

Is this dictionary publicly available? :slight_smile:

not really. I mean its just a bunch of functions for like drive train, lifts, and various other tasks i use. they’re quite simple really.

Edit If you want I can go back and annotate everything so it makes sense and then I can upload it to here if you want. Most of the functions are for programming autonomous modes.

I bet you’re one of those APL guys, aren’t you?

Been a long time since I’ve seen any APL :wink:

My code tends to be very dense without much extra whitespace.

I much prefer:


if (something) x=5;

over

if (something) {
    x = 5;
}

or

if (something)
{
    x = 5;
}

It isn’t necessarily good programming style, but I think it expresses the behavior more succinctly and so it appeals to me.

Cheers,

  • Dean

Perl syntax can be nice too, since it lets you do post-conditionals.
x=5 if something;

I’ve not been able to get EasyC to do “short-circuit AND” correctly;
This would look something like this.
Btn7 AND (motor=127); // If Btn7 isn’t true, there is no point in doing the rest

Another method that does not use a change of flow if/then/else is this,
which only works if (true) = 1 and (false) = 0 and you can use them in math.
prev_value = motor = (prev_value) * !Btn7 + Btn7 * 127;

Please post if you have success with either of these methods.

if (vexRT[Btn7U] == 1) {
motor[port2] = 127;
} else if(vexRT[Btn7D] == 1) {
motor[port2] = -127;
} else {
motor[port7] = 0;
}

Do it in a while loop.

while (1==1)
{
if (vexRT[Btn7U] == 1) {
motor[port2] = 127;
} else if(vexRT[Btn7D] == 1) {
motor[port2] = -127;
} else {
motor[port7] = 0;
}
}

sorry im a bit rusty on robotc over the sumer… but I hope you know what I mean. while loops are very important.