i have a 2 axis joystick, and have had the interfacing code done for a while(reads the joystick writes to a int in values 0 to 1023), but now im trying to use the 2 axis joystick to control 2 servos in differential setup(one on the left, one on right). so if i were to push the stick straight up that would make the robot go forward. if i lean the stick to the right, it would slow down the right wheel a bit and keep the left wheel at full speed to make it turn right.
ive tried two different versions of code, i cant get either to work: int YaxisVar and XaxisVar from both attempts of my code are the joystick inputs(0 to 1023).
if (YaxisVar > YCenterPnt + DeadZone || YaxisVar < YCenterPnt - DeadZone)
{
// Map values from potentiometers to a smaller range for the PWM motor controllers (0-255)
XaxisVar = map(XaxisVar, 0, 1023, 0, 180);
YaxisVar = map(YaxisVar, 0, 1023, 0, 180);
int lYaxisVar = YaxisVar;
int rYaxisVar = YaxisVar;
if (XaxisVar < 88)
{ // turning left, so slow-down left track
if (YaxisVar > 92)
{ // moving forward
lYaxisVar -= (180 - XaxisVar); // decreasing the value - moving it closer to the center-point - slows it down
}
if (YaxisVar < 88)
{ // moving in reverse
lYaxisVar += (180 - XaxisVar); // increasing the value - moving it closer to the center-point - slows it down
}
}
if (XaxisVar > 92)
{ // turning right, so slow-down right track
if (YaxisVar > 92)
{ // moving forward
rYaxisVar -= (180 - XaxisVar); // decreasing the value - moving it closer to the center-point - slows it down
}
if (YaxisVar < 88)
{ // moving in reverse
rYaxisVar += (180 - XaxisVar); // increasing the value - moving it closer to the center-point - slows it down
}
}
LServoMtr.write(lYaxisVar);
RServoMtr.write(rYaxisVar);
Serial.print(lYaxisVar);
Serial.print(" ");
Serial.println(rYaxisVar);
Serial.println();
}
//}
else
{
LServoMtr.write(90);
RServoMtr.write(90);
}
XaxisVar = analogRead(Xaxis); //Read X-Axis postion
YaxisVar = analogRead(Yaxis); //Read Y-Axis postion
SpeedVar = analogRead(SpeedPot); //Read Speed Pot postion
//XaxisVar=512;
//YaxisVar=1023;
// scale x and y axis so that both are zeroed at natural center of potentiometer, implement deadband
if(YaxisVar > YCenterPnt + DeadZone)//forward
{
throttle = map(YaxisVar, YCenterPnt + DeadZone, 1023, 90, 180); //when joy 100% up, no turn, this is 180(fullspeed) :)
if(XaxisVar < XCenterPnt + DeadZone)//left motor
{
steering = -map(XaxisVar, XCenterPnt + DeadZone, 1023, 90, 15);// reverse pot coordinates to agree with cartesian
}
else if(XaxisVar > XCenterPnt - DeadZone)// right motor
{
steering = map(XaxisVar, XCenterPnt - DeadZone, 0, 90, 135);// reverse pot coordinates to agree with cartesian
}
else // in X deadband
{
steering = 90;
}
}
else if(YaxisVar < YCenterPnt - DeadZone)//reverse
{
throttle = map(YaxisVar, YCenterPnt - DeadZone, 0, 90, 0); //when joy 100% down, no turn, this is 0(fullspeed reverse) :)
//
if(XaxisVar < XCenterPnt + DeadZone)//left
{
steering = map(XaxisVar, XCenterPnt + DeadZone, 1023, 90, 45);// reverse pot coordinates to agree with cartesian
}
else if(XaxisVar > XCenterPnt - DeadZone)// right
{
steering = map(XaxisVar, XCenterPnt - DeadZone, 0, 90, 135);// reverse pot coordinates to agree with cartesian
}
else// in X deadband
{
steering = 90;
}
}
else //in Y deadband
{
throttle = 90;
if(XaxisVar > XCenterPnt + DeadZone)//left
{
steering = map(XaxisVar, XCenterPnt + DeadZone, 1023, -90, -0);// reverse pot coordinates to agree with cartesian
}
else if(XaxisVar < XCenterPnt - DeadZone)// right
{
steering = map(XaxisVar, XCenterPnt - DeadZone, 0, 90, 180);// reverse pot coordinates to agree with cartesian
}
else// in X deadband
{
steering = 90;
}
}
LmtrSpd = abs(throttle + steering);
RmtrSpd = abs(throttle - steering);
Serial.print(LmtrSpd);
Serial.print(" ");
Serial.println(RmtrSpd);
Serial.println();
this one i know for sure the issue is in the mapping functions, but ive already tried changing the values it maps to, but no luck...
why cant i get these to work ? ive spent hours dont worry