How to Arduino
==============
Arduino is an open-source hardware and software company, project
and user community that designs and manufactures single-board
microcontrollers and microcontroller kits for building digital
devices at www.arduino.cc. Products are licensed under the GNU
Lesser General Public License (LGPL) or the GNU General Public
License (GPL),permitting the manufacture of Arduino boards and
software distribution by anyone. Arduino boards are available
commercially in preassembled form or as do-it-yourself (DIY)
kits.
Arduino board designs use a variety of microprocessors and
controllers. The boards are equipped with sets of digital and
analog input/output (I/O) pins that may be interfaced to various
expansion boards or breadboards (shields) and other circuits.
The boards feature serial communications interfaces, including
Universal Serial Bus (USB) on some models, which are also used
for loading programs from personal computers. The
microcontrollers can be programmed using C and C++ programming
languages. In addition to using traditional compiler toolchains,
the Arduino project provides an integrated development
environment (IDE) based on the Processing language project.
Form factor is similar to RPIs, programming model differs
considerably. There is no command line, no terminal, no
keyboard, no OS. CPUs include tmel AVR (8-bit), ARM Cortex-M0+
(32-bit), ARM Cortex-M3 (32-bit), Intel Quark (x86) (32-bit).
Memory is tight, typically between 1kb and 8kb static RAM,
storage is flash memory (32-256kB) and EEPROM.
To install the IDE on a Pi, invoke:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install arduino
To run the IDE, an X11 server is required. For headless
configurations, xvnc does just fine. The Arduino package
provided by apt-get is said to be outdated. Instructions to
rebuild latest from scratch are available, reasaonably
straightforward to follow.
An Arduino talks to its host (Windows, Linux, BSD, Pi, Mac, ...)
through USB, presents itself as yet another serial port named
/dev/ttyACM#, 0 <= #. Enumeration across devices appears to be
predictable and constant. Power draw might be an issue, if so,
drive Arduinos from a powered hub.
To control an Arduino, hook it up, invoke, from within X11,
"arduino", configure, via Tools->Board and Tools->Serial Port
what type of Arduino is listening on which port.
Arduinos are programmed in C/C++. Unlike a C program on a Pi,
there is no main() routine, no stdin and no stdout. An Arduino
program (called a sketch, source code resides in an .ino file)
contains at least two functions:
void setup(void)
and
void loop(void)
Setup() gets invoked once at program startup. Initialization
code goes there, while loop() gets invoked indefinitely
repeatedly.
Communication with the outside world takes place via a serial
line implemented on top of USB. Serial data has to be picked up
using the Serial object, described at
It might be a good idea to check for presence of input on the
serial "port" ever so often, using Serial.available(), then one
of the reader functions to pick up data, and/or implementing an input
handler named
void serialEvent(void)
which the Arduino environment calls between invocations of
loop(). Means spending too much time in loop() causes serial
input to get handled delayed.
To handle input from within loop():
void identify(const char *theFile, int theLine)
{
Serial.print("Uno-R3 : ");
Serial.print(theFile);
Serial.print(" @ ");
Serial.print(theLine);
Serial.print(" : ");
}
void inputHandler()
{
[...]
while (Serial.available())
{
c = (char)Serial.read();
}
[...]
}
void serialEvent()
{
identify(__FILE__,__LINE__);
Serial.println("have serial event.");
inputHandler();
}
void Delay(int n)
{
while (n > 0)
{
delay(100);
n -= 100;
inputHandler();
}
}
void loop()
{
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
Delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
Delay(1000); // wait for a second
}
Check
for Arduino-specific functions.