[FTC]: Toggle controls

Is it possible to toggle the controls?
i have a couple of servos that need it

Can you provide additional details about what is meant by “toggle the controls?”

Thanks,

Michael

Push a button…the servo goes there
release the button and it says there
push again and it goes to the starting position
release and it stays there
ex)

animation made by me

Great animation! Thanks.

Which programming language are you using?

Michael

Robot C

OK. So what you want is that when u press a button, let’s assume button 1, then the servo moves to one position. if you press another button, the servo moves to an original position.

So, the code has is a tad bit more complex.

int sum = 2;

if(joy2Btn(1)) // if button one is pressed
{
sum = sum +1;
}

int calcremainder = sum % 2; // This line calculates the remainder of sum/2.

if (calcremainder = 0)
{
servo[Servo1] = 100;
}
else
{
servo[Servo1] = 0;
}

So the code above basically does this. Each time you hit the button, the program counts it. I know there’s an official counter in the program, but i’m ghetto like this.

Then, it takes the number of times the button has been hit, and it divides by 2. So, then the value can either be 0 or 1. So each time you hit the button, the value switches between 0 and 1, so when the value is 0, you have a position, when it is 1 you have a position.

That’s the logic.
There might be a better way, but this is one possibly option.

it just makes the arm reverse itself

That code above should do what the animation does. But you will have to put in the correct servo values.

yeah, i put that in and reset the servo values, but it just reversed the servo…
here’s my code

int sum = 2;

if(joy2Btn(2)) // if button 2 is pressed
{
sum = sum +1;
}

int calcremainder = sum % 2; // This line calculates the remainder of sum/2.

if (calcremainder == 0) <this was an error in your lines...it needed a double=
{
servo[ServoB] = 100;
}
else
{
servo[ServoB] = 0;
}

What do you mean it makes the servos reverse themselves?

Also, I don’t see how that works if the counter resets itself each time, it wouldn’t stay I don’t believe

int touch = 1;
//put variable before while loop

if(joy2Btn(1)) {
   if(touch == 1){
     touch = 0;
   }else{
     touch = 1;
   }
}

//you may want a really short wait here so when you press the button it does not switch back to original really quick

if(touch == 1){
  servo[ServoB] = 100;
}else{
  servo[ServoB] = 0;
}

ah yes. The counter would have to be outside the while loop.

That is a basic code.

Now there is an official counter which might be better than actually going in and creating another variable. It probably won’t make a huge difference, but whateva you want.

still doesn’t do anything
…thanks anaways guys
if you could figure it out, thanks for putting up with me

found this online, but i tried converting it and with my small ammount of programming knowledge, i didn’t get very far

#include <stdio.h>
 
int main() {
     int x;
     for (x = 0; x < 10; x++) {
          if(x == 1 || x == 4 ) {
               continue;
          }
          else if(x == 8) {
               break;
          }
          else {
               printf("x = %d", x);
          }
     }
 
     return (1);
}

The modulo ("%") function code will do what you want, but you really should look into state machines. The code should look more like:

int state=0;

while (true) {
… (add your getjoystick function here)
… (and any other code you have)

switch (state) {
case 0:
if(joy2Btn(1)) // if button one is pressed
{
// Move your server to the first position (aka state)
servo[Servo1] = 100;
state = 1;
}
break;
case 1:
if (joy2Btn(1) == 0) {
// if the button was released then we can wait for another
// button push
state = 2;
}
break;
case 2:
if(joy2Btn(1)) // if button one is pressed
{
// now move your servo to the other position (aka state)
servo[Servo1] = 0;
state = 3;
}
break;
case 3:
if (joy2Btn(1) == 0) {
// again, wait for the button to be released
// before we look for another push
state = 0;
}
break;
default:
state = 0;
break;
}

}

Hope this helps.

Paul Tan.

thanks…will try it tomorrow…that’s when we meet…

i get this when i try it…
http://img22.imageshack.us/img22/1182/1234567890qq4.th.jpg](http://img22.imageshack.us/my.php?image=1234567890qq4.jpg)

I get this when i run the code:
http://img22.imageshack.us/img22/1182/1234567890qq4.th.jpg](http://img22.imageshack.us/my.php?image=1234567890qq4.jpg)

Your error in the screenshot indicates that the servo is out of range (Exception Type: 'Servo out of range(34))

The error is in the line before the state=1; statement

I noticed in the top of the screenshot, your commented code uses ServoB instead of ‘servo1’. Did you do the motor/servo configurations correctly? Have you defined it as ServoB or as servo1?

Also, I noticed you did not add in the get joysticks command in while loop.

yeah just fixed that and hopefully i can get the bluetooth connected–it’s on the fritz

.:EDIT:.
oh yeah, it completely blocks off all other commands from the controllers…
but it does work…
and i figured out that our driver doesn’t want the toggle now…
all this for nothing unless he decides he wants it…
thanks guys