Repeating if loop if executed

Not sure how to word the title really, sorry for that.

I'm working on code for watering the pot based on sensor reading and my idea is to have the main watering loop run every hour and inside that loop, I want to check if reading is in or out of limits. If it's inside limits, it should only run again in an hour, but if it's outside and watering is triggered (for 2 seconds), it should repeat after one minute and check if it's still outside. If it is, it should do the same thing, if it's now inside, it should run again only in an hour.

I'm using safestring library for repeating every hour, but I'm not sure how make that loop which does the watering.

#include <loopTimer.h>

int led = 13;
bool ledOn = false; // keep track of the led state
millisDelay readDelay;
millisDelay pumpDelay;

void setup() {
  Serial.begin(9600);
  for (int i = 10; i > 0; i--) {
    Serial.println(i);
    delay(500);
  }
  pinMode(pump, OUTPUT);

  readDelay.start(600000); // start the ledDelay, toggle every 1000mS
  pumpDelay.start(1200000); // start the printDelay, print every 5000mS
}

void readData() {
  if (readDelay.justFinished()) {   // check if delay has timed out
    readDelay.repeat(); // start delay again without drift

    //read moist and limit from db

  } // else nothing to do this call just return, quickly
}

void loop1() {
  if (pumpDelay.justFinished()) {
    pumpDelay.repeat(); // start delay again without drift

    //A:
    if (moist > limit) {
      waterstart = millis();
      while (millis() - waterstart <= 2000)
      {
        digitalWrite(pump, HIGH);
      }
      digitalWrite(pump, LOW);

      //wait one minute and then run again from point A
      }
      else {
        // run only in one hour
      }
    }

  } // else nothing to do this call just return, quickly
}

void loop() {
  loopTimer.check(Serial);
  readData(); // call the method to blink the led
  loop1(); // print the time
}

Edit:

If I put millis delay of one minute inside the if (moist > limit) loop and goto A after it, would that work?

Instead of thinking about inside/outside the loop, think about what needs to happen.

At this point, I have no idea what you want to happen.

.

Sorry for not being clear.

There's a relay controlling a water pump and a moisture sensor in a pot.

Moisture sensor is connected to a different microcontroller and data is being sent to a database.

The code I sent should also read that value from db (and also the value of moisture limit), but I left that out for simplicity, let's just assume we have both values available.

Every hour, moisture level should be compared to moisture limit. If the level is above the limit (analog values, 1024 means dry soil), then the pump should be activated for two seconds. After that, the program should wait one minute for water get absorbed in the soil and then compare the new moisture level to the limit. If it is still above it, it should again water for 2 seconds and again wait one minute. If moisture level is under the limit after watering, then no further watering should happen and the code should only be repeated after one hour.

Hope that makes it more clear.

It's clear. What is the point of the 1 hour wait?

It doesn't have to be 1 hour, I was thinking about 12 actually. The reason is that manual watering is done only every few days so there really isn't any need to check all the time.

You think it would work better if it was just the if (moist > limit) loop running every minute or so?

The reason is that manual watering is done only every few days so there really isn't any need to check all the time.

What else do you think the microcontroller is going to do while it's not doing the checks? It doesn't care, it can easily check 10000 times per second, it won't get bored and, unlike me, it won't wish it could go to the pub instead of checking if some plants might need water.

I don't know much about plants but I can't see any reason not to check often, what a human would do is irrelevant.

PerryBebbington:
it won't wish it could go to the pub instead of checking if some plants might need water.

:smiley:

I guess sensor longevity would be the only reason to check less often, but as I'm reading data more often for a different reason, there's really no point in doing that.

Thanks.

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