How to control 2 motors with 2 different motor shields with code

I currently have 2 cytron shield-md10 r2s that are stacked on top of each other with 2 different motors connected. I have an Arduino uno r2 at the base. I used jumper wires to connect bend the pin to the 2nd motor shield from pins 3 and 4 to 5 and 6. I am trying to get the motors to reverse however with the code I have currently it only goes forward with a positive number and with a negative number it does nothing. I currently have a joystick which I am trying to get direction and speed control from. My bad for my terrible picture. Is there something wrong with my code?

#define joystick1 A0
#define joystick2 A1
int in1 = 3;
int in2 = 4;
int in3 = 5;
int in4 = 6;
int motorSpeedA;
int motorSpeedB;


void setup() {
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
Serial.begin(9600);
}

void loop() {
int xAxis = analogRead(A1);
int yAxis = analogRead(A0);

  if (yAxis < 470) {
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    motorSpeedA = map(yAxis, 470, 0, 0, -255);
    motorSpeedB = map(yAxis, 470, 0, 0, -255);
  }
  else if (yAxis > 550) {
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    motorSpeedA = map(yAxis, 550, 1000, 0, 255);
    motorSpeedB = map(yAxis, 550, 1000, 0, 255);
  }
  else {
    motorSpeedA = 0;
    motorSpeedB = 0;
  }

  if (xAxis < 470) {
    int xMapped = map(xAxis, 470, 0, 0, 255);
    motorSpeedA = motorSpeedA - xMapped;
    motorSpeedB = motorSpeedB + xMapped;
    if (motorSpeedA < 0) {
      motorSpeedA = 0;
    }
    if (motorSpeedB > 255) {
      motorSpeedB = 255;
    }
  }
  if (xAxis > 550) {
    int xMapped = map(xAxis, 550, 1023, 0, -255);
    motorSpeedA = motorSpeedA + xMapped;
    motorSpeedB = motorSpeedB - xMapped;
    if (motorSpeedA > 255) {
      motorSpeedA = -255;
    }
    if (motorSpeedB < 0) {
      motorSpeedB = 0;
    }
  }
  if (motorSpeedA < 70) {
    motorSpeedA = 0;
  }
  if (motorSpeedB < 70) {
    motorSpeedB = 0;
  }
  analogWrite(in1, motorSpeedA);
  analogWrite(in3, motorSpeedB);
}

Your code is difficult to read and understand with the choice of variable names. It is also difficult to tell what is wrong, since none of us have your hardware setup.

Maybe add some debugging code like this at the end of the loop and see if what gets printed out is what your expect?

untested code:

  static unsigned long last = 0;
  if(millis() - last >= 1000){ // every 1000ms...
      last += 1000;
      Serial.print("xAxis: ");
      Serial.print(xAxis);
      Serial.print(" in1");
      Serial.print(digitalRead(in1));
      Serial.print(" & pwm=");
      Serial.print(motorSpeedA);
      Serial.print("; yAxis: ");
      Serial.print(yAxis );
      Serial.print(", in3=");
      Serial.print(digitalRead(in3));
      Serial.print(",  & pwm=");
      Serial.print(motorSpeedB);
      Serial.println();
  }

consider

struct Axis {
    const byte   JoyPin;
    const byte   DirPin;
    const byte   SpdPin;
    const int    ThreshLow;
    const int    ThreshHi;
};

Axis axis [] = {
#define MyHW
#ifdef MyHW
    { A0, 10, 11, 470, 550 },     // y-axis
    { A1, 12, 13, 470, 550 },     // x-axis
#else
    { A0, 3, 4, 470, 550 },      // y-axis
    { A1, 5, 6, 470, 550 },      // x-axis
#endif
};
#define N_AXIS  (sizeof(axis)/sizeof(Axis))

#define MaxInp  1023

enum { Yaxis, Xaxis };
enum { Forward = 0, Reverse = 1 };

// -----------------------------------------------------------------------------
void loop() {
    byte  dir;
    Axis *a = & axis [0];

    for (unsigned n = 0; n < N_AXIS; n++, a++)  {
        int spd = 0;
        int joy = analogRead (a->JoyPin);

        if (a->ThreshLow > joy)  {
            spd = map (joy, a->ThreshLow, 0, 0, 255);
            dir = Reverse;
        }

        else if (a->ThreshHi < joy)  {
            spd = map (joy, a->ThreshHi, MaxInp, 0, 255);
            dir = Forward;
        }

        digitalWrite (a->DirPin, dir);
        analogWrite  (a->SpdPin, spd);
        
        char s [80];
        sprintf (s, "  %4d %3d %d", joy, spd, dir);
        Serial.print (s);
    }
    Serial.println ();

    delay (200);
}

// -----------------------------------------------------------------------------
void setup() {
    Serial.begin (9600);

    for (unsigned n = 0; n < N_AXIS; n++)  {
        pinMode (axis [n].JoyPin, INPUT_PULLUP);
        pinMode (axis [n].DirPin, OUTPUT);
        pinMode (axis [n].SpdPin, OUTPUT);
    }
}

Hi aksharp,

welcome to the arduino-Forum.
well done posting code as a code-section.

change baudrate to 115200
The added debug-output will give you insight what your code really does

#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);
// usage: dbg("1:my fixed text",myVariable);
// myVariable can be any variable or expression that is defined in scope

#define dbgi(myFixedText, variableName,timeInterval) \
  do { \
    static unsigned long intervalStartTime; \
    if ( millis() - intervalStartTime >= timeInterval ){ \
      intervalStartTime = millis(); \
      Serial.print( F(#myFixedText " "  #variableName"=") ); \
      Serial.println(variableName); \
    } \
  } while (false);



#define joystick1 A0
#define joystick2 A1
int in1 = 3;
int in2 = 4;
int in3 = 5;
int in4 = 6;
int motorSpeedA;
int motorSpeedB;


void setup() {
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  int xAxis = analogRead(A1);
  int yAxis = analogRead(A0);

  dbgi("top of loop",xAxis,500);
  dbgi("top of loop",yAxis,500);
  
  if (yAxis < 470) {
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    motorSpeedA = map(yAxis, 470, 0, 0, -255);
    motorSpeedB = map(yAxis, 470, 0, 0, -255);
    dbgi("(yAxis < 470)",motorSpeedA,500);
    dbgi("(yAxis < 470)",motorSpeedB,500);
  }
  else if (yAxis > 550) {
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    motorSpeedA = map(yAxis, 550, 1000, 0, 255);
    motorSpeedB = map(yAxis, 550, 1000, 0, 255);
    dbgi("else if (yAxis > 550)",motorSpeedA,500);
    dbgi("else if (yAxis > 550)",motorSpeedB,500);
  }
  else {
    motorSpeedA = 0;
    motorSpeedB = 0;
    dbgi("else (to if (yAxis < 470) )",motorSpeedA,500);
    dbgi("else (to if (yAxis < 470) )",motorSpeedB,500);
  }


  if (xAxis < 470) {
    int xMapped = map(xAxis, 470, 0, 0, 255);
    dbgi("if (xAxis < 470)",xMapped,500);
    motorSpeedA = motorSpeedA - xMapped;
    motorSpeedB = motorSpeedB + xMapped;
    dbgi("before limiting if (xAxis < 470)",motorSpeedA,500);
    dbgi("before limiting if (xAxis < 470)",motorSpeedB,500);
    if (motorSpeedA < 0) {
      motorSpeedA = 0;
    }
    if (motorSpeedB > 255) {
      motorSpeedB = 255;
    }
    dbgi("after limiting if (xAxis < 470)",motorSpeedA,500);
    dbgi("after limiting if (xAxis < 470)",motorSpeedB,500);
  }
  
  if (xAxis > 550) {
    int xMapped = map(xAxis, 550, 1023, 0, -255);
    dbgi("if (xAxis > 550)",xMapped,500);
    motorSpeedA = motorSpeedA + xMapped;
    motorSpeedB = motorSpeedB - xMapped;
    dbgi("before limiting if (xAxis > 550)",motorSpeedA,500);
    dbgi("before limiting if (xAxis > 550)",motorSpeedB,500);
    if (motorSpeedA > 255) {
      motorSpeedA = -255;
    }
    if (motorSpeedB < 0) {
      motorSpeedB = 0;
    }
    dbgi("after limiting if (xAxis > 550)",motorSpeedA,500);
    dbgi("after limiting if (xAxis > 550)",motorSpeedB,500);
  }
  
  if (motorSpeedA < 70) {
    motorSpeedA = 0;
  }
  if (motorSpeedB < 70) {
    motorSpeedB = 0;
  }
  
  dbgi("analogWrite(in1, motorSpeedA)",motorSpeedA,500);
  dbgi("analogWrite(in3, motorSpeedB)",motorSpeedB,500);
  analogWrite(in1, motorSpeedA);
  analogWrite(in3, motorSpeedB);
}

best regards Stefan

1 Like

Hi,
Welcome to the forum.

Can you just start some code to try and control ONE motor driver.
Forget about both, just one, it will make your code simpler and easier to follow.

Have you looked at this github, it has an example code.

OR a library.

Tom... :smiley: :+1: :coffee: :australia:

1 Like

Thanks for the replies everyone, I found the problem! The pins for the motor are actually 2 and 3 instead of 3 and 4 and with the code from GitHub - CytronTechnologies/Cytron_ShieldMD10: Check in example sketch for Cytron Shield-MD10, 10A Motor driver for Arduino
It seems to be working. I don't have enough time for testing the actual code that is in my original post but hopefully it will work. Thanks for the help everyone.

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