Project information:
I want to use a proximity sensor that turns on a LED-strip when someone enters my driveway when the sun is down. Currently I'm testing with an Arduino nano but will switch to a NodeMCU V3 so I can easily integrate it into my HomeAssistant setup. A NodeMCU will also easily allow me to do API calls to get the sunrise and sunset (I think, haven't looked into it yet tbh).
I know this might not be the easiest/most logical setup, but it seemed fun to do it this way with all the different types of challenges
You changed the pin in your code. That's bad. Now @jim-p's comment looks crazy. But I saw it before you changed it and so can anyone else by using the "view changes" pencil icon.
Tried that as well, but to no avail. I'm starting to think it might be the sensor, since it also occasionally shows weird behaviour on the build-in LED.
The beam need to be blocked for it to work. If you use something reflective to block the beam you will probably still get a response. If it says 7M so the lenses are probably set up for that distance only.
It's a reflector similar to those on the rear of your car (assuming you have them in your country (reflectors, not cars, I mean)). The reflector's surface consists of a matrix of small corner-reflectors that bounce light back in the direction it came from.
Other shiny surfaces will reflect the light off at some angle, and miss the sensor, unless you align them very carefully.
@PaulRB@jim-p Testing some things rn, using a bigger distance and the reflector. The behaviour seems more consistent. Will keep you posted, but it can take a while (hard to test things this way)
The code you posted in the original post prints the output of the sensor on the serial output. Then you have a 500ms delay before you print again.
The sensor output could change, and then change back again during that 500ms and your code wouldn't detect it.
Have a look at the IDE Example 'StateChangeDetection' to see how you can Serial.print() only when the sensor output changes.