Hardware Reference
In-Depth Information
Sketch
Import Library menu. There, you will see a list of available libraries.
Clicking on one of these will automatically import the library.
Before you can use a software serial implementation, you must i rst import the
library and create an instance of the SoftwareSerial class called an object. When
instantiating the object, it requires two parameters: the pin used for receiving
data and the pin used to send data. Just like the Serial class, you typically call
begin() in setup() . The methods used by SoftwareSerial are just like those used
with Serial, so print() , println() , available() , and the rest work the same.
#include <SoftwareSerial.h>
#define rxPin 10
#define txPin 11
// set up a new software serial port instance
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
void setup()
{
mySerial.begin(4800);
mySerial.println("Hello, world!");
}
The SoftwareSerial object has its own internal buffer of 64 characters. If it
receives any more characters, it will overl ow. To check the overl ow status of
the buffer, the call overflow() function can be used:
bool result = mySerial.overflow();
This function checks the internal overl ow l ag and automatically resets it.
Subsequent calls to this function will report no overl ow, unless more data has
been received, causing another overl ow.
SoftwareSerial requires a pin that supports change interrupts, which, depend-
ing on your model, is not available on all pins. The Mega2560 can use pins 10
through 15, 50 to 53, and A8 to A15 for RX. On the Leonardo, pins 8 through 11
and 14 to 16 can be used. The transmit pin does not require interrupt support,
so any digital pin can be used. For more information about interrupt pins on
your Arduino, check Arduino's website for your specii c board.
Summary
In this chapter you have seen how to open and close serial communications,
allowing you to connect to your Arduino and how to exchange information.
In the next chapter you will see how to store long-term data on your Arduino
using the EEPROM library.
 
Search WWH ::




Custom Search