Programming your joystick with a DC motor

I want to program the Arduino to control the rotation of the DC motor. I have tried a lot, but a problem occurs. When I push the joystick forward, it starts rotating, and when I move it back, the rotation reverses, but if I push it all the way back, the motor turns off. What is the solution?

You, yourself, can find the problem by using serial.Print() to show the values being returned by the code reading the joystick values. When you know the values causing the problem, fix the problem.

Do you have a sketch you can show us and maybe a schimatic?

Are you using the Servo library etc?

Br Max

1 Like

You have a sketch. Show your sketch so it may be fixed.

WOKWI files

sketch.ino

#define VERT_PIN A0
#define HORZ_PIN A1
#define SEL_PIN  2

#define enaLeft 9
#define fwdLeft 8
#define revLeft 12

#define enaRight 5
#define fwdRight 4
#define revRight 7

int vert, oldVert = 512, horz, oldHorz = 512, drive;
bool sel, oldSel = 1;

void setup() {
  Serial.begin(115200);
  welcome();
  
  pinMode(enaLeft, OUTPUT);
  pinMode(fwdLeft, OUTPUT);
  pinMode(revLeft, OUTPUT);
  pinMode(enaRight, OUTPUT);
  pinMode(fwdRight, OUTPUT);
  pinMode(revRight, OUTPUT);
  pinMode(VERT_PIN, INPUT);
  pinMode(HORZ_PIN, INPUT);
  pinMode(SEL_PIN, INPUT_PULLUP);
  delay(1000);
}

void loop() {
  readJoystick();
  drive = map(vert, 0, 1023, -255, 255);

  if (drive > 20) // stick up
    forward();

  if (drive < -20) // stick down
    reverse();

  if (drive <= 20 || drive >= -20) // deadband
    stop();
}

void forward() {
    digitalWrite(fwdLeft, HIGH); // forward polarity
    digitalWrite(revLeft, LOW);
    digitalWrite(fwdRight, LOW); // right motor mounted backwards
    digitalWrite(revRight, HIGH);
    analogWrite(enaLeft, drive); // PWM speed
    analogWrite(enaRight, drive);
}

void reverse() {
    digitalWrite(fwdLeft, LOW); // reverse polarity
    digitalWrite(revLeft, HIGH);
    digitalWrite(fwdRight, HIGH);
    digitalWrite(revRight, LOW);
    analogWrite(enaLeft, -drive); // negate the PWM value
    analogWrite(enaRight, -drive);
}

void stop() { // stop the motors with stick in deadband
    digitalWrite(fwdLeft, LOW);
    digitalWrite(revLeft, LOW);
    digitalWrite(fwdRight, LOW);
    digitalWrite(revRight, LOW);
    analogWrite(enaLeft, LOW);
    analogWrite(enaRight, LOW);
}

void readJoystick() {
  vert = analogRead(VERT_PIN);

  // debug printing

  // horz = analogRead(HORZ_PIN);
  // sel = digitalRead(SEL_PIN);
  // delay(100);

  // if (oldVert != vert) {
  //   Serial.println(vert);
  //   oldVert = vert;
  // }

  // if (oldHorz != horz) {
  //   Serial.println(horz);
  //   oldHorz = horz;
  // }

  // if (oldSel != sel) {
  //   Serial.println(sel);
  //   oldSel = sel;
  // }
}

void welcome() {
  Serial.println("DC motor simulation with joystick control for forward and reverse.");
}

diagram.json
{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": -14.4, "left": -0.5, "attrs": {} },
    {
      "type": "wokwi-analog-joystick",
      "id": "joystick1",
      "top": -154.2,
      "left": 159,
      "attrs": {}
    },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": -135.6,
      "left": 112.2,
      "rotate": 90,
      "attrs": { "color": "green" }
    },
    {
      "type": "wokwi-led",
      "id": "led2",
      "top": -78,
      "left": 112.2,
      "rotate": 90,
      "attrs": { "color": "green" }
    },
    {
      "type": "wokwi-led",
      "id": "led3",
      "top": -106.8,
      "left": 112.2,
      "rotate": 90,
      "attrs": { "color": "white" }
    },
    {
      "type": "wokwi-led",
      "id": "led4",
      "top": -135.6,
      "left": -27.4,
      "rotate": 270,
      "attrs": { "color": "red", "flip": "1" }
    },
    {
      "type": "wokwi-led",
      "id": "led5",
      "top": -106.8,
      "left": -27.4,
      "rotate": 270,
      "attrs": { "color": "white", "flip": "1" }
    },
    {
      "type": "wokwi-led",
      "id": "led6",
      "top": -78,
      "left": -27.4,
      "rotate": 270,
      "attrs": { "color": "red", "flip": "1" }
    }
  ],
  "connections": [
    [ "joystick1:VCC", "nano:5V", "red", [ "v96", "h-67.2" ] ],
    [ "joystick1:GND", "nano:GND.1", "black", [ "v105.6", "h-86.4" ] ],
    [ "nano:A0", "joystick1:VERT", "green", [ "v28.8", "h153.6" ] ],
    [ "nano:A1", "joystick1:HORZ", "green", [ "v38.4", "h153.6" ] ],
    [ "joystick1:SEL", "nano:2", "green", [ "v19.2", "h-96" ] ],
    [ "nano:4", "led2:A", "green", [ "v0" ] ],
    [ "nano:7", "led1:A", "green", [ "v0" ] ],
    [ "nano:5", "led3:A", "green", [ "v0" ] ],
    [ "nano:8", "led4:A", "green", [ "v0" ] ],
    [ "nano:12", "led6:A", "green", [ "v0" ] ],
    [ "nano:9", "led5:A", "green", [ "v0" ] ],
    [ "nano:GND.2", "led2:C", "black", [ "v-19.2", "h-48", "v-28.8" ] ],
    [ "nano:GND.2", "led6:C", "black", [ "v-19.2", "h-96", "v-28.8" ] ],
    [ "nano:GND.2", "led3:C", "black", [ "v-19.2", "h-48", "v-57.6" ] ],
    [ "nano:GND.2", "led1:C", "black", [ "v-19.2", "h-48", "v-86.4" ] ],
    [ "nano:GND.2", "led5:C", "black", [ "v-19.2", "h-96", "v-57.6" ] ],
    [ "nano:GND.2", "led4:C", "black", [ "v-19.2", "h-96", "v-86.4" ] ]
  ],
  "dependencies": {}
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.