1

I'm trying to make my B103348 joystick control a DC motor but I'm getting very unstable values from it specifically when the y axis is at 0. This is what my Serial monitor shows...

0, 21, 21, 21, 0, 22, 23, 21, 21...

This is the wiring...

enter image description here

And here's the code...

int y_pin = A0;
int y_val;

int in_1 = 3;
int in_2 = 4;
int clock_wise_speed;
int counter_clock_wise_speed;

int en = 2;

int dt = 500;

void setup() {
pinMode(y_pin, INPUT);

pinMode(en, OUTPUT);
pinMode(in_1, OUTPUT);
pinMode(in_2, OUTPUT);

Serial.begin(2000000);
}

void loop() {
  //Clock wise = in_1 LOW and in_2 HIGH.
  //Counter clock wise = in_1 HIGH and in_2 LOW.
  y_val = analogRead(y_pin);
  clock_wise_speed = .21994 * y_val;
  counter_clock_wise_speed = 90;

  if(y_val >= 506){
    analogWrite(en, clock_wise_speed);
    digitalWrite(in_1, LOW);
    digitalWrite(in_2, HIGH);
  }

  if(y_val >= 501 and y_val <= 505){
    digitalWrite(en, LOW);
  }

  if(y_val <= 500){
    analogWrite(en, 225);
    digitalWrite(in_1, HIGH);
    digitalWrite(in_2, LOW);
  }
  

Serial.println(y_val);
delay(dt);
}

Thanks in advance.

11
  • 1
    do you honestly believe that the picture shows clearly where each wire is connected? ... please draw a wiring diagram Commented Sep 1, 2021 at 0:16
  • 1
    @jsotola Is there a free software I can use to make a schematic for it? Commented Sep 1, 2021 at 1:17
  • 1
    Personally I've used Express PCB for free schematics. However you could just sketch out on a bit of paper how you wired up the joystick. I would test without what looks like a motor in your photo - that might be influencing things. Commented Sep 1, 2021 at 8:35
  • 1
    Could you try a sketch that does nothing other than Serial.println(analogRead(A0)); delay(500);? Commented Sep 1, 2021 at 9:04
  • 2
    21/1023 possible readings is only 2%, perhaps that is normal for this joystick. Commented Sep 1, 2021 at 10:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.