Read The Arduino Inventor's Guide Online
Authors: Unknown
Now, the
setup()
part of the code sets up the button pin to use an internal pull-up resistor that’s built into the Arduino by declaring the pin mode as an
INPUT_PULLUP
➎
. This trick removes the need for the external pull-up resistor used in
Project 4
.
Next, the sketch initializes the servo motor and sets its default position to
0
. This will be the position of the starting gate when it’s down.
The sketch then displays a little information to the LCD
➏
to let the user know how to start the race. These few lines of code set up the LCD, clear the screen, and display two lines of text. Be careful that your text is limited to 16 characters per line; any more than
16, and your characters will run off the screen to the right. The code then waits for a button press using the blocking
while()
loop technique
➐
used in the Reaction Timer; this blocks the sketch from proceeding until the button is pressed. When the button is pressed,
digitalRead(buttonPin)
will read
LOW
and the code will move the servo to the up position and set the
startTime
variable.
In the
loop()
, the sketch reads the light sensor and stores its current reading to the variable
finishSensor1
➑
. The sensor will be embedded at the end of the ramp. The car will roll over it as it crosses the finish line, covering the sensor and blocking most of the light. Similar to the Night-Light sketch in
Project 5
, the sketch will compare the value of the sensor to the
darkThreshold
value.
NOTE
Your sensor needs to be in a decently lit area so that the contrast between the sensor being lit and being shaded is great enough to cause that drop in voltage. Be aware that overhead lights can create a false detection if your body casts a shadow over the sensor. If you want to make sure that the sensor works well, get a small desk lamp and set it over the sensor.
Remember that in the time it takes the car to pass the sensor, the
loop()
may repeat several times. Because we only want to look for the first moment the car crosses the finish line, the sketch uses a compound
if()
statement
➒
to capture the moment when the
finishFlag
variable is
false
and
the finish sensor is blocked (that is, its value is less than
darkThreshold
). The
&&
indicates a logical AND (see “
Compound Logic Operators
” on page
264
). Pay careful attention to the number of parentheses used in the
if()
statement—they indicate order of operation and how the logic is used.
Now, inside the
if()
statement, the
finishFlag
state variable switches to
true
. Because the
finishFlag
state variable is now set to
true
, the compound
if()
statement will only catch the first moment the car crosses the sensor.
The sketch then records the stopping time and calculates the elapsed race time. Finally, the sketch prints the race time to the LCD.
The
raceTime
variable is declared as a
float
(floating-point variable) so it can store numbers with decimals. By default, the
lcd.print()
method will display two decimal places of precision for a floating-point value, but you can add a second parameter to the
lcd.print()
method to specify more or less. At
➓
, the sketch calculates the number of seconds elapsed by dividing the millisecond count by 1,000. The extra
3
in the instruction
lcd.print(raceTime / 1000, 3);
tells Arduino to display three values past the decimal point, so the time will be accurate to the millisecond. Don’t forget the last two curly brackets in the code. Double-check to make sure that your code matches
Listing 9-2
, and upload the sketch to your device.
COMPOUND LOGIC OPERATORS
In Chapter 4, we introduced simple logical comparison operators to compare two values. Recall that logic comparisons or expressions can only be either true or false. In programming, there are times when you need to compare multiple conditions together; for example, when you need to run some code only when a variable is false AND a sensor value is less than the threshold:
((finishFlag == false) && (finishSensor1 < darkThreshold))
. Here, notice that the logic comparisons are grouped together in parentheses on either side of the compound AND (
&&
).
A combination of two or more logic comparisons is known as a
compound logic expression
. Expressions are
evaluated
(or read) from left to right. To keep everything together and observe the correct order of operations, it’s a good idea to use parentheses to separate out the individual expressions. The two main operators used to combine logic expressions are AND and OR, which are described in the following table.
SYMBOL | COMPOUND OPERATOR | DESCRIPTION |
( | AND | Both |
( | OR | Either |
If you have everything wired up correctly and the code uploaded successfully, you’ll hear the servo motor move to the 0 degree position and see a message displayed on the LCD, as shown in
Figure 9-13
. If the text is garbled or otherwise incorrect, double-check the wiring of the LCD, push button, and light sensor.
FIGURE 9-13:
LCD display text at the start of the race
Push the button and see what happens. The servo motor should move, and the display should change to the message “Go!” Now, cover the photoresistor with your finger. The LCD should display the time elapsed since you pressed the button and covered the photo-resistor (
Figure 9-14
).
With the electronics all working properly, it’s time to build the starting gate and track. If the sensor is not behaving as expected, try changing the
darkThreshold
value. If it’s too sensitive or triggering immediately, reduce the value of
darkThreshold
. If it’s not reacting when you cover up the sensor, try increasing the value. After you’ve made these changes, reupload your code and test it again.
FIGURE 9-14:
LCD display with time elapsed
The Drag Race Track includes a starting tower with a rotating gate that controls the release of the car onto the track. For the track, you can either use a section of a toy car race track or build your own from cardstock. The template for the tower is shown in
Figure 9-15
. You can download a PDF of this template from
https://www.nostarch.com/arduinoinventor/
.
FIGURE 9-15:
Template of cardboard cutout pieces for starting gate (not full size)
Carefully cut the template out from a sheet of cardboard (see
Figure 9-16
). The template has an opening for mounting the servo on one side and a hole on the other to mount the bamboo skewer axle for the starting gate. The other pieces are the support beams and the starting gate.
FIGURE 9-16:
Trace the template and carefully cut out the pieces using a sharp craft knife.
With the pieces cut out, first mount the servo in the opening, labeled in
Figure 9-15
. Insert the servo from the outside of the support beam so that the servo horn faces in toward the car. You can use the small screws included with the servo, or a small amount of glue, to secure the servo in place as shown in
Figure 9-17
. Don’t attach the servo horn just yet. You’ll attach that to the starting gate in the next step.
FIGURE 9-17:
Securing the servo using hot glue
Now, glue the two support beams in place. The lower support beam will insert into the slots cut into each of the side pieces. The top support beam should fit right into the notch on the top of each side piece. Use a small dab of glue to secure each of these pieces in place. When you’re done, you should have a starting support tower like the one in
Figure 9-18
.
FIGURE 9-18:
Adding the support beams