I need to leave behind delay() and go on with millis() for multitasking

Greetings people, since im back to my project of home semi-automations i was wondering if anyone can help me pass the delay() with millis() to my pumps. Since i'm aiming for a safe automation, my main target is to keep the system under control, to help prevent disasters from newbie users.

Now im using an arduino uno, connected to a DHT11 hum/temp sensor, a 16ch 5v relay to switch devices etc. bellow is the code, but i haven't any luck while i was reading others posts, since i find it difficult to edit the code as im using the "case" to use serial commands for actions.

#include <dht.h>
#define DHT11_PIN 7
dht DHT;

int userStart = 0;

//define the pump pins and time
int p1 = A0; // pin that turns on the motor
int p2 = 2;
int p3 = 3;
int p4 = 4;
int p5 = 5;
int p6 = 6;
int p7 = A1;
int p8 = 8;
int p9 = 9;
int p10 = 10;
int p11 = 11;
int p12 = 12;
int p13 = 13;
int watertime = 10; // how long to water in seconds
int waittime = 60; // how long to wait between watering, in minutes

void setup() {
 
  pinMode(p1, OUTPUT);
  pinMode(p2, OUTPUT);
  pinMode(p3, OUTPUT);
  pinMode(p4, OUTPUT);
  pinMode(p5, OUTPUT);
  pinMode(p6, OUTPUT);
  pinMode(p7, OUTPUT);
  pinMode(p8, OUTPUT);
  pinMode(p9, OUTPUT);
  pinMode(p10, OUTPUT);
  pinMode(p11, OUTPUT);
  pinMode(p12, OUTPUT);
  pinMode(p13, OUTPUT);
                    
digitalWrite(p1, HIGH);
digitalWrite(p2, HIGH);
digitalWrite(p3, HIGH);
digitalWrite(p4, HIGH);
digitalWrite(p5, HIGH);
digitalWrite(p6, HIGH);
digitalWrite(p7, HIGH);
digitalWrite(p8, HIGH);
digitalWrite(p9, HIGH);
digitalWrite(p10, HIGH);
digitalWrite(p11, HIGH);
digitalWrite(p12, HIGH);
digitalWrite(p13, HIGH);
  // Setup serial plotter.
  Serial.begin(9600);
}


void loop() {
  if (Serial.available() > 0)  {
    switch(Serial.read())
        {
        case '1':
            
  Serial.print("pump 1 is ON");
  Serial.print('\n');
  digitalWrite(p1, LOW); // turn on the motor
  delay(watertime*1000);        // multiply by 1000 to translate seconds to milliseconds
  digitalWrite(p1, HIGH);  // turn off the motor 
  Serial.print('\n');
  Serial.print("pump 1 is OFF");
  break;
        case '2':
            Serial.flush();
  Serial.print("pump 2 is ON");
  digitalWrite(p2, LOW); // turn on the motor
  delay(watertime*1000);        // multiply by 1000 to translate seconds to milliseconds
  digitalWrite(p2, HIGH);  // turn off the motor 
  Serial.print('\n');
  Serial.print("pump 2 is OFF");
  break;
          case '3':
            Serial.flush();
  Serial.print("pump 3 is ON");
  digitalWrite(p3, LOW); // turn on the motor
  delay(watertime*1000);        // multiply by 1000 to translate seconds to milliseconds
  digitalWrite(p3, HIGH);  // turn off the motor 
  Serial.print('\n');
  Serial.print("pump 3 is OFF");
  break;
  }
 }

 //RH & temp
  int chk = DHT.read11(DHT11_PIN);
  Serial.print("Room Temp. c");
  Serial.println(DHT.temperature);
  Serial.print("RH %");
  Serial.println(DHT.humidity);
  delay(2000); 
           
}
  1. Format that mess. There's no excuse for not lining things up. The IDE will do it for you (Control - T). It will make it MUCH easier for us to read and for you to work on.

  2. Are you sure you know what this function does?

Serial.flush();

I think if you did you might have put it in a different place or not called it at all.

  1. Using millis for timing is as easy as using a clock. Can you use a clock? If I told you to call me in 10 minutes could you do that? It's the exact same idea. Note the time now and periodically check to see how much time has passed.

Give it a try. Show us some code with some millis in it. If it doesn't work that's OK we can help. But you gotta get it started.

You mean you want to replace 'delay' with a non-blocking code?

If so... then try defining a reference time variable -- eg. unsigned long ref_time = 0;

Then, in the setup() part of your code.... assign it.... eg... ref_time = millis();

Then.... delete your delay function completely. ie... remove the "delay(2000);"

And..... just at the beginning of your loop.... you could try....

if (millis() - ref_time >= 2000) {

\ put all the previous code from your original loop here ......

ref_time = millis(); //set new reference time
}

Southpark:
ref_time = micros(); //set new reference time

Probably meant ref_time = millis() there :slight_smile:

When you use numbers in your variable names, you should really start looking at arrays. Usually makes life a lot easier, and code more flexible and shorter.

blh64:
Probably meant ref_time = millis() there :slight_smile:

You're absolutely right about that! Thanks for picking that up! Better put the millis() !!
I amended the code now..... just in case.

Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

...R