Running 4-DC motors

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

motor shield tutorial

Have you looked at this tutorial?

You might want to read this, too.

http://forum.arduino.cc/index.php/topic,148850.0.html

The example from Adafruit changes directions automatically after a short period of time (e.g. 1000ms) as you know, but I send my commands via USB just when I want to change direction or use another motor. I tried to solve the problem with a "switch case", but I was not able to.

I never learned programming and I think that is why my "switch case" won't work. So may I ask you for a more detailed example? I do not use a pin for my commands and as a result I do not know what my (var) is.

Do you have the V1 or V2 motor shield?

V2

Then here’s some code to run 1 motor according to what you send it from the serial monitor. Send it a ‘f’ to run it forward, a ‘b’ to run it backward, and a ‘s’ to stop it. Enjoy!

#include <Wire.h>
#include <Adafruit_MotorShield.h>
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
void setup() {
  // put your setup code here, to run once:
   Serial.begin(9600);
   AFMS.begin();
}

void loop() {
  // put your main code here, to run repeatedly:
  if(Serial.available() > 0){
    int data = Serial.read();
    switch(data){
      case 'f':
      Serial.println("Forward!");
      myMotor->setSpeed(255);
      myMotor->run(FORWARD);
      break;
      case 'b':
      Serial.println("Backward!");
      myMotor->setSpeed(255);
      myMotor->run(BACKWARD);
      break;
      case 's':
      Serial.println("Stopped!");
      myMotor->setSpeed(0);
      myMotor->run(RELEASE);
      break;
    }
  }
}

Thank you very much, my commands were a bit more complicated! :)

Do you reply to personal messages? If yes can I send you my recent code? I think I will have some future questions too and so I won't have to explain my project everytime when I need help.

Regards ADjustMAD

Yes, I reply to PMs :) But I do not answer PMs for help unless you are going to pay me ;)

Here’s a commented version:

//The shield communicates over I2C
#include <Wire.h>//Needed to communicate with the shield
#include <Adafruit_MotorShield.h> //The library to simplify commands
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); //Declaring a shield 
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);//Declaring a motor on connector #1
void setup() {
  // put your setup code here, to run once:
   Serial.begin(9600);
   AFMS.begin(); //Starting the shield
}

void loop() {
  // put your main code here, to run repeatedly:
  if(Serial.available() > 0){
    int data = Serial.read();//Reading the serial port
    switch(data){
      case 'f':
      Serial.println("Forward!");
      myMotor->setSpeed(255);//full speed
      myMotor->run(FORWARD);//running forward
      break;
      case 'b':
      Serial.println("Backward!");
      myMotor->setSpeed(255);//full speed
      myMotor->run(BACKWARD);//running backward 
      break;
      case 's':
      Serial.println("Stopped!");
      myMotor->setSpeed(0);//0 speed
      myMotor->run(RELEASE);//stopped
      break;
    }
  }
}

Great, thanks a lot!