Pololu Simple User Manual
Page 95

{
command[0] = 0xA1;
command[1] = variableID;
sendBlocking(command, 2);
unsigned int response;
if (receiveBlocking((char *)&response, 2, 500))
return 0; // if we don't get a response in 500 ms, return 0
return response;
}
// initialization code called once when the program starts running
void setup()
{
setBaudRate(115200);
// briefly reset SMC when Arduino starts up (optional)
set_digital_output(resetPin, LOW);
delay_ms(1); // wait 1 ms
set_digital_input(resetPin, HIGH_IMPEDANCE); // let SMC run again
// must wait at least 1 ms after reset before transmitting
delay_ms(5);
// this lets us read the state of the SMC ERR pin (optional)
set_digital_input(errPin, HIGH_IMPEDANCE);
command[0] = 0xAA;
sendBlocking(command, 1); // send baud-indicator byte
setMotorLimit(FORWARD_ACCELERATION, 4);
setMotorLimit(REVERSE_ACCELERATION, 10);
setMotorLimit(DECELERATION, 20);
// clear the safe-start violation and let the motor run
exitSafeStart();
}
// main loop of the program; this executes over and over while the program runs
void loop()
{
static int speed = 3200; // full-speed forward
setMotorSpeed(speed);
speed = -speed; // switch motor direction
clear(); // clear the LCD and move cursor to start of first row
print("ts=");
// signed variables must be cast to ints:
print_long((int)getVariable(TARGET_SPEED));
lcd_goto_xy(0, 1); // move LCD cursor to start of second row
if (is_digital_input_high(errPin))
{
// if an error is stopping the motor, print the error status variable
// in hex and try to re-enable the motor
print("Err=");
print_hex(getVariable(ERROR_STATUS));
// once all other errors have been fixed, this lets the motor run again
exitSafeStart();
}
else
{
// print input voltage (in Volts) to the LCD
print("VIN=");
unsigned int vin = getVariable(INPUT_VOLTAGE);
// print truncated whole number of Volts
print_unsigned_long(vin/1000);
print_character('.');
// print rounded tenths of a Volt
print_unsigned_long(((vin%1000) + 50) / 100);
}
delay_ms(1000);
}
// program execution starts here
int main()
{
Pololu Simple Motor Controller User's Guide
© 2001–2014 Pololu Corporation
6. Using the Serial Interface
Page 95 of 101