I have a brand new Arduino Uno R3 verified working, running Arduino IDE v1.0.5 on windows 7 64bit. I am trying to get the demo sketch for Pololu Dual MC33926 Motor Driver Shield for Aduino, P.N. #2503 and it will not compile!
Here is my sketch copied verbatim:
#include "DualMC33926MotorShield.h"
DualMC33926MotorShield md;
void stopIfFault()
{
if (md.getFault())
{
Serial.println("fault");
while(1);
}
}
void setup()
{
Serial.begin(115200);
Serial.println("Dual MC33926 Motor Shield");
md.init();
}
void loop()
{
for (int i = 0; i <= 400; i++)
{
md.setM1Speed(i);
stopIfFault();
if (abs(i)%200 == 100)
{
Serial.print("M1 current: ");
Serial.println(md.getM1CurrentMilliamps());
delay(2);
}
for (int i = 400; i >= -400; i--)
{
md.setM1Speed(i);
stopIfFault();
if (abs(i)%200 == 100)
{
Serial.print("M1 current: ");
Serial.println(md.getM1CurrentMilliamps());
}
delay(2);
}
for (int i = -400; i <= 0; i++)
{
md.setM1Speed(i);
stopIfFault();
if (abs(i)%200 == 100)
{
Serial.print("M1 current: ");
Serial.println(md.getM1CurrentMilliamps());
}
delay(2);
}
for (int i = 0; i <= 400; i++)
{
md.setM2Speed(i);
stopIfFault();
if (abs(i)%200 == 100)
{
Serial.print("M2 current: ");
Serial.println(md.getM2CurrentMilliamps());
}
delay(2);
}
for (int i = 400; i >= -400; i--)
{
md.setM2Speed(i);
stopIfFault();
if (abs(i)%200 == 100)
{
Serial.print("M2 current: ");
Serial.println(md.getM2CurrentMilliamps());
}
delay(2);
}
for (int i = -400; i <= 0; i++)
{
md.setM2Speed(i);
stopIfFault();
if (abs(i)%200 == 100)
{
Serial.print("M2 current: ");
Serial.println(md.getM2CurrentMilliamps());
}
delay(2);
}
}
}
I also copied the libraries from git hub and verified imported into Arduino:
#include "DualMC33926MotorShield.h"
// Constructors ////////////////////////////////////////////////////////////////
DualMC33926MotorShield::DualMC33926MotorShield()
{
//Pin map
_nD2 = 4;
_M1DIR = 7;
_M2DIR = 8;
_nSF = 12;
_M1FB = A0;
_M2FB = A1;
}
DualMC33926MotorShield::DualMC33926MotorShield(unsigned char M1DIR, unsigned char M1PWM, unsigned char M1FB,
unsigned char M2DIR, unsigned char M2PWM, unsigned char M2FB,
unsigned char nD2, unsigned char nSF)
{
//Pin map
//PWM1 and PWM2 cannot be remapped because the library assumes PWM is on timer1
_nD2 = nD2;
_M1DIR = M1DIR;
_M2DIR = M2DIR;
_nSF = nSF;
_M1FB = M1FB;
_M2FB = M2FB;
}
// Public Methods //////////////////////////////////////////////////////////////
void DualMC33926MotorShield::init()
{
// Define pinMode for the pins and set the frequency for timer1.
pinMode(_M1DIR,OUTPUT);
pinMode(_M1PWM,OUTPUT);
pinMode(_M1FB,INPUT);
pinMode(_M2DIR,OUTPUT);
pinMode(_M2PWM,OUTPUT);
pinMode(_M2FB,INPUT);
pinMode(_nD2,OUTPUT);
digitalWrite(_nD2,HIGH); // default to on
pinMode(_nSF,INPUT);
#if defined(__AVR_ATmega168__)|| defined(__AVR_ATmega328P__)
// Timer 1 configuration
// prescaler: clockI/O / 1
// outputs enabled
// phase-correct PWM
// top of 400
//
// PWM frequency calculation
// 16MHz / 1 (prescaler) / 2 (phase-correct) / 400 (top) = 20kHz
TCCR1A = 0b10100000;
TCCR1B = 0b00010001;
ICR1 = 400;
#endif
}
// Set speed for motor 1, speed is a number betwenn -400 and 400
void DualMC33926MotorShield::setM1Speed(int speed)
{
unsigned char reverse = 0;
if (speed < 0)
{
speed = -speed; // Make speed a positive quantity
reverse = 1; // Preserve the direction
}
if (speed > 400) // Max PWM dutycycle
speed = 400;
#if defined(__AVR_ATmega168__)|| defined(__AVR_ATmega328P__)
OCR1A = speed;
#else
analogWrite(_M1PWM,speed * 51 / 80); // default to using analogWrite, mapping 400 to 255
#endif
if (reverse)
digitalWrite(_M1DIR,HIGH);
else
digitalWrite(_M1DIR,LOW);
}
// Set speed for motor 2, speed is a number betwenn -400 and 400
void DualMC33926MotorShield::setM2Speed(int speed)
{
unsigned char reverse = 0;
if (speed < 0)
{
speed = -speed; // Make speed a positive quantity
reverse = 1; // Preserve the direction
}
if (speed > 400) // Max PWM dutycycle
speed = 400;
#if defined(__AVR_ATmega168__)|| defined(__AVR_ATmega328P__)
OCR1B = speed;
#else
analogWrite(_M2PWM,speed * 51 / 80); // default to using analogWrite, mapping 400 to 255
#endif
if (reverse)
digitalWrite(_M2DIR,HIGH);
else
digitalWrite(_M2DIR,LOW);
}
// Set speed for motor 1 and 2
void DualMC33926MotorShield::setSpeeds(int m1Speed, int m2Speed)
{
setM1Speed(m1Speed);
setM2Speed(m2Speed);
}
// Return motor 1 current value in milliamps.
unsigned int DualMC33926MotorShield::getM1CurrentMilliamps()
{
// 5V / 1024 ADC counts / 525 mV per A = 9 mA per count
return analogRead(_M1FB) * 9;
}
// Return motor 2 current value in milliamps.
unsigned int DualMC33926MotorShield::getM2CurrentMilliamps()
{
// 5V / 1024 ADC counts / 525 mV per A = 9 mA per count
return analogRead(_M2FB) * 9;
}
// Return error status
unsigned char DualMC33926MotorShield::getFault()
{
return !digitalRead(_nSF);
}
#ifndef DualMC33926MotorShield_h
#define DualMC33926MotorShield_h
#include <Arduino.h>
class DualMC33926MotorShield
{
public:
// CONSTRUCTORS
DualMC33926MotorShield(); // Default pin selection.
DualMC33926MotorShield(unsigned char M1DIR, unsigned char M1PWM, unsigned char M1FB,
unsigned char M2DIR, unsigned char M2PWM, unsigned char M2FB,
unsigned char nD2, unsigned char nSF); // User-defined pin selection.
// PUBLIC METHODS
void init(); // Initialize TIMER 1, set the PWM to 20kHZ.
void setM1Speed(int speed); // Set speed for M1.
void setM2Speed(int speed); // Set speed for M2.
void setSpeeds(int m1Speed, int m2Speed); // Set speed for both M1 and M2.
unsigned int getM1CurrentMilliamps(); // Get current reading for M1.
unsigned int getM2CurrentMilliamps(); // Get current reading for M2.
unsigned char getFault(); // Get fault reading.
private:
unsigned char _nD2;
unsigned char _M1DIR;
unsigned char _M2DIR;
static const unsigned char _M1PWM = 9;
static const unsigned char _M2PWM = 10;
unsigned char _nSF;
unsigned char _M1FB;
unsigned char _M2FB;
};
#endif
I cannot copy the output error messages from Arduino IDE and there are far too many to type in here, but basically I think there is some inconsistentcy in Arduino.h which snowballs down the line.