Motor 1 works perfectly in both directions. Motor 2 works both ways but one direction is weaker than the other, and it sometimes doesn't turn the other way at all. For some reason I've had a lot of inconsistent behaviour with the motors, including the speed being affected by my hand being on the table and other things. I know all my hardware works (tested each component), but I used the same code for both motors. If anyone could point any errors out in the code I could really use a second pair of eyes. Thanks if you take some time to help.
Also if someone can tell me why my project is affected by its surroundings like the desk or touching multiple parts with my hands I would greatly appreciate it. (My desk has 2 monitors and other peripherals on it)
Code:
//CONSTANTS
int const motor1pin1 = 2;
int const motor1pin2 = 3;
int const motor2pin1 = 4;
int const motor2pin2 = 5;
int const buttonPin = 6;
int const joyXPin = 1;
int const joyYPin = 2;
//VARIABLES
int buttonState = 0;
int motorDir1 = 0;
int motorDir2 = 0;
void setup() {
// PINMODES + SERIAL
pinMode(motor1pin1, OUTPUT);
pinMode(motor1pin2, OUTPUT);
pinMode(motor2pin1, OUTPUT);
pinMode(motor2pin2, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(joyXPin, INPUT);
pinMode(joyYPin, INPUT);
Serial.begin(9600);
}
void loop() {
//X AXIS
if(analogRead(joyXPin) <= 475){ //LOWER THAN 475
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
}
if(analogRead(joyXPin) >= 525){ //HIGHER THAN 525
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, HIGH);
}
if(analogRead(joyXPin) >= 475 && analogRead(joyXPin) <= 525){ //BETWEEN
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, LOW);
}
//Y AXIS
if(analogRead(joyYPin) <= 475){ //LOWER THAN 475
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
}
if(analogRead(joyYPin) >= 525){ //HIGH THAN 525
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, HIGH);
}
if(analogRead(joyYPin) >= 475 && analogRead(joyYPin) <= 525){ //BETWEEN
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, LOW);
}
}