Arduino Forum

Using Arduino => Sensors => Topic started by: White_Lady on Mar 07, 2019, 08:03 pm

Title: Water plant help
Post by: White_Lady on Mar 07, 2019, 08:03 pm
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.
Title: Re: Water plant help
Post by: White_Lady on Mar 07, 2019, 08:09 pm
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);

    }






  }
}
Title: Re: Water plant help
Post by: wvmarle on Mar 08, 2019, 05:18 am
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:

Code: [Select]


  } else {

    (humidityReal > 50); {


You probably meant:

Code: [Select]

  }
  else if (humidityReal > 50) {


(note how that ; has also gone)
Title: Re: Water plant help
Post by: White_Lady on Mar 11, 2019, 02:22 am
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.



Code: [Select]
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);
  }
}
Title: Re: Water plant help
Post by: White_Lady on Mar 11, 2019, 02:38 am
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. :(
Title: Re: Water plant help
Post by: wvmarle on Mar 11, 2019, 04:21 am
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.
Title: Re: Water plant help
Post by: White_Lady on Mar 11, 2019, 04:33 am
Thanks for the reply. Can you provide me a sample?
Title: Re: Water plant help
Post by: wvmarle on Mar 11, 2019, 05:11 am
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():

Code: [Select]

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.
}
Title: Re: Water plant help
Post by: White_Lady on Mar 11, 2019, 04:45 pm
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.


Code: [Select]
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);
  }

}












Title: Re: Water plant help
Post by: White_Lady on Mar 11, 2019, 05:22 pm
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.


Code: [Select]
if (Serial.read() == 'A' && humidityReal > 50)
  {

    digitalWrite(waterPump2, HIGH);


  }
  if (humidityReal == 50) {

    digitalWrite(waterPump2, LOW);
  }
Title: Re: Water plant help
Post by: wvmarle on Mar 11, 2019, 05:23 pm
Please do an autoformat on your code and remove all those extraneous whitelines before posting. It's too hard to read like this.
Title: Re: Water plant help
Post by: White_Lady on Mar 11, 2019, 05:52 pm
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.

Code: [Select]
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);
  }

}
Title: Re: Water plant help
Post by: wvmarle on Mar 11, 2019, 06:03 pm
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...
Title: Re: Water plant help
Post by: White_Lady on Mar 11, 2019, 07:19 pm
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.
Title: Re: Water plant help
Post by: White_Lady on Mar 11, 2019, 07:29 pm
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.
Title: Re: Water plant help
Post by: wvmarle on Mar 12, 2019, 02:09 am
Simple solution as given above.
If that doesn't work your implementation is still wrong.