Delta II | Programming Help

Although I’m a programmer, I might need some help with the other programmers out there…
Can someone build a code that involves where you press a button once, the motor goes to 127, then if you press it a second time, the motor goes to 0. I would like it to be a repeated action, and I would like to be able to still drive the robot.
The button I would like to be pressed is vexRT[Btn5U], with the motor being motor[motor1]


task main() {
  bool btn5ULast,
    mtrOn;
  while(true) {
    if(vexRT[Btn5U] && !btn5ULast) {
      mtrOn = !mtrOn;
      motor[port1] = (mtrOn) ? 127 : 0;
    }
    btn5ULast = vexRT[Btn5U];
    wait1Msec(20);
  }
}

This should work; it might have syntax errors (written on iPhone), but the concept works. If you’d rather avoid a ternary statement, you can replace line 7 (the ternary statement) with this:


motor[port1] = 127 * mtrOn;

but that isn’t as much fun.

With the raw code listed above, pressing the button would be unreliable, due to it evaluating every time the statements are called up, which is like every 20 ms. A delay set up like I did here will give you a reliable press.

Yes, my code will only execute every 20 ms, but that’s also the refresh rate of the joystick. And I guarantee you won’t be able to press the button for less than 20 milliseconds consistently.
The way the delay is set up will not work if the entire loop takes less than 350 ms to execute. You clear the timer at the end of every iteration, so it goes back to zero. If you move the clearTimer(T1) to inside the if statement, that should work. Also, the second version of code, with a delay instead of a wait statement, is not multitasking-friendly; it never released the CPU, so it will be interrupted somewhere in the middle of the loop.

Would the boolean expression setting mtrOn not equal to itself work? Since there are only two values associated with a boolean value, I guess it could, but in case it does not, possibly instead state (Line 6 of Code):


   mtrOn = (mtrOn) ? false : true;

Therefore, if mtrOn is true, it becomes false, as well as vice versa.

Yes, setting a Boolean variable to the opposite of itself works. I’ve done the same thing with LEDs to make them flash.

Thank you. I was unclear on the issue. That’s certainly an interesting way of looking at the logic. I’ll have to remember that for future reference.

Thanks! I’ll be checking that code and possibly adding it to our program. This is very useful, thank you!

I just added it and compiled the program, it’s a yellow warning saying;


*Warning*:Invalid '=' operation for types 'bool' and 'word'

It is marked in the code below


task main() {
  bool btn5ULast,
    mtrOn;
  while(true) {
    if(vexRT[Btn5U] && !btn5ULast) {
      mtrOn = !mtrOn;
      motor[port1] = (mtrOn) ? 127 : 0;
    }
    btn5ULast = vexRT[Btn5U];// <---
    wait1Msec(20);
  }
}

Oh, that’s an annoying thing with RobotC. Joystick digital buttons are word variables, which are functionally identical to bool variables (they do the exact same thing). Just change the bool prefix on btn5ULast to word and that will fix it.


bool mtrOn = false;
word btn5ULast = false;

Also, originally I took advantage of one of RobotC’s features that automatically sets all variables to 0 when the program is executed. I fixed that in the above snippet of code for the sake of developing good programming habits. If you read a variable before you write to it, always initialize it to 0.

K thank you :slight_smile:

No problem, glad I could help!