Been a while and I have forgotten to add the new things I am learning for my personal PD my portfolio.
Today I looked at sensors. In this lesson I looked at the ultrasonic sensor. This sensor emits a high frequency sound then waits for the echo of this sound. It measures time between the sending and receiving the echo. From this the distance to an object is calculated.
The sketch sends a digital low for two microseconds then a digital high for ten microseconds. pulseIn takes two parameters, the pin number in this case 11 and the pulse level (high). As soon as the high ping value is sent time, in microseconds, is recorded until the echo is received. The if statement prints an out of range message if the values are greater than or equal to 200 or if values are negative. Otherwise the distance is given in cm.