Help with joystick deadspace.

This is my first project working with Arduino, what a learning experience this has been.

I am using a joystick and a Megamoto shield driving a 12v DC motor.
Forward and reverse is controlled by PWM on either pin 3 and 11.

I have borrowed a code but have a deadspace issuse I cant figure out.

Other that that the code works great with variable speed & direction!

//
//
// Tank bot work in progress 30/12/10 version 0.5:


#include <Servo.h>



//turret and servo(s)

Servo scanservo ;
Servo Larmservo ;
Servo Harmservo ;


//motor pins and reverse

const int RTMPin     = 11 ;
const int RTRPin     = 3 ;

// pointometers/joystick

int potpin   = A3 ;
int joyx     = A4 ;

// variables
int potval  = 0 ;
int joyxval = 0 ;
int RTval   = 0 ;
int LTval   = 0 ;
int LDval   = 0 ;
int RDval   = 0 ;

/////////////////////////////////////////////////////////////////////////////////////

void setup() {

Serial.begin(9600);

pinMode (RTMPin, OUTPUT);
pinMode (RTRPin, OUTPUT);
pinMode (joyx,   INPUT);
}

/////////////////////////////////////////////////////////////////////////////////////

void loop()
{
  potval = analogRead(potpin);
  LDval = analogRead(joyx);
  RDval = analogRead(joyx);
  LDval = map (LDval, 530, 1023, 0, 255);
  RDval = map (RDval, 500, 0, 0, 255);


if (analogRead(joyx)<500)
{
LTval = analogRead(joyx);
LTval = map(LTval, 0, 500, 0, 255);
RTval = analogRead(joyx);
RTval = map(RTval, 0, 500, 0, 255);


LTval = (LTval - constrain (RDval, 0, 255));
RTval = (RTval - constrain (LDval, 0, 255));

analogWrite  (RTMPin, constrain (LTval, 0, 255));
digitalWrite (RTRPin, HIGH);
}
if (analogRead(joyx)>530)
{
LTval = analogRead(joyx);
LTval = map(LTval, 530, 1023, 0, 255);
RTval = analogRead(joyx);
RTval = map(RTval, 530, 1023, 0, 255);


LTval = (LTval - constrain (RDval, 0, 255));
RTval = (RTval - constrain (LDval, 0, 255));

analogWrite  (RTMPin, constrain (LTval, 0, 255));
digitalWrite (RTRPin, LOW);
}

delay(150);

}

Thanks!

deadspace issue? can you elaborate?

I think he means when you put small amount of power and it does not move at all. My guess is that you have map values to 0, and at very low voltage, the motor just can't get enough power to turn the wheel.

something I did with a joystick recently, I have sparkfun joystick shield and needed a deadspace. mabye this code could help. I widened the gap between the forward and reverse so it would work cleanly. Close the gap and that might help, your code doesn't have a gap so I'd agree about not mapping enough current.

It seems that PWM isn't shutting off when the joystick is centered. It seems to work when the joystick moves slowly

Here is a video to help explain. Thanks for the help.

HOT DAMN figured it out!

I set the delay to 0 and now it works perfect!
Just a random thought and got it.
Thanks for looking!

//
//
// Tank bot work in progress 30/12/10 version 0.5:


#include <Servo.h>



//turret and servo(s)

Servo scanservo ;
Servo Larmservo ;
Servo Harmservo ;


//motor pins and reverse

const int RTMPin     = 11 ;
const int RTRPin     = 3 ;

// pointometers/joystick

int potpin   = A3 ;
int joyx     = A4 ;

// variables
int potval  = 0 ;
int joyxval = 0 ;
int RTval   = 0 ;
int LTval   = 0 ;
int LDval   = 0 ;
int RDval   = 0 ;

/////////////////////////////////////////////////////////////////////////////////////

void setup() {

Serial.begin(9600);

pinMode (RTMPin, OUTPUT);
pinMode (RTRPin, OUTPUT);
pinMode (joyx,   INPUT);
}

/////////////////////////////////////////////////////////////////////////////////////

void loop()
{
  potval = analogRead(potpin);
  LDval = analogRead(joyx);
  RDval = analogRead(joyx);
  LDval = map (LDval, 530, 1023, 0, 255);
  RDval = map (RDval, 500, 0, 0, 255);


if (analogRead(joyx)<500)
{
LTval = analogRead(joyx);
LTval = map(LTval, 0, 500, 0, 255);
RTval = analogRead(joyx);
RTval = map(RTval, 0, 500, 0, 255);


LTval = (LTval - constrain (RDval, 0, 255));
RTval = (RTval - constrain (LDval, 0, 255));

analogWrite  (RTMPin, constrain (LTval, 0, 255));
digitalWrite (RTRPin, HIGH);
}
if (analogRead(joyx)>530)
{
LTval = analogRead(joyx);
LTval = map(LTval, 530, 1023, 0, 255);
RTval = analogRead(joyx);
RTval = map(RTval, 530, 1023, 0, 255);


LTval = (LTval - constrain (RDval, 0, 255));
RTval = (RTval - constrain (LDval, 0, 255));

analogWrite  (RTMPin, constrain (LTval, 0, 255));
digitalWrite (RTRPin, LOW);
}

delay(0);

}

HOT DAMN figured it out!

I set the delay to 0 and now it works perfect!

I guess that's one way to do it.

If you don't want to delay for any length of time, why are you calling delay?

Borrowed a code and removed a lot that I didn't need.Guess I should have removed more. :wink:
NEWB here.