setup()function, and turn off the transmitter.
intstands for integer (whole number). Photon pin numbers are always treated as
intvalues (even though they have letters).
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.
D3. If necessary, modify these values to match the I/O pins that your sensor pins are connected to.
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
ECHO_PINare listed. If necessary, change these to match the variable names for your sensor's pins.
OUTPUTfor the sensor's transmitter pin and
INPUTfor 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_PINis listed. If necessary, change this to match the variable name for your sensor's transmitter pin.
LOW. Your Photon uses this value to send an electrical signal through the pin:
HIGHis a signal of 3.3 volts which represents "on," while
LOWis a signal of 0 volts which represents "off." In this case,
LOWis 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
floatvalue (decimal number).
measureDistance()function after the
TRIG_PIN) and receiver (
ECHO_PIN), then be sure to modify this function to use those variable names instead.
returnstatement towards the end of the function.
loop()function (or within another custom function):
sensorDistis declared that will have a data type of
float(decimal number). This variable will be made equal to the value returned by the
sensorDist. It's common to use an
ifstatement (or an if-else statement) to perform certain actions based on whether the distance is less than (or greater than) a specific value.
sensorDiston the OLED screen to verify the sensor is working accurately.