[SOLVED] Relay not turning off when delay() set to more than 32000ms

I am making a plant watering system that turns on a relay for a period of time if certain conditions are met, then delays for a duration, then turns the relay back off. The timing works fine if the delay is set to 32 seconds or below, but as soon as I set it to 33 seconds (33000ms) or above, it doesn't turn off at all. Any ideas? Here is the code:

github gist

  • 2nPlusOne

This line? const int wateringTime = 2000; // an int rolls over at 32767 or ~33 seconds

Change to: const unsigned int wateringTime = xxxx; // rolls over at 65535 or ~66 seconds

Code should have used millis() for timing. Leo..

This is clearly a case where the additional 2 byte for a uint32_t are worth it.

Especially when going for an esp8266/esp32 to have online monitoring.

Wawa: This line? const int wateringTime = 2000; // an int rolls over at 32767 or ~33 seconds

Change to: const unsigned int wateringTime = xxxx; // rolls over at 65535 or ~66 seconds

Code should have used millis() for timing. Leo..

Ah! I had overlooked the max value of 2-bit integers! Thank you both! I ended up using uint32_t, instead of a millis() difference paired with a #define DURATION. IIRC, #define executes some extra stuff during compilation that assigned the variable whatever datatype is appropriate, correct?

-2nPlusOne

define should be left out. It's C++ and 2018 ;) Just use the right type aka think about what you do. ;) Only use it if you want to do compiler trickery.

2nPlusOne: the max value of 2-bit integers!

That would be 3... ;)

septillion:

define should be left out. It's C++ and 2018 ;) Just use the right type aka think about what you do. ;) Only use it if you want to do compiler trickery.

Code changed: all instances of #define successfully eradicated.