Cant get motors to run using specific code

So i have 2 3v motors using a adafruit motor shield... The problem that im having is that if i run specific code it doesnt work (examples underneath)

So i tried with a single 9v powering everything, also installed 0.1 caps on the motors, tried with a 2, 3 and a 4 cell AA holder and no go.. When i tried to do a delay, or a forward follow by something else and upload the code everything freaks out it doesnt start, it tries but it doesnt

like if i use this code:

motor1.run(FORWARD); // turn it on going forward
motor2.run(FORWARD); // turn it on going forward

or

motor1.run(BACKWARD); // turn it on going backward
motor2.run(BACKWARD); // turn it on going backward

it works fine

or if i use this:

motor1.run(FORWARD); // turn it on going forward
motor2.run(BACKWARD); // turn it on going backward

it works fine..

if i use this:

motor1.run(FORWARD); // turn it on going forward
motor2.run(BACKWARD); // turn it on going backward
delay(2000);

it freaks out tries to start but it doesnt, then it tries again and fails..

also tried this:

motor1.run(FORWARD); // turn it on going forward
motor2.run(FORWARD); // turn it on going forward
delay(2000);
motor1.run(RELEASE); 
motor2.run(RELEASE);

and it freaks out....

The delay function is a "blocking function", nothing happens on Arduino for 2000 ms when you call delay(2000).

I don't know the motor library but my guess is that the blocking nature of the delay function is preventing the motor librarys code from running.

Or if the motorshield is using PWM pins on Arduino the delay is messing with the PWM timers.

i mean when i use the delay function the motors dont even start.. i mean you can hear them trying to start but they dont... so not sure whats going on.. :frowning:

tried putting another 3v motor outside of the gearbox with code that didnt work and it works fine puted the same cables to the 3v motor inside the gear box and no go.... not sure whats going on...

I sounds like your batteries are not able to deliver enough current to get the motors running when they are under load.

Try with a powersupply that can put out more current.

When a DC motor starts up, for a short while it can draw a lot more current than when it'srunning. Also a free running DC motor draws less current than a moter that "is doing work" like driving your gearbox.

but if my code says just forward it works just fine.... the problem is whn i put a delay on the code.. :S

How does this work for you?

#include <AFMotor.h>

AF_DCMotor motor(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Motor test!");
  
  motor.setSpeed(200);     // set the speed to 200/255
}

void loop() {
  Serial.print("tick");
  
  motor.run(FORWARD);      // turn it on going forward
  delay(1000);

  Serial.print("tock");
  motor.run(BACKWARD);     // the other way
  delay(1000);
  
  Serial.print("tack");
  motor.run(RELEASE);      // stopped
  delay(1000);
}

that code and a good little walk through of this found here.
http://www.ladyada.net/make/mshield/use.html

when i run that code, the wheel starts spinning forward for 2 seconds, it then stop for half of second and start spinning forward for 1 second, then it spins backward for half of second then stops for 1 second. then it tries to start and stop within 1 second, then it runs forward for 5 seconds...

No idea what is doing :S

It should run forward for 1 second, then back for 1 second, stop for 1 second and then repeat... Is not doing this at all :frowning:

but at that same code i comment everything but the part to go forward and and delays it works just fine

and if i do this:

#include <AFMotor.h>

AF_DCMotor motor(1, MOTOR12_64KHZ); // create motor #2, 64KHz pwm

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Motor test!");

  motor.setSpeed(200);     // set the speed to 200/255
}

void loop() {
  Serial.print("tick");

  motor.run(FORWARD);      // turn it on going forward
  delay(3000);

  Serial.print("tock");
  motor.run(BACKWARD);     // the other way
  delay(3000);

  Serial.print("tack");
  motor.run(RELEASE);      // stopped
  delay(3000);
}

It runs backwards 6 seconds, then stops for half of second runs forward for 1 second stops for half of second then 6 seconds backwards

Wish i had a the shield to test it myself. But its starting to seam like there may be an issue with the shield.