How to keep a pin "high" for a certain number of seconds

I am running a small 12V pump with a digital pin with a transistor and diodes. I need this pump to run for about 60 seconds. How can I format this in my code. This is what I have now and it comes on for about 3-4 seconds and then cuts off.

if (moisture < 2500) {
    digitalWrite(pump, HIGH);
    delay(1500);
    System.sleep(SLEEP_MODE_DEEP,30);
    }
else if (moisture >= 2500 ){
    System.sleep(SLEEP_MODE_DEEP,30);
   }

A simple way is to change delay(1500) to delay(60000) but that’s not a good way to introduce long delays.

This code snippet is a little too short. Are you controlling the pump elsewhere in code?

Depending on what you want the device to do, other than running the pump, making the change proposed by Kenneth would work. Alternatively, have a look into non-blocking delays or software timers :slight_smile:

1 Like

+1 For using non blocking delays and flags