Joystick and motors aren't quite working

Motor 1 works perfectly in both directions. Motor 2 works both ways but one direction is weaker than the other, and it sometimes doesn't turn the other way at all. For some reason I've had a lot of inconsistent behaviour with the motors, including the speed being affected by my hand being on the table and other things. I know all my hardware works (tested each component), but I used the same code for both motors. If anyone could point any errors out in the code I could really use a second pair of eyes. Thanks if you take some time to help.
Also if someone can tell me why my project is affected by its surroundings like the desk or touching multiple parts with my hands I would greatly appreciate it. (My desk has 2 monitors and other peripherals on it)

Code:

//CONSTANTS
int const motor1pin1 = 2;
int const motor1pin2 = 3;
int const motor2pin1 = 4;
int const motor2pin2 = 5;
int const buttonPin = 6;
int const joyXPin = 1;
int const joyYPin = 2;

//VARIABLES
int buttonState = 0;
int motorDir1 = 0;
int motorDir2 = 0;

void setup() {
  // PINMODES + SERIAL
  pinMode(motor1pin1, OUTPUT);
  pinMode(motor1pin2, OUTPUT);
  pinMode(motor2pin1, OUTPUT);
  pinMode(motor2pin2, OUTPUT);
  pinMode(buttonPin, INPUT);
  pinMode(joyXPin, INPUT);
  pinMode(joyYPin, INPUT);
  Serial.begin(9600);
}

void loop() {

  //X AXIS
  if(analogRead(joyXPin) <= 475){                                      //LOWER THAN 475
    digitalWrite(motor1pin1, HIGH);
    digitalWrite(motor1pin2, LOW);
  }
  if(analogRead(joyXPin) >= 525){                                      //HIGHER THAN 525
    digitalWrite(motor1pin1, LOW);
    digitalWrite(motor1pin2, HIGH);
  }
  if(analogRead(joyXPin) >= 475 && analogRead(joyXPin) <= 525){        //BETWEEN
    digitalWrite(motor1pin1, LOW);
    digitalWrite(motor1pin2, LOW);
  }
  
  //Y AXIS
  if(analogRead(joyYPin) <= 475){                                      //LOWER THAN 475
    digitalWrite(motor2pin1, HIGH);
    digitalWrite(motor2pin2, LOW);
  }
  if(analogRead(joyYPin) >= 525){                                      //HIGH THAN 525
    digitalWrite(motor2pin1, LOW);
    digitalWrite(motor2pin2, HIGH);
  }
  if(analogRead(joyYPin) >= 475 && analogRead(joyYPin) <= 525){        //BETWEEN
    digitalWrite(motor2pin1, LOW);
    digitalWrite(motor2pin2, LOW);
  }
}

Welcome to the forum

How are the motors powered ?

A bit short on information. What motors? What motor driver? How powered?

But the first thing I'd do is put some serial prints in to see exactly what values you're getting from the joystick reads, particularly joyYPin which controls motor2.

Steve

Rather than reading the analog input four times, read each pin once and save the value in a variable.

Rather than three separate 'if' statements with four comparisons, use "if, else if, else" to categorize with only two comparisons.

//CONSTANTS
int const motor1pin1 = 2;
int const motor1pin2 = 3;
int const motor2pin1 = 4;
int const motor2pin2 = 5;
int const buttonPin = 6;
int const joyXPin = A1;  // Better to use names for analog input pins
int const joyYPin = A2;  // Better to use names for analog input pins

//VARIABLES
int buttonState = 0;
int motorDir1 = 0;
int motorDir2 = 0;

void setup()
{
  // PINMODES + SERIAL
  pinMode(motor1pin1, OUTPUT);
  pinMode(motor1pin2, OUTPUT);
  pinMode(motor2pin1, OUTPUT);
  pinMode(motor2pin2, OUTPUT);
  pinMode(buttonPin, INPUT);
  // pinMode(joyXPin, INPUT);  // Not needed for analogRead()
  // pinMode(joyYPin, INPUT);  // Not needed for analogRead()
  Serial.begin(9600);
}

void loop()
{

  //X AXIS
  int joyX = analogRead(joyXPin);
  if (joyX <= 475)                                      //LOWER THAN 475
  {
    digitalWrite(motor1pin1, HIGH);
    digitalWrite(motor1pin2, LOW);
  }
  else if (joyX >= 525)                                  //HIGHER THAN 525
  {
    digitalWrite(motor1pin1, LOW);
    digitalWrite(motor1pin2, HIGH);
  }
  else      //BETWEEN
  {
    digitalWrite(motor1pin1, LOW);
    digitalWrite(motor1pin2, LOW);
  }

  //Y AXIS
  int joyY = analogRead(joyYPin);
  if (joyY <= 475)                                      //LOWER THAN 475
  {
    digitalWrite(motor2pin1, HIGH);
    digitalWrite(motor2pin2, LOW);
  }
  else if (joyY >= 525)                                 //HIGH THAN 525
  {
    digitalWrite(motor2pin1, LOW);
    digitalWrite(motor2pin2, HIGH);
  }
  else        //BETWEEN
  {
    digitalWrite(motor2pin1, LOW);
    digitalWrite(motor2pin2, LOW);
  }
}

I figured it out
I was missing the wire to share ground between the L298N driver and the breadboard.
I had ground to the external power supply for the motors, but I didn't have ground to the arduino nano. Thanks for your advice

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.