Hardware Reference
In-Depth Information
NOTE: If you've got experience with the Basic Stamp or PicBasic
Pro, you will find Arduino serial communications a bit different
than what you are used to. In PBasic and PicBasic Pro, the
serial pins and the data rate are defined each time you send a
message. In Wiring and Arduino, the serial pins are unchangeable,
and the data rate is set at the beginning of the program. This
way is a bit less flexible than the PBasic way, but there are some
advantages, as you'll see shortly.
Where's My Serial Port?
The USB serial port that's associated with the Arduino
or Wiring module is actually a software driver that loads
every time you plug in the module. When you unplug,
the serial driver deactivates and the serial port will
disappear from the list of available ports. You might also
notice that the port name changes when you unplug
and plug in the module. On Windows machines, you may
get a new COM number. On Macs, you'll get a different
alphanumeric code at the end of the port name.
Never unplug a USB serial device when you've got its
serial port open; you must exit the Wiring or Arduino
software environment before you unplug anything.
Otherwise, you're sure to crash the application, and
possibly the whole operating system, depending on how
well behaved the software driver is.
Try It
/*
Simple Serial
Context: Arduino
Listens for an incoming serial byte, adds one to the byte
and sends the result back out serially.
Also blinks an LED on pin 13 every half second.
*/
This next Arduino program listens for
incoming serial data. It adds one to
whatever serial value it receives, and
then sends the result back out. It also
blinks an LED on pin regularly—on the
same pin as the last example—to let
you know that it's still working.
int LEDPin = 13; // you can use any digital I/O pin you want
int inByte = 0; // variable to hold incoming serial data
long blinkTimer = 0; // keeps track of how long since the LED
// was last turned off
int blinkInterval = 1000; // a full second from on to off to on again
void setup() {
pinMode(LEDPin, OUTPUT); // set pin 13 to be an output
Serial.begin(9600); // configure the serial port for 9600 bps
// data rate.
}
void loop() {
// if there are any incoming serial bytes available to read:
if (Serial.available() > 0) {
// then read the first available byte:
inByte = Serial.read();
// and add one to it, then send the result out:
»
 
Search WWH ::




Custom Search