In part 3 we sent and received single characters to control LEDs using a fairly simple technique. If all you need is to remotely turn a few things on and off then this method is probably the best. It is simple, easy to program, and reliable. Sometimes though single characters are not enough and we need to use more complex commands or we may want to send sensor data that comprises more than one character.
In this post I look at a few different techniques for sending complex data and commands; starting with functions that are built in the Arduino language and moving to our own functions that, IMHO, perform better and allow for better code.
A common mistake a lot of beginners make is to test data before they have it. When receiving more than one character via serial, it is easy to assume that all the data arrives at one time. It does not. When a device sends “HELLO” it is sent one character at a time and received one character at a time. The receiving device then has to put all the characters together to form the word “HELLO”. By Arduino standards serial is very slow and the Arduino is capable of performing thousands of tasks in the time it takes to receive all the characters. This means if you are not careful your code can start checking the received data before you have actually received it.
Another problem I have seen is thinking a serial.read reads all the available data. It doesn’t. It reads one character or byte only and it is up to you to read all the data and put it together.
In the last post I briefly talked about different data formats and how I recommend keeping things as simple as possible. With this is mind for a first project let’s create a simple blinking LED. We will have one Arduino controlling an LED on a second Arduino. Controls to turn the LED on or off will be sent via serial from the first Arduino to the second Arduino. This is as basic as it gets. Arduino Blink by remote control. The LED has only two states so simple control codes can be used and to start I am using 1 of on and 0 for off.
In these examples I am using Arduino Nanos but any kind of Arduino can be used and for this series I am using Arduino to Arduino communication. The techniques are exactly the same for any UART to UART device. For example, in Arduino to Arduino by Bluetooth I use exactly the same serial communication techniques wirelessly over Bluetooth.
In the previous post I went through the basics of using serial on an Arduino and ran through the different commands. In this post I want to talk about different types of serial data and some of the things you should consider before starting to create code. The type of communication you use or can use will depend largely on the project but there are things that can be considered before starting.
- Type of communication? 1-way or 2-way
- Type of data? Values or strings? Simple or complex?
- How much data and how frequent? A couple of values every few seconds or a high rate continuous stream.
- Is the data critical? Must you be sure you receive all the data or can you afford to lose some of it.