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:
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?
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.