# Duration in Scheduler

**URL:** <https://forum.arduino.cc/t/duration-in-scheduler/1317567>\
**Category:** Cloud IoT\
**Created:** [November 1, 2024, 5:19pm UTC](https://forum.arduino.cc/t/duration-in-scheduler/1317567 "2024-11-01T17:19:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![blazejskokowski](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blazejskokowski/32/1117186_2.png) [@blazejskokowski](https://forum.arduino.cc/u/blazejskokowski)\
**Post date:** [November 1, 2024, 5:19pm UTC](https://forum.arduino.cc/t/duration-in-scheduler/1317567/1 "2024-11-01T17:19:10Z")

</div>

Hello, I have a problem where I have a pump connected to a relay and a soil moisture sensor. I want the program to work so that when I enter the day and time and the soil moisture is below a certain level, the pump turns on for, say, 10 seconds. However, the 'duration' option in the widget doesn’t work at all – sometimes it lasts for a second, sometimes 10, with different times. Also, the pump does not turn off when the desired moisture level is reached; it just keeps running. Here’s a piece of the code responsible for this. What do you think?

```arduino
// Setting the soil moisture threshold to activate pump no.1 and checking if automatic mode is selected
if (modeSelection == 1) {
    if (schedule.isActive()) {
        if (sensorMoistureVariable1 < soilMoistureSensor1) {
            digitalWrite(Pump1, HIGH); // pump 1 turned on
            pumpStatus1 = true;
        } else {
            digitalWrite(Pump1, LOW); // pump 1 turned off
            pumpStatus1 = false;
        }
    }
} else {
    Serial.println("Select automatic mode!");
}

```

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [November 2, 2024, 5:09am UTC](https://forum.arduino.cc/t/duration-in-scheduler/1317567/2 "2024-11-02T05:09:06Z")

</div>

> [@blazejskokowski](#):
>
> 'duration' option

Can you show the "duration" option? Can you show your complete sketch?

> [@blazejskokowski](#):
>
> sometimes it lasts for a second, sometimes 10, with different times.

Probably this is due to not having a "hysteresis" in the code... The small bit of code you showed, the pump turns on if the sensorMoistureVariable1 is less than some other confusing named variable with "sensor" in it. When these two values are very close, the slightest change will cause the pump to turn on or off... for 10 seconds or one second.

You will want the pump to turn on when the condition is met, but off only when the condition is lower than normal. Using numbers...

(consider "50" to be "normal"... where your pumps are now turning on/off/on/off)

if (moisture \> 50) pump ON (turn on when needed)  
if (moisture \< 40) pump OFF (lower than normal

---

<div class="post-metadata">

**Author:** ![blazejskokowski](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blazejskokowski/32/1117186_2.png) [@blazejskokowski](https://forum.arduino.cc/u/blazejskokowski)\
**Post date:** [November 2, 2024, 9:48am UTC](https://forum.arduino.cc/t/duration-in-scheduler/1317567/3 "2024-11-02T09:48:54Z")

</div>

"Duration" is not a variable I introduced in the code; it’s part of the `schedule.isActive` function because this is a widget from the schedule. However, the "duration" option in the schedule does not work properly. As for the conditions, I want the pump to turn on only when two conditions are met: when the schedule is active and when the moisture level is below a certain threshold. In the current situation, however, the pump does not turn off when the moisture level is reached, precisely due to this schedule.

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [November 2, 2024, 12:54pm UTC](https://forum.arduino.cc/t/duration-in-scheduler/1317567/4 "2024-11-02T12:54:50Z")

</div>

> [@blazejskokowski](#):
>
> schedule is active and when the moisture level is below a certain threshold

```arduino
if (moisture < threshold && schedule == active) pumpON();

```

> [@blazejskokowski](#):
>
> however, the pump does not turn off when the moisture level is reached, precisely due to this schedule

Only you know.

> [@xfpd](#):
>
> show your complete sketch

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 1, 2025, 12:55pm UTC](https://forum.arduino.cc/t/duration-in-scheduler/1317567/5 "2025-05-01T12:55:45Z")

</div>

Closed on May 1, 2025, 12:55pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
