Programming Help for Controller

So right now my students and I are trying to figure out that if a button is pressed on the controller it goes backwards for certain time and speed. But if they program it, then when they use the joysticks on the controller it is affected by making the robot all jittery if they run it. Code will be listed below:

#pragma config(Motor, motor1, LeftDrive, tmotorVexIQ, PIDControl, driveLeft, encoder)
#pragma config(Motor, motor2, BottomLift, tmotorVexIQ, PIDControl, reversed, encoder)
#pragma config(Motor, motor3, TopLift, tmotorVexIQ, PIDControl, reversed, encoder)
#pragma config(Motor, motor4, Strafe, tmotorVexIQ, PIDControl, encoder)
#pragma config(Motor, motor8, MiddleLift, tmotorVexIQ, PIDControl, reversed, encoder)
#pragma config(Motor, motor12, RightDrive, tmotorVexIQ, PIDControl, reversed, driveRight, encoder)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//

task main()
{
//Drive Controls
int threshold = 10;
while (true)
{
int channelA = getJoystickValue(ChA);
int channelD = getJoystickValue(ChD);
//Threshold for Channel A
if (channelA > threshold || channelA < -threshold)
{
setMotorSpeed(LeftDrive, getJoystickValue(ChA)); //
}
else
{
setMotorSpeed(LeftDrive, 0); //
}
//Threshold for Channel D
if (channelD > threshold || channelD < -threshold)
{
setMotorSpeed(RightDrive, getJoystickValue(ChD));
}
else
{
setMotorSpeed(RightDrive, 0);
}

//Lift Controls
if(getJoystickValue(BtnLUp) == 1)
{
setMotorSpeed(TopLift, 100);
setMotorSpeed(MiddleLift, 100);
setMotorSpeed(BottomLift, 100);
}
else if(getJoystickValue(BtnLDown) == 1)
{
setMotorSpeed(TopLift, -100);
setMotorSpeed(MiddleLift, -100);
setMotorSpeed(BottomLift, -100);
}
else
{
setMotorSpeed(TopLift, 0);
setMotorSpeed(MiddleLift, 0);
setMotorSpeed(BottomLift, 0);
}

//Strafe Control
if(getJoystickValue(BtnRUp) == 1)
{
setMotorSpeed(Strafe, 100);
}
else if(getJoystickValue(BtnRDown) == 1)
{
setMotorSpeed(Strafe, -100);
}
else
{
setMotorSpeed(Strafe, 0);
}

//Tongs Routine
if(getJoystickValue(BtnFUp) == 1)
{
setMotorSpeed(LeftDrive, -50);
setMotorSpeed(RightDrive, -50);
}
else
{
setMotorSpeed(LeftDrive, 0);
setMotorSpeed(RightDrive, 0);
sleep(1500);
}
}
}

You have no sleep() command with the drive backward. That means it won’t drive backward unless the button is held down instead of driving backward for the given time.

As for the jittering, that is because of your else command for BtnFUp. If you use the joystick and don’t press BtnFUp, the program sets the motors to they joystick values for a moment, then 0 for a sleep(1500), then the joystick values for a moment, then sleep(1500), etc.

The easiest fix is to, in the “Tongs routine,” move the sleep(1500) line to the if instead of the else, and delete the entire else part.

Thank callen, we just did it but it still jitters with the advice we followed. The actual code was my fault because we accidentally went to an older version we used for documentatiion

Here’s then new code, but the jittering issues persist.

#pragma config(Motor, motor1, LeftDrive, tmotorVexIQ, PIDControl, driveRight, encoder)
#pragma config(Motor, motor2, BottomLift, tmotorVexIQ, PIDControl, reversed, encoder)
#pragma config(Motor, motor3, TopLift, tmotorVexIQ, PIDControl, reversed, encoder)
#pragma config(Motor, motor4, Strafe, tmotorVexIQ, PIDControl, encoder)
#pragma config(Motor, motor8, MiddleLift, tmotorVexIQ, PIDControl, reversed, encoder)
#pragma config(Motor, motor12, RightDrive, tmotorVexIQ, PIDControl, reversed, driveRight, encoder)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//

task main()
{
while(true)
{
int threshold = 10;
int channelA = getJoystickValue(ChA);
int channelD = getJoystickValue(ChD);
//Threshold for Channel A
if (channelA > threshold || channelA < -threshold)
{
setMotorSpeed(LeftDrive, getJoystickValue(ChA)); //
}
else
{
setMotorSpeed(LeftDrive, 0); //
}
//Threshold for Channel D
if (channelD > threshold || channelD < -threshold)
{
setMotorSpeed(RightDrive, getJoystickValue(ChD));
}
else
{
setMotorSpeed(RightDrive, 0);
}

//Lift Controls
if(getJoystickValue(BtnLUp) == 1)
{
setMotorSpeed(TopLift, 100);
setMotorSpeed(MiddleLift, 100);
setMotorSpeed(BottomLift, 100);
}
else if(getJoystickValue(BtnLDown) == 1)
{
setMotorSpeed(TopLift, -100);
setMotorSpeed(MiddleLift, -100);
setMotorSpeed(BottomLift, -100);
}
else
{
setMotorSpeed(TopLift, 0);
setMotorSpeed(MiddleLift, 0);
setMotorSpeed(BottomLift, 0);
}

//Strafe Control
if(getJoystickValue(BtnRUp) == 1)
{
setMotorSpeed(Strafe, 100);
}
else if(getJoystickValue(BtnRDown) == 1)
{
setMotorSpeed(Strafe, -100);
}
else
{
setMotorSpeed(Strafe, 0);
}

//Tongs Routine
if(getJoystickValue(BtnFDown) == 1)
{
setMotorSpeed(RightDrive, -50);
setMotorSpeed(LeftDrive, -50);
sleep(1500);
}
}
}

Let’s see. I’ll just comment on everything as I go and hopefully it will pop out.

  1. Why is “int threshold = 10;” inside the while loop? You would only want to reset it to 10 each time if you’re worried it changed. At least move it above the while loop. It will make the code run the tiniest bit faster, but mostly this is for better coding practices. Personally, I would be more likely to do “#define threshold 10” way up above instead so there is no way to change it and so that everywhere it’s used it gets sped up. (If it’s an int, the int’s value needs to be checked each time; if it’s defined, the value replaces the name at compilation.)

  2. You don’t really want to check values repeatedly when you expect them to be the same. So
    setMotorSpeed(LeftDrive, getJoystickValue(ChA));
    and
    setMotorSpeed(RightDrive, getJoystickValue(ChD));
    would be better as
    setMotorSpeed(LeftDrive, channelA);
    setMotorSpeed(RightDrive, channelD);

​Hmm… Neither of those is much of an issue, just better coding practices. I’m not seeing anything in the current code that should cause the jittering. If you leave Tongs Routine in there but comment it out, does the robot run fine? I’m not asking about another program, but this specific one. Now that I look back through for anything that might stand out, I see both LeftDrive and RightDrive have driveRight in their #pragma statements. It looks odd to me, but I don’t program in RobotC. That, however, should still be an issue if it is one even with the commented out Tongs Routine, which is one of the reasons I suggested commenting out instead of using a different program with a copy of the remote control without Tongs Routine.