Motor Control With Joystick

I tried to build a pretty basic script for controlling my DC motor with a joystick. The code works partially, but when the motor is supposed to be at speed 255 when X = 1023, it is going more like speed 75-100. I know it can't be a problem with the motor, because it has no problem getting to top speed when I put in the command flatly. The joystick also works, because I recently used it to control some LED lights. Does anyone know what my problem is?

Here is my code:

int xPin = A1;
int yPin = A0;
int buttonPin = 2;


int xPosition = 0;
int yPosition = 0;
int buttonState = 0;

const int motorPin = 9;

void setup() {
  pinMode(xPin, INPUT);
  pinMode(yPin, INPUT);
  pinMode(buttonPin, INPUT_PULLUP); 
  pinMode(motorPin, OUTPUT);
  
}

void loop() {
  xPosition = analogRead(xPin);
  yPosition = analogRead(yPin);
  buttonState = digitalRead(buttonPin);
  checkX();
  switch (xPosition) {
    case 1023:
    analogWrite(motorPin, 200);
    break;
    case 0:
    analogWrite(motorPin, 0);
    break;
    default:
    analogWrite(motorPin, 0);
    break;
  }
}

void checkX(){
  analogRead(xPin);

You need to insert some Serial.print() statements so you can see what values the joystick is actually producing.

And ADC values are never so precise that you can use something like case 1023:. You need code like

if (xPosition > 1020) {

...R

Hi,
Shouldn't this;

 analogWrite(motorPin, 200);

be

 analogWrite(motorPin, 255);

What does this do?

void checkX(){
  analogRead(xPin);

You are already reading the joystick in the loop()?
And the function is missing a }

Tom.... :slight_smile:

I took into account your advise, and my code was not what is was originally as I spent some time looking for functions and code that might work. Now the motor goes faster with the serial commands I added, but still not full speed.

int xPin = A1;
int yPin = A0;
int buttonPin = 2;


int xPosition = 0;
int yPosition = 0;
int buttonState = 0;

const int motorPin = 9;

void setup() {
  pinMode(xPin, INPUT);
  pinMode(yPin, INPUT);
  pinMode(buttonPin, INPUT_PULLUP); 
  pinMode(motorPin, OUTPUT);
  
}

void loop() {
  xPosition = analogRead(xPin);
  yPosition = analogRead(yPin);
  buttonState = digitalRead(buttonPin);

  Serial.print("X: ");
  Serial.print(xPosition);
  Serial.print(" | Y: ");
  Serial.print(yPosition);
  Serial.print(" | Button: ");
  Serial.println(buttonState);

  if (xPosition >1020) {
    analogWrite(motorPin, 255);
  }
  else{
    analogWrite(motorPin, 0);
  }
}

What happens if you fix the value of xPosition as in this version

void loop() {
  xPosition = analogRead(xPin);
  yPosition = analogRead(yPin);
  buttonState = digitalRead(buttonPin);
  
  xPosition = 1023; // temporaril fix the value regardless of the joystick position

  Serial.print("X: ");
  Serial.print(xPosition);
  Serial.print(" | Y: ");
  Serial.print(yPosition);
  Serial.print(" | Button: ");
  Serial.println(buttonState);

  if (xPosition >1020) {
    analogWrite(motorPin, 255);
  }
  else{
    analogWrite(motorPin, 0);
  }
}

...R

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Showing how you are powering the motor.

What joystick are you using?

Thanks.. Tom.. :slight_smile:

Again thank your for all the help. When I set xPosition to be 1023, the motor is up to full power and works fine. Also, I believe this is the diagram you are looking for. It is a 5 pin joystick I got from an Elegoo kit.

IMG_2277.jpg

Hi,
Sorry no image.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
This is becoming essential to make sure your input is configured right.

Do you have each of the joystick pots wired as potential dividers.

Thanks.. Tom.. :slight_smile:

AyJay13:
Again thank your for all the help. When I set xPosition to be 1023, the motor is up to full power and works fine.

That suggests that you are not, in fact, consistently getting 1023 from the joystick.

Have you tried reducing the value 1020 in this line

if (xPosition >1020) {

...R

Hi,
Have you got your joysticks connected like this?

Tom.. :slight_smile:

I just reduced the value to 1000 in the line

if (x > 1020){

and that worked perfectly. The motor is working fine now. Also, sorry for the image issue, here it is again.

Image was too large, here is the .zip

untitled folder (1).zip (70.7 KB)

AyJay13:
Image was too large, here is the .zip

Then make it smaller. 640x480 is usually perfectly adequate.

...R

Here it is.

Hi,
Ops pic.
IMG_2277.jpg
Tom.. :slight_smile:

Hi,
I think this is your joystick?


If it is, I have a couple as well and they do not go all the way to 0 or 1023 with their output voltage.
Tom... :slight_smile:

Hi,
Load this code and open the MONITOR in the TOOL menu.
Set its speed for 9600.
Then move your joystick and see what its limits are;

int xPin = A1;
int yPin = A0;
int buttonPin = 2;


int xPosition = 0;
int yPosition = 0;
int buttonState = 0;

const int motorPin = 9;

void setup() {
  Serial.begin(9600);
  pinMode(xPin, INPUT);
  pinMode(yPin, INPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(motorPin, OUTPUT);
  Serial.println("JOYSTICK OUTPUT TEST MONITOR");
  Serial.println("============================");
  delay(500);
}
void loop() {
  xPosition = analogRead(xPin);
  yPosition = analogRead(yPin);
  Serial.print("X Axis position = ");
  Serial.print(xPosition);
  Serial.print("   Y Axis position = ");
  Serial.println(yPosition);
  delay (1000);
}

Tom... :slight_smile: