Auto watering system using 'multi if'

Hi, bros

recently i have made auto watering code for observation and
feel like I'm finally close to my goal.

Thank you very much for your help so far.

Components are

  1. solenoide valve connected with relay.
  2. soil moisture sensor.
  3. arduino uno r3.

and goal is

1.keep watering about 30 sec if sensor value rising suddenly
(coz soil moisure sensor located top of the soil)

so i think the multiple if will be the answer.


// AUTOMATIC IRRIGATION SYSTEM TESTED BY LED BUILT IN

int SMS = A0; // soil moisture sensor
unsigned long pretime = 0;
const long onvalve = 30000; 

//setup
void setup ()
{
  pinMode(relay, OUTPUT); 
  Serial.begin (9600); 
}

//loop
void loop ()
{
  float raw = analogRead(SMS);
  float present = map(raw, lowest, highest, 0, 100); // SET 0~100 %
  Serial.println(present);


  unsigned long currtime = millis(); 
  float to be = 40; // to be 40 %

  Serial.println(to be);
  delay(1000);

  {
    if (present <  to be)
    {
      digitalWrite(relay, HIGH);
    }

    else if ((present > to be)&&(currtime - pretime >= onvalve))
    {
      digitalWrite(relay, LOW);
    }
    else 
    {
      digitalWrite(relay, HIGH);
    }
  }
  currtime = pretime;
}

i think

if(); If soil moisture is below target, relay operation.

else if. Stop if soil moisture is above target and relay running time is more than 30 seconds.

else If it's neither of them.

It's not working right now.

Should I use boolean to make it work?

thx.

Hi,bros

im trying to make auto irrigation code with timer by timer.h

Components are

  1. solenoide valve connected with relay.
  2. soil moisture sensor.
  3. arduino uno r3.

the purpose is

1.keep watering about 30 sec if sensor value rising suddenly
(coz soil moisure sensor located top of the soil)

  1. code looks not bad but he acts like sick. wanna make him agile.
    (i mean, Increased soil moisture does not immediately work. )

the code is here (that is 2nd version)

#include <Timer.h> 
Timer t;

int SMS = A0; // soil moisture sensor
int realy = 13; // solenoide valve connected with relay.

//setup
void setup()
{
  Serial.begin (9600); 
  t.every(1000, watering); // delay replacement
  pinMode(relay, OUTPUT); 
}

//loop
void loop()
{
  t.update();
}

void watering()
{
  float raw = analogRead(SMS);
  // SMS Analog Input = raw
  float mappedraw = map(raw, lowest, highest, 0 , 100  ); // SET 0 TO 100 %

  Serial.print("mapped raw percnet = ");
  Serial.println(mappedraw);

  float expectedpercent = 40;

  Serial.print("expectedpercent = ");
  Serial.println(expectedpercent);

  if (mappedraw < expectedpercent)
  {
    t.pulse(relay, 20000, HIGH); // delay replacement and part of watering, but it just act 10sec
  }

}

plz give me some advices bros

thx

  • best wishes, minorkey

throw away the timer libary and make your own time management.

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

the compiler don´t like your sketch

That looks close but the timer library stuff is really hard for me to understand. Can you have more than one timer?

-jim lee

i try to make it

but it still slow.


int SMS = A0; 
unsigned long t2 = 0; 
const long delaytime = 30000; 

//setup
void setup ()
{
  pinMode(relay, OUTPUT); 
  Serial.begin (9600); 
}

//loop
void loop ()
{
  float raw = analogRead(SMS); 
  float rawmapped= map(raw, lowest, highest, 0, 100); 
  Serial.println(rawmapped);

  
  unsigned long t1 = millis(); 
  float expectedpercent = 40; 

  Serial.println(expectedpercent);
  delay(1000);

  if (t1 - t2 >= delaytime)
  {
    if (rawmapped < expectedpercent)
    {
      digitalWrite(relay, HIGH);
    }

    else
    {
      digitalWrite(relay, LOW);
    }
    t2 = t1;
  }
}

aha, do u mean one more f() ? triggerd by timer?

i ll try that

thx agian

I modified it.

thx

Hello
do you have a modified sketch already?

Why did you start a new topic for the same question ?

The 2 topics have been merged

@

TOPIC MERGED.

Could you take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum.

I didn't mean to.
I'm terribly sorry.

yes, millis(); with multi if will be the answer i think.

if u don't mind plz give me some feedback of above new code.

thx again.

I can take a look in my sketch box to find a similar task.

after adding definitions for relay,lowest and highest, the sketch will compile without errors.
I think the problem is, that you initialize t2 with 0.
putting
t2=millis();
in your setup port will help.

Thx, i will try it.

sorry, i'm afraid that wasn't the problem after all.
Let me get this straight:
You want to measure cyclically (cycle time?) and if the measured value is too low, you want to open your valve for 30 seconds.
Right?

yes,correct!

and also if measured value is higher than expectaion, wanna open valve for 30.

  1. value is low -> open valve for 30s
  2. value was low, but now is high -> open valve for 30s from when it was getting high.
  3. value was high, more than 30s high -> close valve.

latest version is here, but still act wrong.

int SMS = A0; //soil moisture sensor
int relay =13;
unsigned long pretime = 0;
unsigned long curtime = 0;
const long onvalve = 30000;

//setup
void setup ()
{
  pinMode(relay, OUTPUT); 
  Serial.begin (9600); 
  pretime = millis();
}

//loop
void loop ()
{
  float raw = analogRead(SMS); 
  float mappedraw = map(raw, 100, 200, 0, 100); // in AIR = 100, in WATER = 200, SET 0~100 %
  Serial.println(mappedraw);

  curtime = millis();

  float expectedpercent = 40;

  Serial.println(expectedpercent);
  delay(1000);


  {
    if (mappedraw < expectedpercent)
    {
      digitalWrite(relay, HIGH);
    }

    else if ((mappedraw> expectedpercent) && (curtime - pretime >= onvalve))
    {
      digitalWrite(relay, HIGH);
    }
    else
    {
      digitalWrite(relay, LOW);
    }
  }
  pretime = curtime;
}

i cant understand this
but anyway:
to open the valve for 30 Seconds you can write:

digitalWrite(relay, HIGH); // open valve
delay (30000);
digitalWrite(relay, LOW);  // close valve

If your project does not do anything else, you only have to measure in a loop and decide in an if statement whether the valve should be opened or not.

void loop ()
{
float raw = analogRead(SMS);
float rawmapped = map(raw, lowest, highest, 0, 100);
Serial.println(rawmapped);

Serial.println(expectedpercent);

if (rawmapped < expectedpercent)
{
digitalWrite(relay, HIGH); // open valve
delay (delaytime);
digitalWrite(relay, LOW); // close valve
}

delay(cycletime);
}