Piece of code only works if I perform Serial.print? LDR


I’m facing some strange issues on my code for my WiFi lamp.
There are two issues which are really hard to solve, both are related to the LDR sensor, which is responsible for the auto-on/off function.

I’ve attached my code since it is to large.
Basically it is a RGB lamp which runs on a battery. The lamp is controllable through UDP and that all works very cool. There is a LDR integrated to see if the light should go on or not.
This basically create’s a auto on/off.

So first of all, I’ve added two booleans for debug.
This gives me more specific info in the Serial monitor, and is mentioned to be used for configuring or ofcourse debugging.
Why two, well one shows the sensor value of the LDR, which is useful, but only when necessary.

Problem 1
In top of my code you can find ‘SET DEBUG’.
The second of these (bool debugLvl2) is the one that shows the sensor values to the monitor.
If I set this boolean to true the lamp works quite good (only problem 2 is still there).
If I set this boolean to false (which I prefer) then the lamp does not longer accepts UDP packets somehow? So the lamp won’t work any longer.
The strange thing is that the void debugLightSensor really only prints values…?? So it seems to be it has to do something with Serial.print??? So odd…

Problem 2
Okay, so problem 1 is the biggest ‘issue’, it’s not a disaster, but I rather have a clean monitor when done.
However, if it works then I have the second issue… If the LDR measures darkness, it should measure it for a while to make sure it is nature that is getting darker.

There is the void checkSensorDark() and void checkSensorLight() for.

The code for checkSensorDark() works 9 out 10 times.
Currently it is set to 30seconds for testing purpose, if it is dark for 30 seconds, the light will turn on.

However, the for checkSensorLight() does not work…
As soon as the LDR detects a value above 550 the light will turn on, and the lamp will says it is light outside…
It should actually measure if it is light for 30 seconds, then turn the light off.

Also above the void checkSensorDark() you see some code disabled, it has to do with the fact I was testing some different things.

Please find my code attached, happy to hear any advice.

Terraslampv7.ino (22.4 KB)

Maybe there's a timing issue. I see that your debug code has a delay in it. Maybe that gives the system a chance to get a UDP packet. Could it be that without it you're spamming outgoing UDP packets and overwhelming the network?

Too much code to wade through to be sure of that though.

Yes, sorry, actually had to delete the delay before sharing code since when I disable the delay, the issue won't change.

So the issue remains the same without using delay (sorry for that, for debug I do use a delay since I want to see the sensorvalues in a proper way).

I only use delay in my setup, the rest is millis, all unsigned long