This code assumes that your robot has two powered wheels mounted opposite to each other and the left motor is reversed. Positive power to both motors would drive the robot forward.
#include "JoystickDriver.c" // Include the RobotC joystick driver file.
task main() {
while (true) { // infinitely loop
getJoystickSettings(joystick); // Update buttons and joysticks.
int turn = joystick.joy1_x1; // Get joystick 1's left x-axis value.
int power = joystick.joy1_y1; // Get joystick 1's left y-axis value.
motor[leftMotor] = power + turn;
motor[rightMotor] = power - turn;
}
}
To explain this, we can look at some of the different possible ways to push the joystick. For simplicity’s sake, I’m going to make all of the joysticks be pushed to a value of 100.
To drive forward, we push the joystick forward (positive y). This makes the value of the power variable +100. The turn variable is 0 because the joystick hasn’t moved left or right.
This effectively translate in the code to
motor[leftMotor] = 100 + 0;
motor[rightMotor] = 100 + 0;
which as we know, will drive the robot forward.
Likewise, to turn right in place, we can push the joystick to the right. This will cause the turn variable to be +100, but the power variable will be 0.
motor[leftMotor] = 0 + 100;
motor[rightMotor] = 0 - 100;
As you can see, this would make the right wheel spin backward and the left wheel spin forward, effectively turning the robot in place to the right.
Now, say we wanted to go forward and turn right at the same time. If we push the joystick to a positive value of 100 in both directions (x and y), we ge this:
motor[leftMotor] = 100 + 100;
motor[rightMotor] = 100 - 100;
The left motor will go forward at 100% power even though technically the code is trying to set it to 200. This is due to a physical limitation. However, the right motor will stay at 0, which will allow for what is sometimes called a swing turn.
Backing up and turning left work similarly.
I hope this helps. If you are doing something like holonomic omni wheel driving, let me know because that is quite different and requires a lot more explanation.