4 DC motors with Adafruit motorshield v2.3

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
ADjustMAD
 
Posts: 15
Joined: Sun May 24, 2015 6:26 am

4 DC motors with Adafruit motorshield v2.3

Post by ADjustMAD »

Hi guys,

I am trying to run 4 DC motors with the motorshield from adafruit and I use an Arduino Mega 2560.

The Motors are not powered yet, I just try to get them running using the Serial Monitor of the Arduino software. The motors should run forward and backward using a chute. Up to now I am just running them forward and I don't know how to change their direction.

Here is the start of my code:
int pwmLeftNow = 0;

int pwmLeftTarget = 0;

unsigned long timer1000ms = 0;
unsigned long timerPWM = 0;


void setup() {

motorLeft->setSpeed(0);
motorLeft->run(FORWARD);



Looking forward to your early reply,
ADjustMAD

User avatar
adafruit_support_bill
 
Posts: 88091
Joined: Sat Feb 07, 2009 10:11 am

Re: 4 DC motors with Adafruit motorshield v2.3

Post by adafruit_support_bill »

Take a look at the DCMotorTest example in the library. It shows how to change directions.

https://github.com/adafruit/Adafruit_Mo ... orTest.ino

User avatar
ADjustMAD
 
Posts: 15
Joined: Sun May 24, 2015 6:26 am

Re: 4 DC motors with Adafruit motorshield v2.3

Post by ADjustMAD »

The example changes dirctions automatically after a short period of time (e.g. 1000ms) as you know, but I give commands when I want to change direction via USB. I tried to solve the problem with a "switch case", but I was not able to.

I never learned programming and I think thats why my "switch case" won't work. So may I ask you for an more useable example than the one from Arduino? I do not use a pin for my commands and as a result I do not know what hast to be equal.

User avatar
adafruit_support_bill
 
Posts: 88091
Joined: Sat Feb 07, 2009 10:11 am

Re: 4 DC motors with Adafruit motorshield v2.3

Post by adafruit_support_bill »

So, the question is not so much about how to change directions, but how to interpret commands.

What commands are you sending and how are you sending them?

User avatar
ADjustMAD
 
Posts: 15
Joined: Sun May 24, 2015 6:26 am

Re: 4 DC motors with Adafruit motorshield v2.3

Post by ADjustMAD »

Code: Select all

void checkCommand() {
    if (kommando != "") {
    if (kommando.startsWith("left=")) {
         long value = kommando.substring(5).toInt();
         String header = "new value for left=";
         Serial.println(header + value);
         pwmLeftTarget = value;
    } else if (kommando.startsWith("right=")) {
         long value = kommando.substring(6).toInt();
         String header = "new value for right=";
         Serial.println(header + value);
         pwmRightTarget = value; 
    }
} else {
      Serial.println("invalid input " + kommando);
    }
    kommando = "";
  }
}

void checkMotorLeft() {
      // check and set new pwm values for motor left and right
    if (pwmLeftNow != pwmLeftTarget) {
      if (pwmLeftNow < pwmLeftTarget) {
        pwmLeftNow += 1;
      } else {
        pwmLeftNow -= 1;
      }
      motorLeft->setSpeed(pwmLeftNow);
      if (showDebug) {
        Serial.print("set speed left to ");
        Serial.println(pwmLeftNow);
      }
    }
}

void checkMotorRight() {
    if (pwmRightNow != pwmRightTarget) {
      if (pwmRightNow < pwmRightTarget) {
        pwmRightNow += 1;
      } else {
        pwmRightNow -= 1;
      }
      motorRight->setSpeed(pwmRightNow);
      if (showDebug) {
        Serial.print("set speed right to ");
        Serial.println(pwmRightNow);
      }
    }
}
Last edited by adafruit_support_bill on Sun May 24, 2015 2:19 pm, edited 1 time in total.
Reason: please use the </> button when submitting code. press </>, then paste your code between the [code] [/code] tags.

User avatar
ADjustMAD
 
Posts: 15
Joined: Sun May 24, 2015 6:26 am

Re: 4 DC motors with Adafruit motorshield v2.3

Post by ADjustMAD »

Sorry forgot the beginning:

Code: Select all

String inputString = "";         // a string to hold incoming data
String kommando = "";         // a string to hold current command
boolean inMessage = false;
boolean showDebug = true;
String stx = "<";
String etx = ">";
int lastTemp = -9999;
int lastInput = -1;


int pwmLeftNow = 0;
int pwmRightNow = 0;


int pwmLeftTarget = 0;
int pwmRightTarget = 0;


unsigned long timer1000ms = 0;
unsigned long timerPWM = 0;
Last edited by adafruit_support_bill on Sun May 24, 2015 2:19 pm, edited 1 time in total.
Reason: please use the </> button when submitting code. press </>, then paste your code between the [code] [/code] tags.

User avatar
adafruit_support_bill
 
Posts: 88091
Joined: Sat Feb 07, 2009 10:11 am

Re: 4 DC motors with Adafruit motorshield v2.3

Post by adafruit_support_bill »

I still don't see where the commands are coming from.

Post the complete code you are using and describe what exactly it does or does not do correctly.

When posting code to the forums, please use the '</>' code button above the message edit window. Paste your code between the

Code: Select all

 tags.

User avatar
ADjustMAD
 
Posts: 15
Joined: Sun May 24, 2015 6:26 am

Re: 4 DC motors with Adafruit motorshield v2.3

Post by ADjustMAD »

I tried a totally new way, I will see if it works, but thanks for your effort anyway. If I have any future questions I will contact you again. ;)

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”