Go Down

Topic: Motor control with joystck problem (Read 119 times) previous topic - next topic


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.


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);
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    MotorSpeed1 = 0;

  if (MotorSpeed1 < 100)MotorSpeed1=0;



May 04, 2018, 09:39 am Last Edit: May 04, 2018, 09:41 am by slipstick
Where online? What motor? What motor driver?

You keep calculating MotorSpeed1 but it's never used for anything. So either you haven't copied the sketch correctly or it wasn't a good one to copy. Certainly not if you think it's going to control the speed of the motor.

Also it may be worth putting some Serial.prints in to see what values you are actually getting from your joystick.



May 06, 2018, 03:23 pm Last Edit: May 06, 2018, 03:24 pm by TomGeorge
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What does happen when you move the joystick?

Thanks.. Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

Go Up