Cannot get Joystick to drive dc motor

Hi All, first post here, brilliant forum and community.

Basically I am demo’ing on a small scale a skid style 4x4 lawnmower! As for hardware I have a freetronics Eleven, and a duinotech joystick shield v1.A.

Now I have collected some code from the web that is supposed to be skid steer. I have looked at the serial output and I can see my buttons and joystick working accordingly. The issue I am having is when I connect pin 11 on the Eleven to my DC motor driver signal, I would expect some kind of output when pressing the joystick, but alas I get nothing!

I was hoping for some help. I am drawing up a circuit now (but I am confident its right as its very simple), and here is the code:

/*
Simple motor control with square limit joystick

     Y(1023)
   |---------|
   |---------|
X(0)|---512---|X(1023)
   |---------|
   |---------|
      Y(0)
      
Left Motor Forward/Reverse = LMF/LMR
Right Motor Forward/Reverse = RMF/RMR
*/
int up_button = 2;
int down_button = 4;
int left_button = 5;
int right_button = 3;
int start_button = 6;
int select_button = 7;
int analog_button = 8;
int x_axis = A0;
int y_axis = A1;
int LMF = 11;
int LMR = A3;
int RMF = A4;
int RMR = A5;
int buttons[] = {up_button, down_button, left_button, right_button, start_button, select_button, analog_button};




volatile unsigned int X = 512, Y = 512; // pot values default 512 for center
unsigned int Xspeed = 0, Yspeed = 0; //motor speed default 0 for full stop
const int Xdeadzone = 5, Ydeadzone = 5; //amount of slack needed for pot values

void setup(){
 for (int i; i < 7; i++)
 {
  pinMode(buttons[i], INPUT);
  digitalWrite(buttons[i], HIGH);
 }
 pinMode(LMF, OUTPUT);
 pinMode(LMR, OUTPUT);
 pinMode(RMF, OUTPUT);
 pinMode(RMR, OUTPUT);
 Serial.begin(9600);
}

void loop()
{
 X = analogRead(x_axis);
 Y = analogRead(y_axis);
 

 Serial.print("UP = "),Serial.print(digitalRead(up_button)),Serial.print("\t");
 Serial.print("DOWN = "),Serial.print(digitalRead(down_button)),Serial.print("\t");
 Serial.print("LEFT = "),Serial.print(digitalRead(left_button)),Serial.print("\t");
 Serial.print("RIGHT = "),Serial.print(digitalRead(right_button)),Serial.print("\t");
 Serial.print("START = "),Serial.print(digitalRead(start_button)),Serial.print("\t");
 Serial.print("SELECT = "),Serial.print(digitalRead(select_button)),Serial.print("\t");
 Serial.print("ANALOG = "),Serial.print(digitalRead(analog_button)),Serial.print("\t");
 Serial.print("X = "),Serial.print(map(analogRead(x_axis), 0, 1000, -1, 1));Serial.print("\t");
 Serial.print("Y = "),Serial.print(map(analogRead(y_axis), 0, 1000, -1, 1));Serial.print("\n");  
 delay(100);

 if (X >= (512 + Xdeadzone))//Forward
 { 
   Xspeed = (X - 512) / 2; // 0 - 255
   if(Y > (512 + Ydeadzone)) //Left
   {
     Yspeed = (Y - 512) / 2;
     //analogWrite(LMF, Xspeed - Yspeed); analogWrite(RMF, Xspeed);
     digitalWrite(LMR, LOW); digitalWrite(RMR, LOW);
   }
   else if (Y < (512 - Ydeadzone)) //Right
   {
     Yspeed = (512 - Y) / 2;
     //analogWrite(LMF, Xspeed); analogWrite(RMF, Xspeed - Yspeed);
     digitalWrite(LMR, LOW); digitalWrite(RMR, LOW);
   }
   else
   {
     //analogWrite(LMF, Xspeed); analogWrite(RMF, Xspeed);
     digitalWrite(LMR, LOW); digitalWrite(RMR, LOW);
   }
 }

 else if (X <= (512 - Xdeadzone))//Reverse
 { 
   Xspeed = (512 - X) / 2;
   if(Y > (512 + Ydeadzone)) //Left
   {
     Yspeed = (Y - 512) / 2;
     digitalWrite(LMF, LOW); digitalWrite(RMF, LOW);
     //analogWrite(LMR, Xspeed - Yspeed); analogWrite(RMR, Xspeed);
   }
   else if (Y < (512 - Ydeadzone)) //Right
   {
     Yspeed = (512 - Y) / 2;
     digitalWrite(LMF, LOW); digitalWrite(RMF, LOW);
     //analogWrite(LMR, Xspeed); analogWrite(RMR, Xspeed - Yspeed);
   }
   else
   {
     digitalWrite(LMF, LOW); digitalWrite(RMF, LOW);
     //analogWrite(LMR, Xspeed); analogWrite(RMR, Xspeed);
   }
 }

 else // X is between 512 +- deadzone
 {
   if(Y > (512 + Ydeadzone)) // zero point turn Left
   {
     digitalWrite(LMF, LOW); analogWrite(RMF, Yspeed);
     digitalWrite(LMR, LOW); digitalWrite(RMR, LOW);
   }
   else if(Y < (512 - Ydeadzone))// zero point turn Right
   {
     //analogWrite(LMF, Yspeed); digitalWrite(RMF, LOW);
     digitalWrite(LMR, LOW); digitalWrite(RMR, LOW); 
   }
   else
   { // Full stop
     digitalWrite(LMF,LOW);
     digitalWrite(RMF,LOW);
     digitalWrite(LMR,LOW);
     digitalWrite(RMR,LOW);
   }
 }
}

Any help would be greatly appreciated!

Welcome to the Forum. Please read the two posts

How to use this forum - please read.
and
Read this before posting a programming question …

at the top of this Forum on guidelines for posting here, especially the use of code tags which make the code look

like this

when posting source code files. Also, before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read.

If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button.

:confused:

So I’ve worked on this today and have more questions that when I started!

Firstly to the wiring. It would appear the motor drivers would not turn on unless I used the 5v supply and ground from the Arduino. Is that right? I’d like to power these independently reducing the load on the Arduino, but when supplied with an independent voltage supply, they would not work. Any ideas?

Now for the coding…I’ve managed to adapt a bit of code I found on this forum to my particular setup which includes a joystick. Now in my setup ill have a single motor controller for the left two motors on the tank, and the same on the right. So as far as coding is concerned I will have two motor variables to work with, LEFT_MOTOR and RIGHT_MOTOR. I’ve managed to get forward direction working based on the code I clipped, but reverse is a little tricky and raises some issues for a newbie:

  1. How do I inverse the “reverse” values sent out the PWN pin? So when moving forward I’m essentially taking a value between 512 and 1023 and adding it to Y, so it is an increasing value, which makes sense as the more you press the throttle, Y, the faster I move forward. This issue I have is in reverse it doesn’t quite behave like I want it to, so I take Y and subtract 512 , which means at the very start of the reversing position where it should be reversing the slowest, I am sending a large value, and as I press more into reverse I am sending smaller and smaller numbers, meaning my reverse starts at full power and goes to nothing. I hope that makes sense!

  2. As far as actually driving the motor into forward and reverse settings, how exactly is this done? Do I need to have a motor controller with 2 PWN pins so I can send one set of signals for forward, and another for reverse? I am just trying to work out how I switch polarity if its hards wired. Hope that makes sense.

Here is my updated, and WIP code. Note I have commented out a lot as I am just building the config slowly getting things to work.

/*
Simple motor control with square limit joystick

      Y(1023)
    |---------|
    |---------|
X(0)|---512---|X(1023)
    |---------|
    |---------|
       Y(0)
       
 Left Motor Forward/Reverse = LEFT_MOTORS/LMR
 Right Motor Forward/Reverse = RIGHT_MOTORS/RMR
*/
int up_button = 2;
int down_button = 4;
int left_button = 5;
int right_button = 3;
int start_button = 6;
int select_button = 7;
int analog_button = 8;
int x_axis = A0;
int y_axis = A1;
int LEFT_MOTORS = 11;
int LMR = 12;
int RIGHT_MOTORS = 10;
int RMR = A5;
int buttons[] = {up_button, down_button, left_button, right_button, start_button, select_button, analog_button};



volatile unsigned int X = 519, Y = 510; // pot values default 512 for center
unsigned int Xspeed = 0, Yspeed = 0; //motor speed default 0 for full stop
const int Xdeadzone = 8, Ydeadzone = 8; //amount of slack needed for pot values

void setup(){
  pinMode(LEFT_MOTORS, OUTPUT);
  pinMode(LMR, OUTPUT);
  pinMode(RIGHT_MOTORS, OUTPUT);
  pinMode(RMR, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  X = analogRead(x_axis);
  Y = analogRead(y_axis);
  //Serial.print("UP = "),Serial.print(digitalRead(up_button)),Serial.print("\t");
  //Serial.print("DOWN = "),Serial.print(digitalRead(down_button)),Serial.print("\t");
  //Serial.print("LEFT = "),Serial.print(digitalRead(left_button)),Serial.print("\t");
  //Serial.print("RIGHT = "),Serial.print(digitalRead(right_button)),Serial.print("\t");
  //Serial.print("START = "),Serial.print(digitalRead(start_button)),Serial.print("\t");
  //Serial.print("SELECT = "),Serial.print(digitalRead(select_button)),Serial.print("\t");
  //Serial.print("ANALOG = "),Serial.print(digitalRead(analog_button)),Serial.print("\t");
  Serial.print("X = "),Serial.print(map(analogRead(x_axis), 0, 1023, 0, 1023));Serial.print("\t");
  Serial.print("Y = "),Serial.print(map(analogRead(y_axis), 0, 1023, 0, 1023));Serial.print("\n"); 
  delay(100);




//THIS SECTION WORKS FOR FORWARD CONTROL OF BOTH LEFT AND RIGHT MOROTS, DO NOT TOUCH*************

  if (Y >= (512 + Ydeadzone))//Forward
  { 
    Xspeed = (Y - 512) / 2; // 0 - 255
    if(Y > (512 + Ydeadzone)) //Forward both Motors
    {
      Yspeed = (Y - 512) / 2;
      analogWrite(LEFT_MOTORS, Yspeed); analogWrite(RIGHT_MOTORS, Yspeed);
      //digitalWrite(LMR, LOW); digitalWrite(RMR, LOW);
    }
    //else if (Y < (512 - Ydeadzone)) //Right
    {
     // Yspeed = (512 - Y) / 2;
      //analogWrite(LEFT_MOTORS, Xspeed); analogWrite(RIGHT_MOTORS, Xspeed - Yspeed);
      //digitalWrite(LMR, LOW); digitalWrite(RMR, LOW);
    }
    //else
    {
      //analogWrite(LEFT_MOTORS, Xspeed); analogWrite(RIGHT_MOTORS, Xspeed);
      //digitalWrite(LMR, LOW); digitalWrite(RMR, LOW);
    }
  }

//THIS SECTION WORKS FOR FORWARD CONTROL OF BOTH LEFT AND RIGHT MOROTS, DO NOT TOUCH*************









//THIS SECTION WORKS FOR REVERSE CONTROL OF BOTH LEFT AND RIGHT MOROTS, DO NOT TOUCH*************

  else if (Y <= (512 - Ydeadzone))//Reverse
  { 
    Xspeed = (512 - Y) / 2; // 0 - 255
    if(Y < (512 - Ydeadzone)) //Reverse both Motors
    {
      Yspeed = (Y - 512) / 2;
      //digitalWrite(LEFT_MOTORS, LOW); digitalWrite(RIGHT_MOTORS, LOW);
      analogWrite(LEFT_MOTORS, Yspeed); analogWrite(RIGHT_MOTORS, Yspeed);
    }
    //else if (Y < (512 - Ydeadzone)) //Right
    {
      //Yspeed = (512 - Y) / 2;
      //digitalWrite(LEFT_MOTORS, LOW); digitalWrite(RIGHT_MOTORS, LOW);
      //analogWrite(LMR, Xspeed); analogWrite(RMR, Xspeed - Yspeed);
    }
    //else
    {
      //digitalWrite(LEFT_MOTORS, LOW); digitalWrite(RIGHT_MOTORS, LOW);
      //analogWrite(LMR, Xspeed); analogWrite(RMR, Xspeed);
    }
  }

//THIS SECTION WORKS FOR REVERSE CONTROL OF BOTH LEFT AND RIGHT MOROTS, DO NOT TOUCH*************
//(NEEDS INVERSE POWER APPLIED AS NUMBERS GO FROM LARGE TO SMALL)






  else // X is between 512 +- deadzone
  {
    if(Y > (512 + Ydeadzone)) // zero point turn Left
    {
      digitalWrite(LEFT_MOTORS, LOW); analogWrite(RIGHT_MOTORS, Yspeed);
      //digitalWrite(LMR, LOW); digitalWrite(RMR, LOW);
    }
    else if(Y < (512 - Ydeadzone))// zero point turn Right
    {
      analogWrite(LEFT_MOTORS, Yspeed); digitalWrite(RIGHT_MOTORS, LOW);
      //digitalWrite(LMR, LOW); digitalWrite(RMR, LOW); 
    }
    else
    { // Full stop
      digitalWrite(LEFT_MOTORS,LOW);
      digitalWrite(RIGHT_MOTORS,LOW);
      digitalWrite(LMR,LOW);
      digitalWrite(RMR,LOW);
    }
  }
}

adzza: 2. As far as actually driving the motor into forward and reverse settings, how exactly is this done? Do I need to have a motor controller with 2 PWN pins so I can send one set of signals for forward, and another for reverse? I am just trying to work out how I switch polarity if its hards wired. Hope that makes sense.

The way a motor is controlled is dependent on the motor controller. What are you using to control the motors?

I am currently using this one to test with:

http://www.jaycar.com.au/Kits%2C-Science-%26-Learning/Science-Lab-Equipment/Instruments/Arduino-Compatible-24V-5A-MOS-Driver-Module/p/XC4488

Hi,
Have you got the gnd of the arduino connected to the gnd of the motor controller, it has to be connected so that the motor controller has the same gnd reference to the input signal as the arduino which is supplying it.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks Tom… :slight_smile:

adzza: I am currently using this one to test with:

http://www.jaycar.com.au/Kits%2C-Science-%26-Learning/Science-Lab-Equipment/Instruments/Arduino-Compatible-24V-5A-MOS-Driver-Module/p/XC4488

That can control one motor in one direction.

TomGeorge:
Hi,
Have you got the gnd of the arduino connected to the gnd of the motor controller, it has to be connected so that the motor controller has the same gnd reference to the input signal as the arduino which is supplying it.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks Tom… :slight_smile:

Hi Tom, Is it just the ground that needs to be common?

I’ve made progress, essentially outputting the input from my joystick Y axis to two motor controllers (so both controllers are synchronised yay!). Now I want to use the X axis of the same joystick to drive a servo (So think forward reverse on Y and steering on X) but I am having issues. I have put the servo code in, and basically what happens is when I test it with new code is ONE of the two motor controllers stops behaving like it should, and when I accelerate with Y throttle axis, only at about half way does it suddenly come on. Its very weird…Oh and the servo doesn’t work either (it think its blown however)…I’ve tried outputting to pin 9, and also 13(test led). When connected to the test led I am seeing the light dim when I go left on X Axis, and brighten when I go right, which is great, but with the code in place, as I said that second motor controller misbehaves.

Here is the full code:

// Steering Servo declarations
#include <Servo.h>
Servo Steering_servo;  // create servo object to control a servo

//General Declarations
int deadZone = 5; // joystick deadzone rangec
int maxPWMValue =  255; // maximum PWM value. 255 for built-in PWM

//Joystick Axis Declarations
int Joystick_Y_Axis = A1; // Analog pin for Y_Axis pot sense (Motors)
int Joystick_X_Axis = A0; // Analog pin for Y_Axis pot sense (Steering)

//Joystick Buttons Declarations
int up_button = 2;
int down_button = 4;
int left_button = 5;
int right_button = 3;
int start_button = 6;
int select_button = 7;
int analog_button = 8; //joystick press button

//PWM Output Declarations
int PWMPin1 = 11; // Horiz motor speed control connected to this pin
int PWMPin2 = 10; // Horiz motor speed control connected to this pin


//int Y_Axis_Direction_Pin = 2; // directional control connected here. HIGH = forward, LOW = reverse

//Steering Servo Declarations
int Servo_Value;    // variable to read the value from the analog pin

void setup()
{

  // Steering_servo.attach(13);  // attaches the servo on pin 9 to the servo object

  pinMode(PWMPin1, OUTPUT);
  pinMode(PWMPin2, OUTPUT);
  //pinMode(Y_Axis_Direction_Pin, OUTPUT);
  pinMode(up_button, INPUT);
  pinMode(down_button, INPUT);
  pinMode(left_button, INPUT);
  pinMode(right_button, INPUT);
  pinMode(start_button, INPUT);
  pinMode(select_button, INPUT);
  pinMode(analog_button, INPUT);
  pinMode(Joystick_Y_Axis, INPUT);
  pinMode(Joystick_X_Axis, INPUT);


  // for debugging
  Serial.begin(9600);
  Serial.print("UP = "),Serial.print(digitalRead(up_button)),Serial.print("\t");
  Serial.print("DOWN = "),Serial.print(digitalRead(down_button)),Serial.print("\t");
  Serial.print("LEFT = "),Serial.print(digitalRead(left_button)),Serial.print("\t");
  //Serial.print("RIGHT = "),Serial.print(digitalRead(right_button)),Serial.print("\t");
  //Serial.print("START = "),Serial.print(digitalRead(start_button)),Serial.print("\t");
  //Serial.print("SELECT = "),Serial.print(digitalRead(select_button)),Serial.print("\t");
  Serial.print("ANALOG = "), Serial.print(digitalRead(analog_button)), Serial.print("\t");
  Serial.print("X = "), Serial.print(map(analogRead(Joystick_Y_Axis), 0, 1023, 0, 1023)); Serial.print("\t");
  Serial.print("Y = "), Serial.print(map(analogRead(Joystick_X_Axis), 0, 1023, 0, 1023)); Serial.print("\n");
  delay(100);
}


void loop()
{
  Servo_Value = analogRead(Joystick_X_Axis);              // reads the Servo_Value of the potentiometer on X Axis of Joystick (Servo_Value between 0 and 1023)
  Servo_Value = map(Servo_Value, 0, 1023, 0, 180);     // scale it to use it with the servo (Servo_Value between 0 and 180)
  Steering_servo.write(Servo_Value);                   // sets the servo position according to the scaled Servo_Value
  delay(15);                                           // waits for the servo to get there

  int PWM_Value = 0; // holds the pwm output for 'Y_Axis' motor
  boolean  forwardDir = true; // direction for 'Y_Axis' motor. true = forward, false = reverse

  // read Y_Axis pot value
  int Y_Axis_Pot_Reading = analogRead(Joystick_Y_Axis);

  // map the analog value of 0..1023 to -255..+255
  // If the direction and speed go in the wrong direction compared to joystick movement,
  // just switch the sign of the last two parameters
  int h = map(Y_Axis_Pot_Reading, 0, 1023, -maxPWMValue, maxPWMValue);

  // check if joy in reverse position
  if (h < 0)
  {
    forwardDir = false;
    h *= -1;
  }

  // only set final PWM output if joy is outside of deadzone range
  if (h > deadZone)
    PWM_Value = h;


  // set pwm and direction
  //  digitalWrite(Y_Axis_Direction_Pin, forwardDir);
  analogWrite(PWMPin1, PWM_Value);
  analogWrite(PWMPin2, PWM_Value);


  //debug crap
  Serial.print(PWM_Value );
  Serial.print(", ");
  Serial.println(forwardDir);
  delay(50);

}

This is the line I have disabled that’s causing me some grief…

  // Steering_servo.attach(13);  // attaches the servo on pin 9 to the servo object

This is the line I have disabled that’s causing me some grief…

Would it have anything to do with the fact that the comment and the code don’t agree?

That statement really does not contain any useful content.

Hi,

 // map the analog value of 0..1023 to -255..+255
  // If the direction and speed go in the wrong direction compared to joystick movement,
  // just switch the sign of the last two parameters
  int h = map(Y_Axis_Pot_Reading, 0, 1023, -maxPWMValue, maxPWMValue);

You can only have 0 to 255 PWM values, you cannot go negative. You have to change motor polarity to change direction.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Tom... :)

You can only have 0 to 255 PWM values, you cannot go negative.

But h can go negative. That is dealt with in the code that follows.

Apologies for the delay in getting a diagram together:

https://onedrive.live.com/redir?resid=DB52FFED3913D2A4!26827&authkey=!ABxt2q_nngddwP0&v=3&ithint=photo%2CPNG

NOTE: The motor controllers etc are for display purposes only and do not match my exact controller board. Mine are single motor, single direction controllers.

I have also included link to fritzing schematic:

https://onedrive.live.com/redir?resid=DB52FFED3913D2A4!26829&authkey=!AIOig_am0GwPIUA&ithint=folder%2C

I have noticed some strange activity and I am certain it is because of my lack of programming knowledge. I was again looking at the code last night and working out how to get the push buttons to turn the wheels in a skid motion, which will be used in conjunction with the forward reverse offered by Y axis on the joystick, and the servo steering offered on the X axis. I kinda got it working, but like the servo issue am getting some very strange responses when running the code. Here is what happens:

I enable the pushbutton code I added, and it appears its working when I push the buttons. Yay! BUT…now when I try and use the joystick for forward and reverse, which works fine if I disable the buttons I just added, I get a really weak signal presented to the motors and they don’t even turn! WTF is going on! This is similar to when I enable the servo line that I commented out above. Basically ONE of the two motors works fine, with the other one only coming on at half power up to full power, but it turns on at full speed, not the incremental speed denoted by the position of Y axis that the other motor is running at. Hoping this makes sense… If not I may need to upload a YouTube video of what’s happening.

I know people are probably sick of newbs on here, and I am hoping someone has the patience to work through this with me, but I am thinking its because I haven’t structured the program correctly. I am betting this issue along with the servo issue are related to the same thing, but I have no idea what. Thanks for the input thus far guys. Its frustrating but I am loving learning something new everyday again!

latest code :

#include <Button.h>


// Steering Servo declarations
#include <Servo.h>

Servo Steering_servo;  // create servo object to control a servo

//General Declarations
int deadZone = 5; // joystick deadzone rangec
int maxPWMValue =  255; // maximum PWM value. 255 for built-in PWM

//Joystick Axis Declarations
int Joystick_Y_Axis = A1; // Analog pin for Y_Axis pot sense (Motors)
int Joystick_X_Axis = A0; // Analog pin for Y_Axis pot sense (Steering)

//Joystick Buttons Declarations
int up_button = 2;
int down_button = 4;
//int left_button = 5;
//int right_button = 3;
int start_button = 6;
int select_button = 7;
int analog_button = 8; //joystick press button

//PWM Output Declarations
int PWMPin1 = 11; //  motor speed control connected to this pin
int PWMPin2 = 10; //  motor speed control connected to this pin


//int Y_Axis_Direction_Pin = 2; // directional control connected here. HIGH = forward, LOW = reverse

//Steering Servo Declarations
int Servo_Value;    // variable to read the value from the analog pin

//Skid Steer Declarations
Button left_button(5,PULLUP);
Button right_button(3,PULLUP);
int skid_PWR = 150;
int skid_PWR_0 = 0;

void setup()
{

  


  // Steering_servo.attach(13);  // attaches the servo on pin 9 to the servo object

  pinMode(PWMPin1, OUTPUT);
  pinMode(PWMPin2, OUTPUT);
//pinMode(Y_Axis_Direction_Pin, OUTPUT);
  pinMode(up_button, INPUT);
  pinMode(down_button, INPUT);
//pinMode(left_button, INPUT);
//pinMode(right_button, INPUT);
  pinMode(start_button, INPUT);
  pinMode(select_button, INPUT);
  pinMode(analog_button, INPUT);
  pinMode(Joystick_Y_Axis, INPUT);
  pinMode(Joystick_X_Axis, INPUT);


  // for debugging
  Serial.begin(9600);
  Serial.print("UP = "),Serial.print(digitalRead(up_button)),Serial.print("\t");
  Serial.print("DOWN = "),Serial.print(digitalRead(down_button)),Serial.print("\t");
//Serial.print("LEFT = "),Serial.print(digitalRead(left_button)),Serial.print("\t");
//Serial.print("RIGHT = "),Serial.print(digitalRead(right_button)),Serial.print("\t");
//Serial.print("START = "),Serial.print(digitalRead(start_button)),Serial.print("\t");
//Serial.print("SELECT = "),Serial.print(digitalRead(select_button)),Serial.print("\t");
  Serial.print("ANALOG = "), Serial.print(digitalRead(analog_button)), Serial.print("\t");
  Serial.print("X = "), Serial.print(map(analogRead(Joystick_Y_Axis), 0, 1023, 0, 1023)); Serial.print("\t");
  Serial.print("Y = "), Serial.print(map(analogRead(Joystick_X_Axis), 0, 1023, 0, 1023)); Serial.print("\n");
  delay(100);
}


void loop()
{



if(left_button.isPressed())
 {
   analogWrite(PWMPin1, skid_PWR);
 }   
 
 else analogWrite(PWMPin1, skid_PWR_0);
     
  
if(right_button.isPressed())
 {
   analogWrite(PWMPin2, skid_PWR);
 }   
 
 else analogWrite(PWMPin2, skid_PWR_0);
     


  
  
//  Servo_Value = analogRead(Joystick_X_Axis);           // reads the Servo_Value of the potentiometer on X Axis of Joystick (Servo_Value between 0 and 1023)
//  Servo_Value = map(Servo_Value, 0, 1023, 0, 180);     // scale it to use it with the servo (Servo_Value between 0 and 180)
//  Steering_servo.write(Servo_Value);                   // sets the servo position according to the scaled Servo_Value
//  delay(15);                                           // waits for the servo to get there

  int PWM_Value = 0; // holds the pwm output for 'Y_Axis' motor
  boolean  forwardDir = true; // direction for 'Y_Axis' motor. true = forward, false = reverse

  // read Y_Axis pot value
  int Y_Axis_Pot_Reading = analogRead(Joystick_Y_Axis);

  // map the analog value of 0..1023 to -255..+255
  // If the direction and speed go in the wrong direction compared to joystick movement,
  // just switch the sign of the last two parameters
  int h = map(Y_Axis_Pot_Reading, 0, 1023, -maxPWMValue, maxPWMValue);

  // check if joy in reverse position
  if (h < 0)
  {
    forwardDir = false;
    h *= -1;
  }

  // only set final PWM output if joy is outside of deadzone range
  if (h > deadZone)
    PWM_Value = h;


  // set pwm and direction
  //  digitalWrite(Y_Axis_Direction_Pin, forwardDir);
  analogWrite(PWMPin1, PWM_Value);
  analogWrite(PWMPin2, PWM_Value);


  //debug crap
  Serial.print(PWM_Value );
  Serial.print(", ");
  Serial.println(forwardDir);
  }
  // Steering_servo.attach(13);  // attaches the servo on pin 9 to the servo object

When are you going to delete the useless, inaccurate comment?

PaulS:   // Steering_servo.attach(13);  // attaches the servo on pin 9 to the servo object

When are you going to delete the useless, inaccurate comment?

Sorry how is this constructive? Granted it may be inaccurate due to a typo but either way the function still doesn't work, be it on 9 or 13. That and given the fact that the comments are for my reference I'm not sure how your comment helps???

The function on its own works but as I've stated above not when combined with some of my other code, regardless of what the comments say...

Hi,

Can you please post a copy of your complete circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Your last diags did not include the joysticks, please no fritzy diagrams, if you draw a schematic, use blocks for the arduino and the motor controllers, but make sure you label the wire connections on each of them. Include joysticks and power supplies.

Thanks.. Tom.. :)

I am hoping someone has the patience to work through this with me

Well, I’m losing patience. The comment is wrong. You either need to fix it or get rid of it. Since it says nothing more than what it is perfectly obvious that the code is doing, I say get rid of it.

If I sense that you are going to do as suggested, even on trivial issues like fixing useless comments, then I might reconsider dropping out of this thread.

Paul I'm happy to drop the comment given that I have a lot to learn from basically anyone else on the forum. I'll put up a new schematic shortly, with the "updated" code.

Thanks guys

Ok, so here is my first go at a schematic. Now I think everything is correct except the left and right buttons. Keep in mind the joystick and buttons are on a Joystick game pad shield thing, but I have separated out the joystick and left/right buttons explicitly. If the buttons are not correct, happy to take some advice as always:

I didn’t add the obvious USB connection into the Arduino, and for reference, the motor controllers share the same power, both GND and V+5v as supplied by the Arduino board.

The updated code:

#include <Button.h>


// Steering Servo declarations
#include <Servo.h>

Servo Steering_servo;  // create servo object to control a servo

//General Declarations
int deadZone = 5; // joystick deadzone rangec
int maxPWMValue =  255; // maximum PWM value. 255 for built-in PWM

//Joystick Axis Declarations
int Joystick_Y_Axis = A1; // Analog pin for Y_Axis pot sense (Motors)
int Joystick_X_Axis = A0; // Analog pin for Y_Axis pot sense (Steering)

//Joystick Buttons Declarations
int up_button = 2;
int down_button = 4;
//int left_button = 5;
//int right_button = 3;
int start_button = 6;
int select_button = 7;
int analog_button = 8; //joystick press button

//PWM Output Declarations
int PWMPin1 = 11; //  motor speed control connected to this pin
int PWMPin2 = 10; //  motor speed control connected to this pin


//int Y_Axis_Direction_Pin = 2; // directional control connected here. HIGH = forward, LOW = reverse

//Steering Servo Declarations
int Servo_Value;    // variable to read the value from the analog pin

//Skid Steer Declarations
Button left_button(5,PULLUP);
Button right_button(3,PULLUP);
int skid_PWR = 150;
int skid_PWR_0 = 0;

void setup()
{

//Steering_servo.attach(13);
  pinMode(PWMPin1, OUTPUT);
  pinMode(PWMPin2, OUTPUT);
//pinMode(Y_Axis_Direction_Pin, OUTPUT);
  pinMode(up_button, INPUT);
  pinMode(down_button, INPUT);
//pinMode(left_button, INPUT);
//pinMode(right_button, INPUT);
  pinMode(start_button, INPUT);
  pinMode(select_button, INPUT);
  pinMode(analog_button, INPUT);
  pinMode(Joystick_Y_Axis, INPUT);
  pinMode(Joystick_X_Axis, INPUT);


  // for debugging
  Serial.begin(9600);
//Serial.print("UP = "),Serial.print(digitalRead(up_button)),Serial.print("\t");
//Serial.print("DOWN = "),Serial.print(digitalRead(down_button)),Serial.print("\t");
//Serial.print("LEFT = "),Serial.print(digitalRead(left_button)),Serial.print("\t");
//Serial.print("RIGHT = "),Serial.print(digitalRead(right_button)),Serial.print("\t");
//Serial.print("START = "),Serial.print(digitalRead(start_button)),Serial.print("\t");
//Serial.print("SELECT = "),Serial.print(digitalRead(select_button)),Serial.print("\t");
//Serial.print("ANALOG = "), Serial.print(digitalRead(analog_button)), Serial.print("\t");
  Serial.print("X = "), Serial.print(map(analogRead(Joystick_Y_Axis), 0, 1023, 0, 1023)); Serial.print("\t");
  Serial.print("Y = "), Serial.print(map(analogRead(Joystick_X_Axis), 0, 1023, 0, 1023)); Serial.print("\n");
  delay(100);
}


void loop()
{



if(left_button.isPressed())
 {
   analogWrite(PWMPin1, skid_PWR);
 }
 else analogWrite(PWMPin1, skid_PWR_0);

  
if(right_button.isPressed())
 {
   analogWrite(PWMPin2, skid_PWR);
 }   

 else analogWrite(PWMPin2, skid_PWR_0);
  


  
  
//  Servo_Value = analogRead(Joystick_X_Axis);           // reads the Servo_Value of the potentiometer on X Axis of Joystick (Servo_Value between 0 and 1023)
//  Servo_Value = map(Servo_Value, 0, 1023, 0, 180);     // scale it to use it with the servo (Servo_Value between 0 and 180)
//  Steering_servo.write(Servo_Value);                   // sets the servo position according to the scaled Servo_Value
//  delay(15);                                           // waits for the servo to get there

  int PWM_Value = 0; // holds the pwm output for 'Y_Axis' motor
  boolean  forwardDir = true; // direction for 'Y_Axis' motor. true = forward, false = reverse

  // read Y_Axis pot value
  int Y_Axis_Pot_Reading = analogRead(Joystick_Y_Axis);

  // map the analog value of 0..1023 to -255..+255
  // If the direction and speed go in the wrong direction compared to joystick movement,
  // just switch the sign of the last two parameters
  int h = map(Y_Axis_Pot_Reading, 0, 1023, -maxPWMValue, maxPWMValue);

  // check if joy in reverse position
  if (h < 0)
  {
    forwardDir = false;
    h *= -1;
  }

  // only set final PWM output if joy is outside of deadzone range
  if (h > deadZone)
    PWM_Value = h;


  // set pwm and direction
  //  digitalWrite(Y_Axis_Direction_Pin, forwardDir);
  analogWrite(PWMPin1, PWM_Value);
  analogWrite(PWMPin2, PWM_Value);


  //debug crap
  Serial.print(PWM_Value );
  Serial.print(", ");
  Serial.println(forwardDir);
  }
  Serial.print("X = "), Serial.print(map(analogRead(Joystick_Y_Axis), 0, 1023, 0, 1023)); Serial.print("\t");

Can you explain why you call map() here?

Button left_button(5,PULLUP);
Button right_button(3,PULLUP);

What is this class adding to the party? The state change detection example shows how to determine if a switch has become pressed or become released, it that is important. Otherwise, hiding the call to digitalRead() in a class is pointless.

  // set pwm and direction
  //  digitalWrite(Y_Axis_Direction_Pin, forwardDir);
  analogWrite(PWMPin1, PWM_Value);
  analogWrite(PWMPin2, PWM_Value);

These are the same pins that you PWM'ed just a few lines/nanoseconds back.

An overall description of what the program is supposed to do, with no references to any code would be useful.

A summary of what the current program actually does and how that differs from what you want it to do is also in order.

PaulS:  Serial.print("X = "), Serial.print(map(analogRead(Joystick_Y_Axis), 0, 1023, 0, 1023)); Serial.print("\t");

Can you explain why you call map() here?

Button left_button(5,PULLUP);
Button right_button(3,PULLUP);

What is this class adding to the party? The state change detection example shows how to determine if a switch has become pressed or become released, it that is important. Otherwise, hiding the call to digitalRead() in a class is pointless.

  // set pwm and direction
  //  digitalWrite(Y_Axis_Direction_Pin, forwardDir);
  analogWrite(PWMPin1, PWM_Value);
  analogWrite(PWMPin2, PWM_Value);

These are the same pins that you PWM'ed just a few lines/nanoseconds back.

An overall description of what the program is supposed to do, with no references to any code would be useful.

A summary of what the current program actually does and how that differs from what you want it to do is also in order.

Granted Map is not exactly needed as it going from same to same.

The class for the buttons was introduced to easily enable me to select PULLUP or PULLDOWN modes depending on the function, rather than having to code the response etc.

Here is what I am doing:

I am building a 4x4 remote control lawnmower (based on an older style 4x4 mobility scooter). At this stage on the bench we will omit any RC code as I want to get it working across the wire before I add more complexity.

So we have a 4wd chassis, with 4 motors. The chassis will drive in both skid steer and servo turn modes.

When pressing forward on the joystick, both the left-hand side and right-hand side motors will move forward. When pressing backwards on the joystick, both the left-hand side and right-hand side motors will move reverse.

When pressing left or right on the joystick, this will in turn move the steering servo connected to the front wheels.

When pressing the left button, skid steer will activate and move left-hand side wheels in reverse, and right hand side wheels forward.

When pressing the right button, skid steer will activate and move left-hand side wheels in forward, and right hand side wheels reverse.

I also expect the following safety features added to the program:

PIR sensor; If the PIR detects anything, it stops all wheels, and will also trigger a relay which will disengage the ignition circuit, forcing mower motor to stop also

Temp sensor; If the tempo sensor detects temp range above X, same as above, stops chassis and mower (via relay to ignition)

That's about it in its simplest form to try and explain what I am trying to program here ...

Does any of this make sense?