Two different millis() sessions

Hi,

I am trying to put together a little project. I am using a nano board to read values from a water level sensor and to control a water pump if the value is to high. I am using it to pump water out from a container behind the fridge/freezer. Two 18650 batteries will power the board and I will have a 5v voltage regulator for the pump along an npn transistor to act as a switch. As you can tell from my code I am a beginner with Arduino and I`ve looked on the web for few days before coming here. This is my code which works 'fine' to some point.

int pump = 8;
unsigned long previousMillis1 = 0;
const long read_interval = 3000;   
unsigned long previousMillis2 = 0;
const long pump_run_interval = 5000; 
void setup(){
  pinMode(pump, OUTPUT);
  Serial.begin(9600);
}
void loop(){
  unsigned long currentMillis = millis();
  
 if (currentMillis - previousMillis1 >= read_interval){
     previousMillis1 = currentMillis;
    int level = analogRead(A0); // read input value 
    Serial.print("Pin Value   ");
    Serial.println(level);  
  if (level > 600 ){
    digitalWrite(pump, HIGH);}
   if (currentMillis - previousMillis2 >= pump_run_interval){     
       previousMillis2 = currentMillis;
       digitalWrite(pump, LOW);}
       
    }
 }

Everything works fine until I change the interval time. If the read_interval is higher than the pump_run_interval the pump does not start anymore. The interval time I am using here is just for testing purpose. The real world intervals will be about 10min for read_interval and I have to perform some tests to find out how long it takes to empty the container for the pump_run_interval.

Any help will be appreciated.
Thanks

You have the code for your pump timing inside the IF code for your read timing. They need to be separate.

...R

This might help too:
Demonstration code for several things at the same time

Thank you for your help. I did separate the if conditions but still no good. Also Ive read the other linkand did try few things from there but somehow I cant make it work.

Well, when you're ready to work on it post the new version and we can take a look.

Be sure to describe the difference between what it is doing and what you want it to do. "It's not working" is the single most useless thing you could possibly ever say in the debugging process.

Why are you using setting a read interval time?

Why not just let it run as normal and the loop go full blast?

I assume it is to slow down the sensor feedback so it’s more stable.

I would normally not use delay in most of my code because it causes problems but if you have literally a a single pump and a single sensor a delay will not have any negative effects in your case

It seems that you do not have to take a reading if the pump is running. You could add a flag that is set when the pump is turned on and cleared when it is turned off. If the flag is set there is no need to read the sensor since the pump is running.

This has the side effect of not always taking readings exactly read_interval milliseconds apart if the pump is running, I don’t know if that’s important.

int pump = 8;
unsigned long previousMillis1 = 0;
const long read_interval = 3000;
unsigned long previousMillis2 = 0;
const long pump_run_interval = 5000;

bool pumpRunning = false;  // true => pump is running

void setup() {
  pinMode(pump, OUTPUT);
  Serial.begin(9600);
}
void loop() {
  unsigned long currentMillis = millis();

  // if pump is not runnig and it's time to take a reading
  if ( !pumpRunning && (currentMillis - previousMillis1 >= read_interval)) {
    previousMillis1 = currentMillis;
    int level = analogRead(A0); // read input value
    Serial.print("Pin Value   ");
    Serial.println(level);

    // start pump
    if (level > 600 ) {
      pumpRunning = true;
      previousMillis2 = millis();
      digitalWrite(pump, HIGH);
    }

  }

  // if pump is runnig and it's time to turn if off
  if (pumpRunning && (currentMillis - previousMillis2 >= pump_run_interval)) {
    digitalWrite(pump, LOW);
    pumpRunning = false;
  }

}

Hey everyone, i appreciate your help, 10 minutes of your time saved me one or two days of searching and trying. I know now where i was wrong, i had problems nesting my if statements and also I should have chek the state of the pump before teling it to do something.
JayMan you're right i could let the loop to check the level every time it's starts but i thought is going to do it to fast and i don't know if that is good or bad.
Blue Eyes thank you, your code works perfect.