/* 6-29-10 Pete Dokter SparkFun Electronics This is an example sketch for Arduino that shows very basically how to control an Ardumoto motor driver shield with a 5V Arduino controller board (3.3V may work, but it's out of spec for the L298 H-bridge logic, so no promises). If you're plugging the Ardumoto into an Arduino Duemilanove or similar, all the connections are done for you and you can just rewrite the examples in this code to your liking. */ int pwm_a = 3; //PWM control for motor outputs 1 and 2 is on digital pin 10 int pwm_b = 9; //PWM control for motor outputs 3 and 4 is on digital pin 11 int dir_a = 2; //direction control for motor outputs 1 and 2 is on digital pin 12 int dir_b = 8; //direction control for motor outputs 3 and 4 is on digital pin 13 void setup() { pinMode(pwm_a, OUTPUT); //Set control pins to be outputs pinMode(pwm_b, OUTPUT); pinMode(dir_a, OUTPUT); pinMode(dir_b, OUTPUT); analogWrite(pwm_a, 100); //set both motors to run at (100/255 = 39)% duty cycle (slow) analogWrite(pwm_b, 100); } void loop() { digitalWrite(dir_a, LOW); //Set motor direction, 1 low, 2 high digitalWrite(dir_b, LOW); //Set motor direction, 3 high, 4 low delay(1000); analogWrite(pwm_a, 255); //set both motors to run at 100% duty cycle (fast) analogWrite(pwm_b, 255); delay(1000); digitalWrite(dir_a, HIGH); //Reverse motor direction, 1 high, 2 low digitalWrite(dir_b, HIGH); //Reverse motor direction, 3 low, 4 high delay(1000); analogWrite(pwm_a, 100); //set both motors to run at (100/255 = 39)% duty cycle analogWrite(pwm_b, 100); delay(1000); } /** * Divides a given PWM pin frequency by a divisor. * * The resulting frequency is equal to the base frequency divided by * the given divisor: * - Base frequencies: * o The base frequency for pins 3, 9, 10, and 11 is 31250 Hz. * o The base frequency for pins 5 and 6 is 62500 Hz. * - Divisors: * o The divisors available on pins 5, 6, 9 and 10 are: 1, 8, 64, * 256, and 1024. * o The divisors available on pins 3 and 11 are: 1, 8, 32, 64, * 128, 256, and 1024. * * PWM frequencies are tied together in pairs of pins. If one in a * pair is changed, the other is also changed to match: * - Pins 5 and 6 are paired on timer0 * - Pins 9 and 10 are paired on timer1 * - Pins 3 and 11 are paired on timer2 * * Note that this function will have side effects on anything else * that uses timers: * - Changes on pins 3, 5, 6, or 11 may cause the delay() and * millis() functions to stop working. Other timing-related * functions may also be affected. * - Changes on pins 9 or 10 will cause the Servo library to function * incorrectly. * * Thanks to macegr of the Arduino forums for his documentation of the * PWM frequency divisors. His post can be viewed at: * http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1235060559/0#4 */ void setPwmFrequency(int pin, int divisor) { byte mode; if(pin == 5 || pin == 6 || pin == 9 || pin == 10) { switch(divisor) { case 1: mode = 0x01; break; case 8: mode = 0x02; break; case 64: mode = 0x03; break; case 256: mode = 0x04; break; case 1024: mode = 0x05; break; default: return; } if(pin == 5 || pin == 6) { TCCR0B = TCCR0B & 0b11111000 | mode; } else { TCCR1B = TCCR1B & 0b11111000 | mode; } } else if(pin == 3 || pin == 11) { switch(divisor) { case 1: mode = 0x01; break; case 8: mode = 0x02; break; case 32: mode = 0x03; break; case 64: mode = 0x04; break; case 128: mode = 0x05; break; case 256: mode = 0x06; break; case 1024: mode = 0x7; break; default: return; } TCCR2B = TCCR2B & 0b11111000 | mode; } }