Arduino Uno with MShield version 1 controlling 2 dc motors and 1 joystick

Good day,

Been experimenting using Arduino Uno and motor shield to be able to control 2 dc motor for a potential home made robot with tracks.

I did a sketch using 4 push buttons and able to move forward, backward, left, right by pressing the adequate push button but now I wanted to challenge myself and use 1 joystick.

I have a bit of success but maybe I am not taking the right approach as in my code you have to be somewhat precise on where the joystick is located to have the desire results (Backward, Forward, Left, Right).

Below is the code I have been playing around and wanted to see if anyone had advice or better way of attacking the problem of controlling 2 dc motors with one single joystick?

I am under the impression I would require more logic then the below.

Thoughts?
Thank You :slight_smile:

#include <AFMotor.h>

const int potpin1 = A0;
const int potpin2 = A1;

// DC motor on M1
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);

void setup(){

  Serial.begin(9600);

}

void loop(){

  int val1 = analogRead(potpin1);
  int val2 = analogRead(potpin2);
  val1 = map(val1, 0, 1023, 0, 180);
  val2 = map(val2, 0, 1023, 0, 180); 

  Serial.print("Output X: "); //print
  Serial.println(val2); //print
  Serial.print("Output Y: "); //print
Serial.println(val1); //print


if (val2 > 100) {
  motor1.setSpeed(val2);
  motor1.run(FORWARD);
  motor2.setSpeed(val2);
  motor2.run(FORWARD);
}

else if (val2 < 80) {
  motor1.setSpeed(val2);
  motor1.run(BACKWARD);
  motor2.setSpeed(val2);
  motor2.run(BACKWARD);
}


else if (val1 > 100) {
  motor1.setSpeed(val2);
  motor1.run(FORWARD);
  motor2.setSpeed(val1);
  motor2.run(BACKWARD);
}

else if (val1 < 80) {
  motor1.setSpeed(val2);
  motor1.run(BACKWARD);
  motor2.setSpeed(val1);
  motor2.run(FORWARD);
}

else {
motor1.run(RELEASE);
motor2.run(RELEASE);
}
}

What's the range of values you get from full scale deflection on the two axes of the joystick?

Good day,

Thank you for the hint which I believe made me realize what I need to do.

To your question I posted a pic showing values when pointing the joystick to to left corner and value seem to match a picture I found on the internet.

If I understand correctly I would need set some condition for these particular conditions (top-Left, Top-Right, Bottom-Left, Bottom-Right?

It seem re-mapping the joystick is not even needed
// valX = map(valX, 0, 1023, 0, 180);
// valY = map(valY, 0, 1023, 0, 180);

joystick reference: https://lastminuteengineers.com/joystick-interfacing-arduino-processing/

You may still need your mapping. It looks like setSpeed takes up to 255.

Thank You,

I made some modification. The results are good.
Below is a sketch for Pololu Shield and another for he ladyada mshield

#include "DualVNH5019MotorShield.h"

DualVNH5019MotorShield md;

const int potpinY = A0;
const int potpinX = A1;

void setup()
{
Serial.begin (9600); //initialize serial communications
md.init(); //initiates default pololu shield pins
}

void loop()
{
  int valY = analogRead(potpinY);
  int valX = analogRead(potpinX);
  valY = map(valY, 0, 1023, -255, 255);
  valX = map(valX, 0, 1023, -255, 255);


if (valX >= 150)
{
md.setM1Speed(valX);
md.setM2Speed(valX);
}


 else if  (valX <= -150)
  {
    md.setM1Speed(valX);
    md.setM2Speed(valX);
  }

   else if  (valY <= -150)
  {
    md.setM1Speed(valY);
    md.setM2Speed(-valY);
  }


   else if  (valY >= 150)
  {
    md.setM1Speed(-valY);
    md.setM2Speed(valY);
  }
  
else {
md.setM1Speed(0);
md.setM2Speed(0);
}
}
#include <AFMotor.h>

const int potpinY = A0;
const int potpinX = A1;

// DC motor on M1
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);

void setup(){

  Serial.begin(9600);

}

void loop(){

  int valY = analogRead(potpinY);
  int valX = analogRead(potpinX);
  valY = map(valY, 0, 1023, -255, 255);
  valX = map(valX, 0, 1023, -255, 255);

//  Serial.print("Output X: "); //print
//  Serial.println(valX); //print
// Serial.print("Output Y: "); //print
//Serial.println(val1); //print


if (valX >= 20){
  motor1.setSpeed(255);
  motor1.run(FORWARD);
  motor2.setSpeed(255);
  motor2.run(FORWARD);
}

else if  (valX <= -20) {
  motor1.setSpeed(255);
  motor1.run(BACKWARD);
  motor2.setSpeed(255);
  motor2.run(BACKWARD);
}


else if  (valY <= -20) {
  motor1.setSpeed(255);
  motor1.run(FORWARD);
  motor2.setSpeed(255);
  motor2.run(BACKWARD);
}

else if  (valY >= 20) {
  motor1.setSpeed(255);
  motor1.run(BACKWARD);
  motor2.setSpeed(255);
  motor2.run(FORWARD);
}

else {
motor1.run(RELEASE);
motor2.run(RELEASE);
}
}