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.

  • what's your question? – jsotola Sep 01 '21 at 00:14
  • 1
    do you honestly believe that the picture shows clearly where each wire is connected? ... please draw a wiring diagram – jsotola Sep 01 '21 at 00:16
  • *"specifically when the y axis"* If this is meant to imply that it behaves differently when wired to the x-axis, that should probably be spelled out. Not being able to actually see the joystick or know its model, the distinction seems otherwise fairly meaningless. – timemage Sep 01 '21 at 00:25
  • 1
    @jsotola Is there a free software I can use to make a schematic for it? – Christian Bautch Sep 01 '21 at 01:17
  • If nothing else, you can hand draw it. It doesn't have to be professional; just readable. You could also retake your picture under good light, e.g. near a window during the day. – timemage Sep 01 '21 at 03:17
  • How stable to you *need* the values to be? You’re dealing with analog-to-digital conversion which will always bring some noise. If you need values that change less abruptly, run the raw *analogRead()* values through a filter (for example a simple moving average filter). – StarCat Sep 01 '21 at 06:05
  • 1
    Personally I've used [Express PCB](https://www.expresspcb.com/) 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. – Nick Gammon Sep 01 '21 at 08:35
  • 1
    Could you try a sketch that does nothing other than `Serial.println(analogRead(A0)); delay(500);`? – Edgar Bonet Sep 01 '21 at 09:04
  • 1
    There is [a video](https://www.youtube.com/watch?v=MlDi0vO9Evg) which appears to be about your joystick. Can you perhaps try what is suggested there and report what happens? – Nick Gammon Sep 01 '21 at 10:22
  • 2
    21/1023 possible readings is only 2%, perhaps that is normal for this joystick. – Nick Gammon Sep 01 '21 at 10:24
  • There is a schematic editor built into this website. Just edit your question and click the schematic button at the top of the editor. – Majenko Sep 01 '21 at 12:30

0 Answers0