Goal: To incorporate the light sensor into the circuit so that the motor turns one way when the light is detected and another way when no light is detected.
I wired the light sensor to the Analog port using this diagram to help me. One side of the LED connects to live and the other side connects, to analogue but through a resistor that is grounded.
This is the main loop of code. It makes use of two functions fwd() and rev() that I wrote in an earlier session. The variable ‘sensor’ takes a reading from the analog port.
The last line of this loop prints the value of sensor to the serial monitor, I added this in because I wanted to see the values that shining a torch on the light sensor would throw out. What I found here was unusual…
The serial monitor displayed lines of 1’s regardless of whether I exposed the light sensor to any light and, as a result the motor only rotated forward.
I looked online for other examples of serial monitor prints from light sensor. They were returning numbers up to 3 digits long, often in the 700 – 900 range. Something is going wrong somewhere.