If you’ve dabbled in Basic, you just need to remember
that in C, keywords and variables are case sensitive. Instead
of using If/End If, in C, code blocks are grouped together
using the { and } (brace) characters. Statements are
terminated with a ; (semi-colon) character, rather than just a
simple line break. Any other differences, you’ll pick up
quickly.
To get started with programming your Arduino, first go
to: http://arduino.cc and then click on the Download tab.
Find the platform link (PC, Mac, Linux) for your computer
and download the installation file. Step-by-step instructions
are provided in the Getting Started section of the Arduino
website. Be sure to read through the entire instructions.
Be aware that the main Getting Started section
assumes you’re using an Arduino Uno, Duemilanove, Nano,
or Diecimila board. If you’re using another version of the
Arduino, be sure to check out its corresponding page on
the site.
Once installation is complete, you’re ready to try out
your Arduino. Start by connecting the board to your PC via
a USB cable. If this is the first time you’ve used an Arduino
on your PC, you must install the USB communications
drivers, as detailed in the Getting Started guide.
Using the Arduino programming environment is simple.
First-time use of the environment requires you to specify the
Arduino board you are using, and as necessary, the serial
port that is connected to the board (the Arduino’s USB
connection looks like a serial port to your computer). You
may then open an existing example program which is called
a sketch in Arduino parlance, and download the program to
your board. Or, you may write your own sketch in the IDE
editor. Figure 3 shows the Arduino IDE with a short sketch
in the main window.
After writing or opening an existing sketch, you need
to compile it which prepares the code for downloading to
the Arduino. The Arduino IDE calls compiling a program
verifying. At the bottom of the text editor is a status
window which shows you the progress of compiling
(verifying). If the sketch is successfully compiled, it can then
be downloaded to the Arduino where it will automatically
run once the download is complete.
Programming for Robots
As you go through the list of programming statements
available in the Arduino IDE (choose Help->Reference), you
might think there isn’t much power for doing things like
running servos, operating stepper motors, reading
potentiometers, or displaying text on an LCD.
Like most any language based on C, the Arduino
supports the notion of “libraries” — code repositories that
extend core programming functionality. Libraries let you
re-use code without having to physically copy and paste it
into all your programs. The standard Arduino software
installation comes with several libraries you may use,
and you can download others from the Arduino support
pages and from third-party websites that publish
FIGURE 4.
Schematic for
Listing 1
testing
circuit.
Arduino library code.
A good example of a library you’ll use with the ArdBot
— and likely many other robot projects — is Servo. This
library allows you to connect one or more hobby R/C servos
to the Arduino’s digital I/O pins. The Servo library comes
with the standard Arduino installation package, so adding it
to your sketch is as simple as choosing Sketch->Import
Library->Servo. This adds the line
#include <Servo.h>
which tells the Arduino IDE that you wish to include the
Servo library in your sketch. With the functionality of the
library now available to you, you can use its various
functions to control one or more servos. For example,
you can use the write function to rotate a servo to a
specific position, from 0 to 180 degrees. The following
code
myServo.write(90);
moves a servo to its midpoint, or 90 degree position.
Structurally, Arduino sketches are very straightforward
and are pretty easy to read and understand. The Arduino
program contains two main parts: setup() and loop(). These
are programming functions that do what their names
suggest: setup() sets up the Arduino hardware, such as
specifying which I/O lines you plan to use, and whether
they are inputs or outputs. The loop() function is repeated
endlessly when the Arduino is operating.
SERVO 11.2010 61