Page 1 of 1

MDD10aA with digital joystick

PostPosted: Sat Sep 09, 2017 9:21 pm
by mscholl
I am new to the forum. I am trying to drive 2 motors with a digital joystick, and use a separate PWM to control the speed. I bought the MDD10A, but this hooks up differently than the L298N motor driver that I mocked up first. (I need the MDD10A to handle the amps needed).
Below is the code I wrote for the L298N, which works fine. How do I adjust the code to work for the MDD10A, as I only have DIR1 and DIR2, instead of EN1, IN1, IN2, IN3, IN4, En2?
Also, is there a way to power the arduino thru the motor driver (I don't see a 5v connection)?

CODE: SELECT_ALL_CODE
int sw1=10;
int sw2=11;
int sw3=12;

int sw4=6;
int state1;
int state2;
int state3;
int state4;

//MOTOR A
int enA = 9;
int in1 = 8;
int in2 = 7;

// Motor B
int enB = 3;
int in3 = 5;
int in4 = 4;

void setup() {
  pinMode(sw1,INPUT_PULLUP);
  pinMode(sw2,INPUT_PULLUP);
  pinMode(sw3,INPUT_PULLUP);
  pinMode(sw4,INPUT_PULLUP);

  Serial.begin(9600);

  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
}

void loop() {
  state1 = digitalRead(sw1);
  state2 = digitalRead(sw2);
  state3 = digitalRead(sw3);
  state4 = digitalRead(sw4);

  if (state1 == 0) { //Make a Right turn
    analogWrite(enB, 50);
    analogWrite(enA, 0);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
  }
  else if (state2 == 0) { //Forward Movement
    analogWrite(enA, 250);
    analogWrite(enB, 250);
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
  }
  else if (state3 == 0) { //Take a left turn
    analogWrite(enA, 50);
    analogWrite(enB,0);
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW);
  }
  else if (state4 == 0) { //Backward Movement
    analogWrite(enA, 50);
    analogWrite(enB, 50);
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
  }
  else {
    analogWrite(enA, 0);
    analogWrite(enB, 0);
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW);
  }

  Serial.print("1-");
  Serial.print(state1);
  Serial.print(" 2-");
  Serial.print(state2);
  Serial.print(" 3-");
  Serial.print(state3);
  Serial.print(" 4-");
  Serial.println(state4);
  delay(250);
}

Re: MDD10aA with digital joystick

PostPosted: Mon Sep 11, 2017 6:03 pm
by Idris
Hi mscholl,

mscholl WROTE:I am new to the forum. I am trying to drive 2 motors with a digital joystick, and use a separate PWM to control the speed. I bought the MDD10A, but this hooks up differently than the L298N motor driver that I mocked up first. (I need the MDD10A to handle the amps needed).
Below is the code I wrote for the L298N, which works fine. How do I adjust the code to work for the MDD10A, as I only have DIR1 and DIR2, instead of EN1, IN1, IN2, IN3, IN4, En2?

For EN pin, it is same as PWM pin.
For DIR pin, instead of using 2 pins (IN1, IN2) to control direction, you just need 1 pin in MDD10A, for example:
DIR = 1, equal to IN1 = 1, IN2 = 0.
DIR = 0, equal to IN1 = 0, IN2 = 1.
If you want to stop the motor, just set PWM pin = 0.

mscholl WROTE:Also, is there a way to power the arduino thru the motor driver (I don't see a 5v connection)?

Yes, there is no 5V pin at MDD10A. However you still can use a single battery to supply both. If you are using 12V to supply the motor driver, you can use the same 12V to supply Arduino by connecting it to Vin pin.

Hope this helps. Thanks.

Re: MDD10aA with digital joystick

PostPosted: Tue Sep 12, 2017 3:52 am
by mscholl
I got it to work but would like to add a potentiometer- how do I do this? My current code is this?
int sw1=10;
int sw2=11;
int sw3=12;

int sw4=7;
int state1;
int state2;
int state3;
int state4;

//MOTOR A
int pwm1 = 9;
int dir1 = 8;


// Motor B
int pwm2 = 3;
int dir2 = 5;


void setup() {
pinMode(sw1,INPUT_PULLUP);
pinMode(sw2,INPUT_PULLUP);
pinMode(sw3,INPUT_PULLUP);
pinMode(sw4,INPUT_PULLUP);

Serial.begin(9600);

pinMode(pwm1, OUTPUT);
pinMode(pwm2, OUTPUT);
pinMode(dir1, OUTPUT);
pinMode(dir2, OUTPUT);
}

void loop() {
state1 = digitalRead(sw1);
state2 = digitalRead(sw2);
state3 = digitalRead(sw3);
state4 = digitalRead(sw4);

if (state1 == 0) { //Make a Right turn
analogWrite(pwm2, 50);
analogWrite(pwm1, 0);
digitalWrite(dir2, HIGH);
digitalWrite(dir1, LOW);
}
else if (state2 == 0) { //Forward Movement
analogWrite(pwm1, 100);
analogWrite(pwm2, 100);
digitalWrite(dir1, HIGH);
digitalWrite(dir2, HIGH);
}
else if (state3 == 0) { //Take a left turn
analogWrite(pwm1, 50);
analogWrite(pwm2,0);
digitalWrite(dir1, HIGH);
digitalWrite(dir2, LOW);
}
else if (state4 == 0) { //Backward Movement
analogWrite(pwm1, 100);
analogWrite(pwm2, 100);
digitalWrite(dir1, LOW);
digitalWrite(dir2, LOW);

}
else {
analogWrite(pwm1, 0);
analogWrite(pwm2, 0);
digitalWrite(dir1, LOW);
digitalWrite(dir2, LOW);
}

Serial.print("1-");
Serial.print(state1);
Serial.print(" 2-");
Serial.print(state2);
Serial.print(" 3-");
Serial.print(state3);
Serial.print(" 4-");
Serial.println(state4);
delay(250);
}

Re: MDD10aA with digital joystick

PostPosted: Tue Sep 12, 2017 10:36 am
by ober
I think you can just try it out, it is faster. Others do not have your setup and it is not possible for us to setup every projects in forum to test our the code.
Let us know where it is not working.