Serial Communication Using Various Languages

When working with the Arduino, you often have to talk to it using a serial port. In this section, you’ll learn how to do that in various programming languages. For demonstration purposes, we’ll use the same Arduino sketch for all of them:

SerialProgramming/AnalogReader/AnalogReader.ino
 
const​ ​unsigned​ ​int​ BAUD_RATE = 9600;
 
const​ ​unsigned​ ​int​ NUM_PINS = 6;
 
 
String pin_name = ​""​;
 
boolean input_available = false;
 
 
void​ setup() {
 
Serial.begin(BAUD_RATE);
 
}
 
 
void​ loop() {
 
if​ (input_available) {
 
if​ (pin_name.length() > 1 &&
 
(pin_name[0] == ​'a'​ || pin_name[0] == ​'A'​))
 
{
 
const​ ​unsigned​ ​int​ pin = pin_name.substring(1).toInt();
 
if​ (pin < NUM_PINS) {

Get Arduino: A Quick-Start Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.