Please help! Arduino Mega & Motor shield problems

Hi, I’m using a mega (http://www.yerobot.com/shop/arduino/arduino-mega.html) and motor shield (http://www.yerobot.com/arduino-motor-shield.html).

I’m having some major problems just getting two motors to run in one direction then another. Using the following code:

int EN1 = 6;
int EN2 = 5;
int IN1 = 7;
int IN2 = 8;

void Motor1(int pwm, boolean reverse) {
analogWrite(EN1,pwm); //set pwm control, 0 for stop, and 255 for maximum speed
if(reverse) {
digitalWrite(IN1,HIGH);
}
else {
digitalWrite(IN1,LOW);
}
}
void Motor2(int pwm, boolean reverse) {
analogWrite(EN2,pwm);
if(reverse) {
digitalWrite(IN2,HIGH);
}
else {
digitalWrite(IN2,LOW);
}
}
void setup() {
int i;
for(i=5;i<=9;i++) //For Arduino Motor Shield

pinMode(i, OUTPUT); //set pin 5,6,7,8 to output mode
Serial.begin(9600);
}
void loop()
{
Motor1(100,true);
Motor2(100,true);
delay(1000);
Motor1(100,false);
Motor2(100,false);
delay(1000);
}

Only turns one motor backwards and forth, the other will just run continuosly in one direction. The above code is a simplified version of the manufacturers test code which also does not work - one wheel only goes in one direction. I have tried a different identical motor shield with the same problem.

Has anybody any idea what this could be? >:(

Page 4 of the manual available on the motor shield link that you provided says that the roboduino shield uses pins 6 and 9 to control the motors, and pins 7 and 8 to control direction. The Arduino motor shield appears to use pins 5 and 6 to control the motors.

Does your shield say Roboduino on it, by any chance?

Hi Paul,

No it doesn't say Roboduino on it, although this is the second shield I have received with the same problem so I'm really unsure whats going on? Could it be the Mega failing? I've checked the pins with a multimeter and 5&6 do the same thing, as do 7&8? I'm really stuck here :-(

I don't see that you could hurt anything trying pins 6 and 9, instead of 5 and 6.

LOL I'm at work, but I'm nipping home now to grab it to try!

Will keep you posted.

I've tried Pin 6 & 9 and it doesnt work. Whereas with pin 6 & 5 the red light on motor 1 will just stay red, if I set the pin as 9 neither the red or green light on the board will come on.

I'm really fed up with this now :-( two seperate motor shields with the same problem, using code from the manual, I've tested the voltage on the Mega running the code and it varies how I'd expect it to.

I just don't know :-(

Do the red and green lights for motor change as motor 1 changes direction?

Does motor 2 work correctly when plugged in as motor 1?

One thing about the code that strikes me as strange is that the PWM signal is sent before the direction control data. Looks to me like you are telling the motor to get going, and then saying "No, the other way", instead of telling it which way to go, then how fast.

I'd try setting the direction first, then the speed, for both motors.

Hi, with the code uploaded that I originally posted, the red and green LED for motor 2 will alternate, depending on direction. For motor 1, the red light just stays on constantly. I tried moving the analogWrite below the digitalWrite (effectively setting direction first, then speed).

Also if I plug the same motor into M2 it will alternate. Without either motor plugged in, the M2 will still alternate whilst M1 won't.

I really appreciate this help.

I can also confirm that the other Motor shield (a replacement as I thought it was faulty) does exactly the same thing..

OK, I have used a breadboard to swap pin 5&6 and 7&8 (5 on the Mega goes to 6 on the Motor shield and visa-versa.

I am displaying the same problems on M1. Surely this would suggest that it is the motor shield that is faulty. Is there anything else I can try to test either part?

Many thanks

It turned out that the pin was supposed to be 4!

Here is the code I used to get the dfrduino to work: (thankyou for your help PaulS!)

int EN1 = 6; 
int EN2 = 5;
int IN1 = 7;
int IN2 = 4;


void Motor1(int pwm, boolean reverse) {
analogWrite(EN1,pwm); //set pwm control, 0 for stop, and 255 for maximum speed
if(reverse) {
digitalWrite(IN1,HIGH);
}
else {
digitalWrite(IN1,LOW);
}
}
void Motor2(int pwm, boolean reverse) {
analogWrite(EN2,pwm);
if(reverse) {
digitalWrite(IN2,HIGH);
}
else {
digitalWrite(IN2,LOW);
}
}
void setup() {
int i;
for(i=4;i<=7;i++) //For Arduino Motor Shield

pinMode(i, OUTPUT); //set pin 5,6,7,8 to output mode
Serial.begin(9600);
}
void loop()
{
Motor1(100,true);
Motor2(100,true);
delay(1000);
Motor1(100,false);
Motor2(100,false);
delay(1000);
}