These #defines just help keep track.Ĭonst int pumpRelayPin1 = 12 // Pin numbers for the pump relaysĬonst int floatSensorPin1 = 7 // Pin numbers for the float sensorsĬonst int pumpDuration = 500 // Number of millisecs that the pumps run #define RELAY_OFF 1 // and off when HIGH (1). #define RELAY_ON 0 // The pump relays are active-LOW on when LOW (0) -Edit these if you need to change the pin layout or pump duration How can I eliminate the code duplication? I believe I could use arrays and for loops but now I've entered another grey area for my knowledge level. I don't like that I've repeated so much code in the readFloatSensor functions, but this is where I am so far. ![]() ![]() I may not need to touch this code again for years and years (or a new house owner may need to look at it should I move out), so I tried to comment it as much as possible. Using info from every suggestion, I've so far come up with the following. (set timer to 300, and delay to 100ms) for better result. You might use unsigned chars to preserve extra RAM.Using interrupts should make it even more efficient. It's not the neatest thing, but it will do. Which might also be for the sensor inputs. Personally, I think, handling the timing events in an interrupt is way better. You might use something like this: #define AMOUNT_OF_TIMERS 10 ![]() Please note that the Scheduler requires yield() or delay() to switch between the tasks. Scheduler.start(setupController, loopController) While (digitalRead(sensorPin) = LOW) delay(DEBOUNCE_TIMEOUT) Belows is the logic for a single sensor-relay pair with a state trace to serial: #include Ĭonst uint32_t TURN_OFF_TIMEOUT = 30000L Įxpanding to three sensor-relays becomes: #include Ĭonst int sensorPin = The Timemark library provides a solution. The "blink-without-delay"-pattern shows how this can be solved but with several time periods and logic this becomes complex.
0 Comments
Leave a Reply. |