Hardware Reference
In-Depth Information
line 1. Remember, most numbers start with 0, so this is actually on the i rst
column on the second line. The HC-SR04 can give results up to 4 meters away;
values greater than that will be ignored. A quick check is done on line 49 with
an if() statement. If the result is greater than 400 centimeters or if the result is
negative, the sketch writes “Out of range” if the distance value is out of range.
If it is not out of range, the value is printed. This is done in two steps: i rst, the
decimal value is displayed. Afterward, some text is displayed with a leading
space and several spaces after the text. Why? Because if the previous text were
“Out of range,” the end of that text would still be visible. Writing text on a line
does not automatically delete all the text at the end of the line. Just like using
the insert function in a word processor, each keypress deletes one character and
inserts the character you want in that text, but it does not delete text afterward.
To make sure that no trailing text is displayed, several spaces are included.
The last thing that happens is waiting for one-half a second before repeating
the process. This is done on line 62. Figure 11-4 shows the i nished product.
Figure 11-4: The finished product
NOTE It is vitally important to double-check the Arduino before connecting com-
ponents. It is tempting to connect the sensor before uploading the sketch to the
Arduino, but what would happen if the previous sketch used the I/O pins for some-
thing else? In the worst case, the VCC and GND pins could be inverted, essentially
reversing the polarity of the component, damaging or destroying it. I have a dozen
Arduino boards at home, and it is impossible to remember exactly which board has
which sketch. Remember to upload the proper code of your Arduino before connect-
ing external devices.
Search WWH ::




Custom Search