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.

0 Answers0