EasyManua.ls Logo

Arduino NG

Arduino NG
311 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
Serial Communication Using Various Languages
When working with the Arduino, you often have to talk to it using a serial
port. In this section, youll learn how to do that in various programming lan-
guages. For demonstration purposes, well 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) {
Serial.print(pin_name);
Serial.print(": ");
Serial.println(analogRead(pin));
} else {
Serial.print("Unknown pin: ");
Serial.println(pin);
}
} else {
Serial.print("Unknown pin name: ");
Serial.println(pin_name);
}
pin_name = "";
input_available = false;
}
}
void serialEvent() {
while (Serial.available()) {
const char c = Serial.read();
if (c == '\n')
input_available = true;
else
pin_name += c;
}
}
report erratum discuss
Serial Communication Using Various Languages 255
www.it-ebooks.info

Table of Contents

Related product manuals