Page 1 of 1

arduino and ardumoto motor shield basic program help

Posted: Sun Jul 19, 2009 2:43 pm
by theboosted_1
ok, so i have a little tankbot built and have set up the arduino with the moto shield. i was wondering if anyone have a simple sketch/program i can play with to test simple movements i.e left motor forward, left motor reverse etc.
i have tryed a few things and got it working but it was not correct. glitches, and unexpected issues to say the least.

im just looking for something simple to make sure everything is wired correct and to get my feet wet with the programming.
thanks for your time,
Scott

UPDATE

Posted: Sun Jul 19, 2009 7:10 pm
by theboosted_1
ok let me clarify:
most of the h bridge set ups ive seen require 2 pins for direction ( high, low) and one for pwm. (per motor)
it seems on the sparkfun ardumoto shield you get one pin high, low and one pwm (per motor)
did they use a trick to minimize the amount of pins needed or am i missing something here?
what is the best way to set this up code-wise?

any examples would be great. im sure someones used the ardu-moto right?

Posted: Tue Jul 21, 2009 11:28 am
by theboosted_1
after a closer look at the schematic for the ardumoto, i realized that it uses 2 inverters to eliminate the need for the 2 pins normally required for high/low. nice trick. i also realized that the motors in the tamiya gearbox are rated for 3v and have a stall current of around 3-4amps. not good. i think this is why i keep getting "glitches" as well as a really hot h bridge. does anyone know of a trick to run these motors correctly with the ardumoto? im planning on buying some higher voltage, lower current motors from solarbotics. just curious if anyone has had any luck with this combo. ill post a basic code to test your motors when i get home.

Posted: Tue Sep 22, 2009 7:08 am
by mechomaniac
I've been using the Ardumoto with the Tamiya gearbox successfully - you can read more about it (along with some sample source code) here: http://mechomaniac.com/node/16

To minimize glitches, I ended up running the motors on a separate supply to the Arduino. I did this by bending the Vin pin outwards so that it doesn't plug into the Arduino, and connecting this to a separate 6V battery.

motor control

Posted: Tue Dec 29, 2009 9:27 pm
by darrspil
I have tried using this code and my motor control does not work well.

For example when I issue AR200# nothing happens, then issue a BF200# and B motor will move forward. I try AR200# again and it works.

I am not great a coding, I am learning Arduino/ coding from scratch.

Thanks,

Darrspi

update on motor shield

Posted: Wed Dec 30, 2009 9:52 am
by darrspil
This is code I am using I tweeked the examples above...

I wanted a single command to move forward, backwards, left and right.

All the commands work fine but the last one. If I enter BR200# nothing happens... but any of the others work fine.

AF200#
AR200# and so on...

Code: Select all


#define PwmPinMotorA 5
#define PwmPinMotorB 6
#define DirectionPinMotorA 9
#define DirectionPinMotorB 3
#define SerialSpeed 9600
#define BufferLength 16
#define LineEnd '#'
 
char inputBuffer[BufferLength];
 
void setup()
{
  // motor pins must be outputs
  pinMode(PwmPinMotorA, OUTPUT);
  pinMode(PwmPinMotorB, OUTPUT);
  pinMode(DirectionPinMotorA, OUTPUT);
  pinMode(DirectionPinMotorB, OUTPUT);
 
  Serial.begin(SerialSpeed); 
}
 
// process a command string
void HandleCommand(char* input, int length)
{
  Serial.println(input);
  if (length < 2) { // not a valid command
    return;
  }
  int value = 0;
  // calculate number following command
  if (length > 2) {
    value = atoi(&input[2]);
  }
  int* command = (int*)input;
  // check commands
  // note that the two bytes are swapped, ie 'RA' means command AR
  switch(*command) {
    
    
    case 'FA':
      // motors A & B BACKWARDS
      analogWrite(PwmPinMotorA, value);
      digitalWrite(DirectionPinMotorA, HIGH);
      analogWrite(PwmPinMotorB, value);
      digitalWrite(DirectionPinMotorB, LOW);
      break;
      
    case 'RA':
      // motor A & B FORWARDS
      analogWrite(PwmPinMotorA, value);
      digitalWrite(DirectionPinMotorA, LOW);
      analogWrite(PwmPinMotorB, value);
      digitalWrite(DirectionPinMotorB, HIGH);
      break;
      
    case 'FB':
      // motor B forwards
      analogWrite(PwmPinMotorB, value);
      digitalWrite(DirectionPinMotorB, HIGH);
      analogWrite(PwmPinMotorA, value);
      digitalWrite(DirectionPinMotorA, HIGH);
      break;
      
    case 'RB':
      // motor B reverse
      analogWrite(PwmPinMotorB, value);
      digitalWrite(DirectionPinMotorB, LOW);
      analogWrite(PwmPinMotorA, value);
      digitalWrite(DirectionPinMotorA, LOW);
      break;
      
          default:
      break;
  }  
} 
 
void loop()
{ 
  // get a command string form the serial port
  int inputLength = 0;
  do {
    while (!Serial.available()); // wait for input
    inputBuffer[inputLength] = Serial.read(); // read it in
  } while (inputBuffer[inputLength] != LineEnd && ++inputLength < BufferLength);
  inputBuffer[inputLength] = 0; //  add null terminator
  HandleCommand(inputBuffer, inputLength);
}

arduino and ardumoto motor shield basic program help

Posted: Tue Jan 26, 2010 2:52 am
by pineapple_donut
mechomaniac wrote: To minimize glitches, I ended up running the motors on a separate supply to the Arduino. I did this by bending the Vin pin outwards so that it doesn't plug into the Arduino, and connecting this to a separate 6V battery.
What do you mean by "glitches"? I have exactly the same setup, but the 6V battery pack doesn't seem to be enough for the motors.

Re: arduino and ardumoto motor shield basic program help

Posted: Tue Mar 02, 2010 3:11 pm
by mechomaniac
Small DC motors can draw a lot of current, and if this is from the same supply as the Arduino (which is how the Ardumoto board is originally setup), then the Arduino may not run reliably (eg it may reset when it switches on the motors).