Hardware Reference
In-Depth Information
Listing 5-1 ( continued )
35 //Print out the user's age
36 Serial.print("Oh, you are ");
37 Serial.print(age);
38 Serial.println("?");
39 Serial.print("I am ");
40 Serial.print(millis());
41 Serial.println(" microseconds old. Well, my sketch is.");
42
43 //Now print out the alphabet
44 Serial.println("I know my alphabet! Let me show you!");
45 Serial.println("Letter\tDec\tHex\t");
46 for (i = 'A'; i <= 'Z'; i++)
47 {
48 Serial.write(i);
49 Serial.print('\t');
50 Serial.print(i);
51 Serial.print('\t');
52 Serial.print(i, HEX);
53 Serial.print('\t');
54 Serial.print('\n');
55 }
56 }
57
58 void loop()
59 {
60 // put your main code here, to run repeatedly:
61 }
Lines 1 to 5 declare the global variables in the program. The myName variable is
declared and initialized with the name "Arduino" ; the others are only declared.
On line 7, setup() is declared. Because the code runs only once, all the code
in this example is placed in setup() . Even though there's nothing happening
in loop() , it still needs to be there.
On line 10, the serial device is initialized. The default serial port, Serial , con-
nects to pins 0 and on1e. On an Arduino Uno, these are connected to the USB
port. The speed is set to 9,600 baud, and no other parameters are set; therefore
the device defaults to 8 data bits, no parity, and 1 stop bit. On line 13, the Arduino
greets the user through println() . The program waits for 10 seconds and reads
the serial buffer with readBytes() . The data will be put into the userName vari-
able and read up to the size of the buffer, 64 bytes. I hope your name isn't longer
than 64 characters! Because it probably isn't, the function will read the bytes
in your name and then wait for 1 second to see if there are up to 64 characters.
After this, it returns what data it has.
On line 19, the sketch greets the user again, this time with her name. This is
done by printing some default text and then printing a variable, the user's name.
Search WWH ::




Custom Search