My joystick will not move my car

For the past few days i have been trying to make small car that can be controlled with the joystick. but for some reason, the motors will not respond to the joystick whatsoever. I am extremely new to arduino programming and building and have no idea where to even begin trying to fix this. Please help if possible!

My code:

const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output
#define enA 9
#define in1 4
#define in2 5
#define enB 10
#define in3 6
#define in4 7
int motorSpeedA = 0;
int motorSpeedB = 0;
void setup() {
 pinMode(enA, OUTPUT);
 pinMode(enB, OUTPUT);
 pinMode(in1, OUTPUT);
 pinMode(in2, OUTPUT);
 pinMode(in3, OUTPUT);
 pinMode(in4, OUTPUT);
 pinMode(SW_pin, INPUT);
 digitalWrite(SW_pin, HIGH);
 Serial.begin(115200);
}
void loop() {
 // Y-axis used for forward and backward control
 if (Y_pin < 470) {
   // Set Motor A backward
   digitalWrite(in1, HIGH);
   digitalWrite(in2, LOW);
   // Set Motor B backward
   digitalWrite(in3, HIGH);
   digitalWrite(in4, LOW);
   // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
   motorSpeedA = map(Y_pin, 470, 0, 0, 255);
   motorSpeedB = map(Y_pin, 470, 0, 0, 255);
 }
 else if (Y_pin > 550) {
   // Set Motor A forward
   digitalWrite(in1, LOW);
   digitalWrite(in2, HIGH);
   // Set Motor B forward
   digitalWrite(in3, LOW);
   digitalWrite(in4, HIGH);
   // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
   motorSpeedA = map(Y_pin, 550, 1023, 0, 255);
   motorSpeedB = map(Y_pin, 550, 1023, 0, 255);
 }
 // If joystick stays in middle the motors are not moving
 else {
   digitalWrite(in1, HIGH);
   digitalWrite(in2, HIGH);
   /* set speed to 200 out of possible range 0~255*/
   analogWrite(enA, 255);
   /*turn on motor B*/
   digitalWrite(in3, HIGH);
   digitalWrite(in4, HIGH);
   /*set speed to 200 out of possible range 0~255*/
   analogWrite(enB, 255);
 }
 // X-axis used for left and right control
 if (X_pin < 470) {
   // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
   int xMapped = map(X_pin, 470, 0, 0, 255);
   // Move to left - decrease left motor speed, increase right motor speed
   motorSpeedA = motorSpeedA - xMapped;
   motorSpeedB = motorSpeedB + xMapped;
   // Confine the range from 0 to 255
   if (motorSpeedA < 0) {
     motorSpeedA = 0;
   }
   if (motorSpeedB > 255) {
     motorSpeedB = 255;
   }
 }
 if (X_pin > 550) {
   // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
   int xMapped = map(X_pin, 550, 1023, 0, 255);
   // Move right - decrease right motor speed, increase left motor speed
   motorSpeedA = motorSpeedA + xMapped;
   motorSpeedB = motorSpeedB - xMapped;
   // Confine the range from 0 to 255
   if (motorSpeedA > 255) {
     motorSpeedA = 255;
   }
   if (motorSpeedB < 0) {
     motorSpeedB = 0;
   }
 }
 // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)
 if (motorSpeedA < 70) {
   motorSpeedA = 0;
 }
 if (motorSpeedB < 70) {
   motorSpeedB = 0;
 }
 analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
 analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
Serial.print("Switch:  ");
 Serial.print(digitalRead(SW_pin));
 Serial.print("\n");
 Serial.print("X-axis: ");
 Serial.print(analogRead(X_pin));
 Serial.print("\n");
 Serial.print("Y-axis: ");
 Serial.print(analogRead(Y_pin));
 Serial.print("\n\n");
 delay(500);
}
 if (Y_pin < 470) {

Y_pin is the NUMBER of the pin. It will always be < 470. You probably wanted to READ the pin!

You’ve done the same thing in several other places though strangely you’ve done it right to print the joystick values.

Steve

To get the joystick to move the car, you will want a long lever! Or a long string if it is a model

Now start by going and reading the forum instructions so that you can go back and modify your original post - the “More → Modify” option below the right hand corner of your post - to mark up your code as such using the “</>” icon in the posting window. Just highlight each section of code (or output) from the IDE and click the icon. In fact, the IDE has a “copy for forum” link to put these markings on for you so you then just paste it here in a posting window.

Do not attach it as a “.ino” file. People can usually see the mistakes directly and do not want to have to actually load it in their own IDE. And that would also assume they are using a PC and have the IDE running on that PC.

But don’t forget to use the “Auto-Format” (Ctrl-T) option first to make it easy to read. If you do not post it as “code”, it can be quite garbled and is always more difficult to read.

This is my updated code, would this work? Again apologies for any mistakes i have made.

const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output
#define enA 9
#define in1 4
#define in2 5
#define enB 10
#define in3 6
#define in4 7
int motorSpeedA = 0;
int motorSpeedB = 0;
void setup() {
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
  Serial.begin(115200);
}
void loop() {
  // Y-axis used for forward and backward control
  if (analogRead(Y_pin) < 470) {
    // Set Motor A backward
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    // Set Motor B backward
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
    motorSpeedA = map(Y_pin, 470, 0, 0, 255);
    motorSpeedB = map(Y_pin, 470, 0, 0, 255);
  }
  else if (analogRead(Y_pin) > 550) {
    // Set Motor A forward
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    // Set Motor B forward
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
    motorSpeedA = map(Y_pin, 550, 1023, 0, 255);
    motorSpeedB = map(Y_pin, 550, 1023, 0, 255);
  }
  // If joystick stays in middle the motors are not moving
  else {
    digitalWrite(in1, HIGH);
    digitalWrite(in2, HIGH);
    /* set speed to 200 out of possible range 0~255*/
    analogWrite(enA, 255);
    /*turn on motor B*/
    digitalWrite(in3, HIGH);
    digitalWrite(in4, HIGH);
    /*set speed to 200 out of possible range 0~255*/
    analogWrite(enB, 255);
  }
  // X-axis used for left and right control
  if (analogRead(X_pin) < 470) {
    // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
    int xMapped = map(X_pin, 470, 0, 0, 255);
    // Move to left - decrease left motor speed, increase right motor speed
    motorSpeedA = motorSpeedA - xMapped;
    motorSpeedB = motorSpeedB + xMapped;
    // Confine the range from 0 to 255
    if (motorSpeedA < 0) {
      motorSpeedA = 0;
    }
    if (motorSpeedB > 255) {
      motorSpeedB = 255;
    }
  }
  if (analogRead(X_pin) > 550) {
    // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
    int xMapped = map(X_pin, 550, 1023, 0, 255);
    // Move right - decrease right motor speed, increase left motor speed
    motorSpeedA = motorSpeedA + xMapped;
    motorSpeedB = motorSpeedB - xMapped;
    // Confine the range from 0 to 255
    if (motorSpeedA > 255) {
      motorSpeedA = 255;
    }
    if (motorSpeedB < 0) {
      motorSpeedB = 0;
    }
  }
  // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)
 // if (motorSpeedA < 70) {
 //   motorSpeedA = 0;
 // }
 // if (motorSpeedB < 70) {
  //  motorSpeedB = 0;
  //}
  analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
Serial.print("Switch:  ");
  Serial.print(digitalRead(SW_pin));
  Serial.print("\n");
  Serial.print("X-axis: ");
  Serial.print(analogRead(X_pin));
  Serial.print("\n");
  Serial.print("Y-axis: ");
  Serial.print(analogRead(Y_pin));
  Serial.print("\n\n");
  delay(500);
}

No. Now you're using the pin number in your map() statements. I suggest doing the analogRead()s once into variables at the top of loop() and then using those variables everywhere else. That's the way just about every example program ever written does it.

Steve

just to state the obvious;

X_pin is the number of the pin on the Arduino being used.

analogRead(X_pin) is the VALUE of the reading on the pin on the Arduino being used.

The first step is to write a very simple program to tests whether the motor/motor driver/battery wiring actually works.

Have you done that step?

To jremington - I have and they work just fine.

To Babygeezer and Slipstick: how would i go about doing what you are saying, again i apologize since i have 0 experience before this.

since i have 0 experience before this.

You can gain that experience by working through the simple examples that come with Arduino.

Start with File>Examples>03.Analog>AnalogInOutSerial

the hint is here;

    // Convert the declining [color=blue]X-axis readings[/color] from 470 to 0 into increasing 0 to 255 value
    int xMapped = map([color=blue]X_pin[/color], 470, 0, 0, 255);

if you still don't see it (after my earlier reply) have a look again at the map() function on the Reference page (from the Arduino Help menu).

It looks like you haven't even bothered looking at all the useful examples that come with the IDE.

Sorry but if you just want someone to write your program for you without you making any attempt then you're in the wrong place. If you don't understand what we are telling you either just try something that you think might be it and post it here for us to check/correct or ask some more detailed questions.

We don't need you to apologise, we need you to make some attempt for yourself.

Steve