Water plant help

Hi, im new to the arduino. Im working on plant water and would like to add an off on control to activate the reading value of the sensor. Control is by sending serial. If I type send A, sensor value reads if its higher than 50 then relay off then then when lower and disable reading function. Thank you very much.

soil.ino (741 Bytes)

also I would like to replace serialread to readstring. I will send string from android phone tru hc05.

here is the code so far.

int waterPump = 2;

String readString;
void setup() {
Serial.begin(9600);
pinMode(waterPump, OUTPUT);

digitalWrite(waterPump, HIGH);
}

void loop() {

while (Serial.available()) {
delay(100);//delay added to make thing stable
char c = Serial.read();
readString += c;

}

int humidityRaw = analogRead(A0); // 1023 to 0 ===> 0 to 100%
int humidityReal = map(humidityRaw, 1023, 0, 0, 100);

Serial.println(humidityReal);

if (Serial.read() == 'A' && humidityReal < 50)
{

digitalWrite(waterPump, HIGH);

} else {

(humidityReal > 50); {

digitalWrite(waterPump, LOW);

}

}
}

White_Lady:
here is the code so far.

Please do autoformat your code (ctrl-T in the IDE) and use code tags when posting.

This doesn't make sense:

  } else {

    (humidityReal > 50); {

You probably meant:

  } 
  else if (humidityReal > 50) {

(note how that ; has also gone)

Hi, thanks for the reply. notice the bottom code. I use serial read 'L' and 'P' to manual turn on and off the relay but it keeps on interrupting to sensor when value change. How can I isolate it from the sensor? Im sorry for being noob.

int waterPump = 2;
int waterPump2 = 3;




String readString;
void setup() {
  Serial.begin(9600);
  pinMode(waterPump, OUTPUT);
  pinMode(waterPump2, OUTPUT);

  digitalWrite(waterPump, LOW);
  digitalWrite(waterPump2, LOW);
}

void loop() {

  while (Serial.available() > 0)
  {
    delay(100);//delay added to make thing stable
    char c = Serial.read();
    readString += c;

  }

  int humidityRaw = analogRead(A0); // 1023 to 0 ===> 0 to 100%
  int humidityReal = map(humidityRaw, 1023, 0, 0, 100);

  Serial.println(humidityReal);


  if (Serial.read() == '1' && humidityReal < 50)
  {


    digitalWrite(waterPump, HIGH);

  }

  else if (humidityReal == 50) {

    digitalWrite(waterPump, LOW);
    digitalWrite(waterPump2, LOW);

  }


  else if (Serial.read() == '1' && humidityReal > 50)
  {

    digitalWrite(waterPump2, HIGH);


  }


  if (Serial.read() == 'L') {

    digitalWrite(waterPump2, HIGH);
  }


  if (Serial.read() == 'P') {

    digitalWrite(waterPump2, LOW);
  }
}

Here is what tthe function that I need. When I send serial '1' pump1 will turn on and if the sensor value <50. pump1 will turn on and tun off when ==50. pump 2 will turn on if reads >50 and turn off at ==50. I need to function only when I send '1'.
serial send 'L' and 'P' will manual turn on and off pump2 without sensor value interfere. Please help. :frowning:

You're going to need a simple state machine. Set it to a "manual" state upon receiving the L or P command (do make it so you can also send l or p instead), then to "auto" state upon receiving a "1".

Furthermore don't switch at humidity ==50 as there's a good chance you never see this, instead just go straight down to 49 or up to 51. Set it to >= 50 or <= 50.

Thanks for the reply. Can you provide me a sample?

For the state machine? Well, quite simple, just set flag to indicate whether you're in manual or automatic mode and test for it. Something like this in loop():

if (c == 'L' || c == 'l' ) {
  manual = true;
  digitalWrite(waterPump2, HIGH);
}
else if (c == 'P' || c == 'p') {
  manual = true;
  digitalWrite(waterPump2, LOW);
}
else if (c == '1') {
  manual = false)
}
if (manual == false) {
  // do automatic stuff.
}

I still cant make the code work. When manual on the pump read 'L'. Pump will turn on but sensor will
turn it off. I use potmeter to simulate sensor for test.

int waterPump = 2;
int waterPump2 = 3;

int manual;

String readString;
void setup() {
  Serial.begin(9600);
  pinMode(waterPump, OUTPUT);
  pinMode(waterPump2, OUTPUT);

  digitalWrite(waterPump, LOW);
  digitalWrite(waterPump2, LOW);



}

void loop() {

  char c;

  if (Serial.available() > 0)
  { c = Serial.read();
    readString += c;

  }
 
  int humidityRaw = analogRead(A0); // 1023 to 0 ===> 0 to 100%
  int humidityReal = map(humidityRaw, 1023, 0, 0, 100);

  Serial.println(humidityReal);

  if (c == 'L' || c == 'l' ) {
    manual = true;
    digitalWrite(waterPump2, HIGH);
  }
  
  else if (c == 'P' || c == 'p') {
    manual = true;
    digitalWrite(waterPump2, LOW);
  }
else if (c == '1') {
  manual = false;
}
if (manual == false) {
  // do automatic stuff



  if (Serial.read() == '1' && humidityReal < 50)
  


    digitalWrite(waterPump, HIGH);

  }

  if (humidityReal == 50) {

    digitalWrite(waterPump, LOW);


  }


  if (Serial.read() == 'A' && humidityReal > 50)
  {

    digitalWrite(waterPump2, HIGH);


  }
  if (humidityReal == 50) {

    digitalWrite(waterPump2, LOW);
  }

}

here is where my problem is. How can I seperate this code not to interfere with normal turn on and off pump. when I manual turn on pump, I need to not interfere with the humidityreal eguals to 50 and stays on until I send off. I need to seperate the code.

 if (Serial.read() == 'A' && humidityReal > 50)
  {

    digitalWrite(waterPump2, HIGH);


  }
  if (humidityReal == 50) {

    digitalWrite(waterPump2, LOW);
  }

Please do an autoformat on your code and remove all those extraneous whitelines before posting. It's too hard to read like this.

Here it is. The whole code. sorry for not being tidy. I just need to remove conflict between manual turn on/off and the sensor reading. I need to isolate.

int waterPump = 2;
int waterPump2 = 3;

int manual;

String readString;
void setup() {
  Serial.begin(9600);
  pinMode(waterPump, OUTPUT);
  pinMode(waterPump2, OUTPUT);
  digitalWrite(waterPump, LOW);
  digitalWrite(waterPump2, LOW);
}

void loop() {

  char c;

  if (Serial.available() > 0)
  { c = Serial.read();
    readString += c;

  }

  int humidityRaw = analogRead(A0); // 1023 to 0 ===> 0 to 100%
  int humidityReal = map(humidityRaw, 1023, 0, 0, 100);

  Serial.println(humidityReal);

  if (c == 'L' || c == 'l' ) {
    manual = true;
    digitalWrite(waterPump2, HIGH);
  }

  else if (c == 'P' || c == 'p') {
    manual = true;
    digitalWrite(waterPump2, LOW);
  }
  else if (c == '1') {
    manual = false;
  }
  if (manual == false) {
    // do automatic stuff

    if (Serial.read() == '1' && humidityReal < 50)
      digitalWrite(waterPump, HIGH);
  }
  if (humidityReal == 50) {
    digitalWrite(waterPump, LOW);
  }
  if (Serial.read() == 'A' && humidityReal > 50) {
    digitalWrite(waterPump2, HIGH);
  }
  if (humidityReal == 50) {
    digitalWrite(waterPump2, LOW);
  }

}

Now you formatted it, it's clear that the point where you switch off the pumps at exactly 50 humidity are outside the if (manual==false) block. That's probably your problem.

I hope you realise that if humidity rises fast enough and you go from 49 to 51 you won't ever switch it off... not a good idea if you're in automatic mode...

yes thats my problem. how can I isolate or set the sensor 50 humidity inside if. Sensor interferes the manual switch. I need to isolate it. I hope there is a solution to my problem.

sensor still turns the pump on and of at < > humidity and can override it turn on and off pump without reading the sensor. This is what exactly the function that I need.

If there is a simple sample for the solution. I will adpot it to my codes. that will be a big help.

Simple solution as given above.
If that doesn't work your implementation is still wrong.