I am trying to control a DC Motor using a joystick with an Arduino Mega. I have been able to run the DC motor, as well as finding the potentiometer values of the joystick separately. I am using just the y-axis of the joystick in order to have the motor move forward when the joytick is up, then backwards when the joystick is down. The middle of the joystick is ~504.
// motor one
int enA = 3;
int in1 = 22;
int in2 = 24;
// y_joystick
int y_joy = A1;
int joy_value = 0;
void setup() {
// set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode (y_joy, INPUT);
Serial.begin(9600);
}
void loop() {
joy_value = analogRead(y_joy);
if (0 < joy_value < 500) {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(enA, 120);
} else if (511 < joy_value < 1024) {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enA, 120);
} else {
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
analogWrite(enA, 0);
}
Serial.print("y-axis: ");
Serial.println(analogRead(y_joy));
Serial.print("\n");
delay(1000);
}
Now this code prints out the joystick values in the serial monitor, but does not rotate the motor at all. I am positive that the connections are correct because they are the same connections I used before to just run the motor forward and back on its own. I am probably missing something in my code but just can't see it so any feedback is appreciated.