Hardware Reference
In-Depth Information
Example Program
For this example, you use an Arduino Uno. It connects via USB to your develop-
ment PC and is powered via USB. No power supply is needed, and there will
not be any components connected this time.
This program demonstrates the principles of a serial connection. The Arduino
welcomes the user, asks for her name, and then presents itself. It asks for the
user's age and then gives the age. Finally, it prints out a few ASCII characters
using tabs.
Listing 5-1: Serial Connection (Filename: Chapter5 . ino )
1 char myName[] = {"Arduino"};
2 char userName[64];
3 char userAge[32];
4 int age;
5 int i;
6
7 void setup()
8 {
9 // Configure the serial port:
10 Serial.begin(9600);
11
12 // Welcome the user
13 Serial.println("Hello! What is your name?");
14
15 //Wait for a few seconds, then read the serial buffer
16 delay(10000);
17 Serial.readBytes(userName, 64);
18
19 //Say hello to the user
20 Serial.print("Hello, ");
21 Serial.print(userName);
22 Serial.print(". My name is ");
23 Serial.print(myName);
24 Serial.print("\n");
25
26 //Ask for user's age
27 Serial.print("How old are you, ");
28 Serial.print(userName);
29 Serial.println("?");
30
31 //Wait for a few seconds, then read the serial buffer
32 delay(10000);
33 age = Serial.parseInt();
34
Continues
 
Search WWH ::




Custom Search