Arduino examples, Simple example – Pololu Simple User Manual

Page 88

Advertising
background image

Arduino R3, top view.

6.7.1. Arduino Examples

The

Arduino

[http://www.pololu.com/product/2191]

is a popular prototyping

platform that is well suited for beginners to the world of embedded
programming. Arduino boards are based on Atmel’s AVR microcontrollers,
like the

Orangutan robot controllers

[http://www.pololu.com/category/8/robot-

controllers]

, and are essentially programmed in C++. The Arduino uses its

hardware serial (or “UART”) lines for programming and for debugging with
the Arduino IDE’s serial monitor, so we do not recommend using these lines
to communicate with peripheral serial devices like the Simple Motor
Controller. Instead, we recommend using the

SoftwareSerial

[http://arduino.cc/

hu/Reference/SoftwareSerial]

library included with the Arduino IDE, which lets

you use arbitrary I/O lines for transmitting and receiving serial bytes. The
drawback is that software serial requires much more processing time than hardware serial.

In the following examples, we use the SoftwareSerial library to transmit bytes on digital pin 4 and receive bytes on
digital pin 3. These examples are written for Arduino 1.0 and will not work with earlier verisons of the IDE.

These sample programs require the Simple Motor Controller to have a fixed baud rate set to
19200 bps
. It must also be in Binary serial mode with the CRC Mode set to Disabled. Auto baud rate
detection can be used, but it is not recommended because of inaccuracy in the SoftwareSerial library.

Simple Example

This example assumes the following connections exist between the Arduino and the Simple Motor Controller:

• Arduino digital pin 4 to Simple Motor Controller RX

• Arduino GND to Simple Motor Controller GND

There is nothing special about Arduino pin 4; you can use any free digital pins other than 0 and 1 (the Arduino’s
hardware serial lines) if you change the pin definition at the top of the sample program accordingly. See

Section 4.2

for more information on connecting a serial device to the Simple Motor Controller.

This program demonstrates how to initiate serial communication with the Simple Motor Controller and how to send
commands to set the motor speed. For information about the serial commands used by this sample code, refer to

Section 6.2.1

. Note that the Simple Motor Controller must be powered when this Arduino sketch starts running.

#include <SoftwareSerial.h>

#define rxPin 3 // pin 3 connects to smcSerial TX (not used in this example)

#define txPin 4 // pin 4 connects to smcSerial RX

SoftwareSerial smcSerial = SoftwareSerial(rxPin, txPin);

// required to allow motors to move

// must be called when controller restarts and after any error

void exitSafeStart()

{

smcSerial.write(0x83);

}

// speed should be a number from -3200 to 3200

void setMotorSpeed(int speed)

{

if (speed < 0)

{

smcSerial.write(0x86); // motor reverse command

speed = -speed; // make speed positive

}

Pololu Simple Motor Controller User's Guide

© 2001–2014 Pololu Corporation

6. Using the Serial Interface

Page 88 of 101

Advertising