Strange Gremlin! - Delay() and motor control

Hi all,

I am running an Arduino Mega2560 with a Sparkfun Ardumoto shield, remotely mounted (not as a shield, per se). Separate power supplies -- 9V alkaline battery for the Mega, 5.4V NiMH battery for the Ardumoto. Grounds are connected between the boards.

I am finding that my Ardumoto board loses all power when I try to have the motors change direction. I figured it was some kind of overcurrent protection, so I tried debugging by introducing some delay()'s. Here is the code:

#define motR_PWM_PIN 2
#define motR_DIR_PIN 4
#define motL_PWM_PIN 3
#define motL_DIR_PIN 5

#define encR_A_PIN 21  
#define encR_B_PIN 20
#define encL_A_PIN 18
#define encL_B_PIN 19

int speed = 45;

//==============================================================
void setup(){
  
  Serial.begin(19200);
  
  //SET DIGITAL OUTPUT PINS
  pinMode(motR_PWM_PIN, OUTPUT);  pinMode(motL_PWM_PIN, OUTPUT);
  pinMode(motR_DIR_PIN,OUTPUT);  pinMode(motL_DIR_PIN,OUTPUT);
  
  //SET DIGITAL INPUT PINS
  pinMode(encR_A_PIN, INPUT);  pinMode(encL_A_PIN, INPUT);
  pinMode(encR_B_PIN, INPUT);  pinMode(encL_B_PIN, INPUT);
  
}
//==============================================================

//==============================================================
void loop(){

  digitalWrite(motR_DIR_PIN, HIGH);
  digitalWrite(motL_DIR_PIN, HIGH);
  analogWrite(motR_PWM_PIN, speed);
  analogWrite(motL_PWM_PIN, speed);
  delay(1000);
  digitalWrite(motR_DIR_PIN, LOW);
  digitalWrite(motL_DIR_PIN, HIGH);
  analogWrite(motR_PWM_PIN, speed);
  analogWrite(motL_PWM_PIN, speed);
  delay(1000);
  
}
//==============================================================

Here's the strange a disturbing part...

-The code as written results in INSTANT direction change. I see no delay, certainly not 1 full second worth.
-When I take the delay() calls out, the Ardumoto board loses power right where the first delay() is shown above, and regains power when the loop() repeats.
-When I change the delay(1000)'s to delay(1)'s, the RIGHT motor CHATTERS back and forth instead of reversing direction.

I'm at a complete loss here. The motors work correctly on their own, and I'm almost 100% positive I have everything wired correctly. Why don't the delay() calls behave correctly?

Why don't the delay() calls behave correctly?

Ask yourself - what is more likely?
delay() doesn't work as expected, or you screwed something else up?

Please, show a schematic.

A single 9V Alkaline is not going to deliver the current the board needs.

AWOL:

Why don’t the delay() calls behave correctly?

Ask yourself - what is more likely?
delay() doesn’t work as expected, or you screwed something else up?

Please, show a schematic.

I have asked myself that already. From what I understand, calling delay() should halt all other code due to its implementing of timer interrupts. How can I be sitting here watching the wheels spin if I’m supposed to be inside a 1000msec long delay? Yes, I know the function itself isn’t misbehaving, but you see the code… it’s about as straightforward as it gets. What am I missing?

As far as a schematic, not sure this is as helpful as you’d like since I am using a Mega now instead of an Uno. So, pin numbers are different but attached is the basic breakdown.

GoForSmoke:
A single 9V Alkaline is not going to deliver the current the board needs.

The Ardumoto is supplied by 4x 5.4V NiMH cells. The Arduino is the one supplied by 9V battery.

I just posted the schematic, I am using the VIN for both boards separately. I think this is how they are intended to run(?)

Although, now that I look at this schematic, I do notice that I didn't specifically show the GND's of the arduino and ardumoto being connected. I had planned to do a chassis ground between the two. This is not what is currently sitting on the bench. Something for me to check!

calling delay() should halt all other code due to its implementing of timer interrupts

That's unusual wording.
What does it mean?

What do your debug prints tell you is happening?

bpmirsch:

GoForSmoke:
A single 9V Alkaline is not going to deliver the current the board needs.

The Ardumoto is supplied by 4x 5.4V NiMH cells. The Arduino is the one supplied by 9V battery.

I just posted the schematic, I am using the VIN for both boards separately. I think this is how they are intended to run(?)

The Arduino all by itself should have more. 9V transistor batteries deliver limited current. This has been a problem for others here before.

Also how long does the motor take to move after the move commands have been sent to the motor shield?
Does the Arduino know to not count until the motor stops.

Sparkfun Ardumoto hookup instructions:

Spec'ing a Power Supply

Because VIN powers both your Arduino and your motors, you need to take extra care in deciding what you’ll use to power your Arduino/Ardumoto combo. Not only does VIN have to fall within the acceptable range of your Arduino (usually 6-15V), but it also has to meet the specifications of your motor.

Check the voltage and current requirements of your motor before deciding how to power your Ardumoto project. These specifications vary. The Mini Metal Gearmotors, for example, have a recommended range of 4.5-6V, but can be safely powered at up to 9V. The motors on a Magician Chassis have a recommended voltage of 4.5V, and a maximum of 6V (more may work, but it could also shorten the motor’s life).

Dual-cell LiPo battery packs (7.4V nominal, 1000mAh capacity) are usually a good option if you’re looking for something mobile. A 9V wall wart can work if your project is stationary. For more help picking a power supply, check out our How to Power a Project tutorial.

9V wall wart is not replaced by 1 or even 2 9V transistor batteries wired in parallel.
See if you find them mentioned in the How to Power a Project tutorial.

AWOL:

calling delay() should halt all other code due to its implementing of timer interrupts

That's unusual wording.
What does it mean?

What do your debug prints tell you is happening?

The serial.begin is a remnant from older coding. I can put debug messages in but i didnt see a reason to since the entire loop() function restarts without ever stopping. I know the motor analogwrites are working because the motor spins and the pin state changes are working because the wheels change directiom. That delay call of 1000msec seems to be totally ignored. And there is not an easy way that i know of to put debug messages inside of the actual delay() code. I dont want to edit libraries or anything.

When o speak of delay() i dont mean actually calling it without parameters.

When o speak of delay() i dont mean actually calling it without parameters.

Serial.print("Before: ");
Serial.println(millis());
delay(1000);
Serial.print("After: ");
Serial.println(millis());

Are the times approximately 1000 milliseconds apart?

bpmirsch:
I have asked myself that already. From what I understand, calling delay() should halt all other code due to its implementing of timer interrupts. How can I be sitting here watching the wheels spin if I'm supposed to be inside a 1000msec long delay?

You totally misunderstand your system. The code changes the state of the pins and the
duty-cycle of the PWM pins as it runs, but during delay those outputs and PWM pins
are stll there, in the same state, doing exactly what they were last told to do. You would
have to power-down the chip to stop that.

The wheels spin because the motor driver has certain voltages and waveforms present
on its inputs, not because the program counter is changing and instructions are being
executed.

So there needs to be a stop command and another delay? (Doh!)

The use of delay() is going to make problems further into the project.
It's past time to learn BlinkWithoutDelay.

Aaahh! My apologies, you guys are completely right that the attached sketch doesnt make sense because it doesnt have any stop commands.

The delay issue is completely resolved by that, this all came about because the motors are dying when i set the direction pins to low. In my haste trying to figure out that issue i made that test code that doesnt work! Thanks guys.

I didn't expect your power scheme to work but it does? Please, that's wiring I could use knowing!