I found a sketch online that supposedly allows me to control a motor with a joystick. but the motor moves by itself even though I'm not pushing on the joystick. can anyone please tell me a better way to do this ot whats wrong with the sketch.
//motor
int in1 = 5;
int in2 = 6;
int joyVer = A0;
int MotorSpeed1 = 0;
int joyposVer = 512;
void setup(){
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
}
void loop(){
joyposVer = analogRead(joyVer);
if (joyposVer < 460)
{
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
joyposVer = joyposVer - 460;
joyposVer = joyposVer * -1;
MotorSpeed1 = map(joyposVer, 0, 460, 0, 255);
}
else if (joyposVer > 564)
{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
MotorSpeed1 = map(joyposVer, 564, 1023, 0, 255);
}
else
{
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
MotorSpeed1 = 0;
}
if (MotorSpeed1 < 100)MotorSpeed1=0;
}