Data Sheet
63 : circuit 3b
CODE TO NOTE
FLOAT VARIABLES:
float echoTime;
The float variable, short for floating-point
number, is similar to an integer except it can
represent numbers that contain a decimal point.
Floats are good for representing values that need
to be more precise than an integer. Floats allow us
to measure precise distances such as 9.33 inches
instead of just 9 inches.
ELSE IF STATEMENT:
if(logic statement){
//run if first is true
}
else if(second logic
statement){
//run if second is true
}
else{
//run if neither is true
}
Else if statements let you combine more than
one logic statement. Arduino will test each logic
statement in order; if one is true it will run the code
in that section and then skip all of the other sections
of code in the remaining statements.
USER-DEFINED FUNCTION:
getDistance();
This function tells the distance sensor to send out
an ultrasonic wave form, measures the time it takes
to bounce back to the sensor, and then calculates
the distance based on the speed of sound. This
calculation is based off information found in the
distance sensor’s datasheet.
CODING CHALLENGES
CHANGE THE LIMITS OF THE DISTANCE SENSOR: Try editing the values in the
logic statements so that the RGB LED changes color at different distances.
CHANGE THE UNITS OF THE DISTANCE SENSOR: Try editing the code so that the
distance sensor outputs a different unit of length, such as centimeters or feet.
ADD A FOURTH COLOR: Try adding another else if statement so that there are four
different colors instead of three.
SIK v4 Book Oct 13.indb 63 10/18/17 10:01 AM