setup()
function, and turn off the transmitter.setup()
function:int
stands for integer (whole number). Photon pin numbers are always treated as int
values (even though they have letters).TRIG_PIN
(transmitter) and ECHO_PIN
(receiver). You can change the variable names, but choose names that will make sense to anyone reading the code. You will need to make sure the same variable names are listed in the custom function which measures the distance to the nearest object.D2
and D3
. If necessary, modify these values to match the I/O pins that your sensor pins are connected to.setup()
function:pinMode()
method requires two parameters inside its parentheses (in this order):D2
, etc.) or a variable that stores a pin number. In this example, the variables named TRIG_PIN
and ECHO_PIN
are listed. If necessary, change these to match the variable names for your sensor's pins.OUTPUT
for the sensor's transmitter pin and INPUT
for the sensor's receiver pin.digitalWrite()
method requires two parameters inside its parentheses (in this order):D2
, etc.) or a variable that stores a pin number. In this example, the variable named TRIG_PIN
is listed. If necessary, change this to match the variable name for your sensor's transmitter pin.HIGH
or LOW
. Your Photon uses this value to send an electrical signal through the pin: HIGH
is a signal of 3.3 volts which represents "on," while LOW
is a signal of 0 volts which represents "off." In this case, LOW
is being used to turn off the transmitter.measureDistance()
than can be called to measure the distance from the ultrasonic sensor to the nearest object. The function will return the distance as a float
value (decimal number).measureDistance()
function after the loop()
function:TRIG_PIN
) and receiver (ECHO_PIN
), then be sure to modify this function to use those variable names instead.return
statement towards the end of the function.measureDistance()
function.loop()
function (or within another custom function):sensorDist
is declared that will have a data type of float
(decimal number). This variable will be made equal to the value returned by the measureDistance()
function.sensorDist
. It's common to use an if
statement (or an if-else statement) to perform certain actions based on whether the distance is less than (or greater than) a specific value.sensorDist
on the OLED screen to verify the sensor is working accurately.loop()
function: