PS2 controller RC Car code problem.

Hey guys,

I am trying to make a RC controlled car using Arduino and a PS2 DualShock Joystick.

I am using Bill Porter's PS2X Library: PlayStation 2 Controller Arduino Library v1.0 « The Mind of Bill Porter to get the readings form the controller and it works perfectly.
I am using the controllers left and right joysticks to control the PWM of the left and right motors.

The only problem I am suffering from is that when the joysticks are in the rest position (i.e. When I am not moving the joysticks) the values are unstable.
I have attached a picture of the Serial Monitor with the joystick readings in the rest position.
Due to these reading the motors move even when I am not moving the joystick.

The Code :

#include <PS2X_lib.h>

PS2X ps2x; //initialize controller

//int Lp = 7;
//int Ln = 4;
//int Rp = 8;
//int Rn = 12;
int El = 11;
int Er = 9;
int right_speed = 0;
int left_speed = 0;

void setup () {
  ps2x.config_gamepad(2,6,4,7, true, true); //controller pins
  //pinMode (Lp, OUTPUT); //motor pins
  //pinMode (Ln, OUTPUT);
  //pinMode (Rp, OUTPUT);
  //pinMode (Rn, OUTPUT);
  pinMode (El, OUTPUT);
  pinMode (Er, OUTPUT);
  
  digitalWrite (El,LOW);
  digitalWrite (Er,LOW);
  Serial.begin(9600);
}

void loop () {
  ps2x.read_gamepad(); 
  left_speed = ps2x.Analog(PSS_LY); //Left Y-Axis
  right_speed = ps2x.Analog(PSS_RY); //Right Y-Axis
  left_speed = 255 - left_speed;
  right_speed = 255 - right_speed;
  left_speed = map(left_speed, 0, 255, -255, 255);
  right_speed = map(right_speed, 0, 255, -255, 255);
  Serial.print(left_speed);
  Serial.print("\t");
  Serial.println(right_speed);
  
  if(ps2x.Button(PSB_L1) && ps2x.Button(PSB_R1)) {  //when L1 and R1 are pressed-drive motors
    analogWrite(Er, right_speed);
    analogWrite(El, left_speed);
  }
  
  else {
    analogWrite(Er, 0);
    analogWrite(El, 0);
  }
}

How should I modify the code so that the values of the rest position can be zero?

Thank you in advance,
SolidSnake31295

Can't you just program in a dead zone around stick-centre?

AWOL:
Can't you just program in a dead zone around stick-centre?

Sorry didn't get that what should I do?

You're mapping the whole range of the stick to +/- 255, so just take the centre of the original range, say 128, and put a band of +/- 10 around that that maps to zero.

AWOL:
You're mapping the whole range of the stick to +/- 255, so just take the centre of the original range, say 128, and put a band of +/- 10 around that that maps to zero.

How should I do that? Please Help

Think about using the >= and <= operators.

I have put an 'if' statement so that if the values are between -10 and 30 convert it into zero.
But what should I put in the 'Else' statement?
I have added a screenshot.

CODE:

#include <PS2X_lib.h>

PS2X ps2x;

//int Lp = 7;
//int Ln = 4;
//int Rp = 8;
//int Rn = 12;
int El = 11;
int Er = 9;
int right_speed = 0;
int left_speed = 0;

void setup () {
  ps2x.config_gamepad(2,6,4,7, true, true);
  //pinMode (Lp, OUTPUT);
  //pinMode (Ln, OUTPUT);
  //pinMode (Rp, OUTPUT);
  //pinMode (Rn, OUTPUT);
  pinMode (El, OUTPUT);
  pinMode (Er, OUTPUT);
  
  digitalWrite (El,LOW);
  digitalWrite (Er,LOW);
  Serial.begin(9600);
}

void loop () {
  ps2x.read_gamepad();
  left_speed = ps2x.Analog(PSS_LY);
  right_speed = ps2x.Analog(PSS_RY);
  left_speed = 255 - left_speed;
  right_speed = 255 - right_speed;
  left_speed = map(left_speed, 0, 255, -255, 255);
  right_speed = map(right_speed, 0, 255, -255, 255);
  if(-10<=left_speed<=30) {
    left_speed = 0;
  }
  //WHAT SHOULD I PUT IN THE ELSE STATEMENT??
  Serial.print("Left Joystick Value: ");
  Serial.print(left_speed);
  Serial.print("\t");
  Serial.print("Right Joystick Value: ");
  Serial.println(right_speed);
  
  if(ps2x.Button(PSB_L1) && ps2x.Button(PSB_R1)) {
    analogWrite(Er, right_speed);
    analogWrite(El, left_speed);
  }
  
  else {
    analogWrite(Er, 0);
    analogWrite(El, 0);
  }
}

Thanks in advance.

But what should I put in the 'Else' statement?

Nothing is necessary. This is a problem though:

if(-10<=left_speed<=30)

It doesn't do what you expect. Try this instead:

if((left_speed>=-10) && (left_speed<=30))

Thank you.. It works like a gem.

Why:

right_speed = 255 - right_speed;
  right_speed = map(right_speed, 0, 255, -255, 255);

?

why not simply   right_speed = map(right_speed, 255, 0, -255, 255);
or   right_speed = map(right_speed, 0, 255, 255, -255);?

Does it make a difference?

Thank you anyway

Does it make a difference?

Yes - a whole lot less typing.

AWOL:

Does it make a difference?

Yes - a whole lot less typing.

It didn't strike in my mind.

Thank You