0

Hey guys i have made an automatic plant watering system. Its really basic. the code is:-

char yo = 0;
void setup() {
  pinMode(2, INPUT);//moisture sensor
  pinMode(3, OUTPUT);// pump
  Serial.begin(9600);
}

void loop() {
  yo = digitalRead(2);
  if(yo == 1){
      digitalWrite(3,HIGH);
      Serial.println("Plant is Being Watered as the soil is dry");
      delay(5000);
    }
  else{
      digitalWrite(3,LOW);
      delay(5000);
      Serial.println("the plant has sufficient water");
    }
}

The thing that i want to add is.. i want to add a feature which can manually control it with bluetooth but the normal program should also be running. for eg. soil is wet but i want to add more water so i write 9 on the blueterm app which starts the pump for 2 seconds. I am using an HC-05 bluetooth module. can someone tell me how this can work on an arduino?

EDIT!
Sketch 2:

char ha;
void setup() {
  pinMode(3, OUTPUT); //pump
  Serial.begin(9600);
}

void loop() {
 ha = Serial.read();
 if(ha == 5){
    digitalWrite(3, HIGH);
    delay(2000);
  }
 else{
    digitalWrite(3,LOW);
  }
}
  • How far have you got? Are you able to send data from the app and receive it in your sketch? – Mark Smith Dec 14 '16 at 7:54
  • well i have completed both in 2 different sketches that is i can control it manually(send and recieve data from app) in one sketch and run the normal program in the other. when i tried to merge them only the normal program worked and when i entered one in serial nothing happens. @MarkSmith – Dhruva Dec 14 '16 at 7:57
  • Could you add the second sketch to your question, please? – Mark Smith Dec 14 '16 at 7:58
  • There @MarkSmith – Dhruva Dec 14 '16 at 8:02
  • You should learn about finite state machines for this task. – Majenko Dec 14 '16 at 10:52
1
char ha;
void setup() {
  pinMode(3, OUTPUT); //pump
  Serial.begin(9600);
}

void loop() 
{

  //.:.bluetooth control block
  if (Serial.available()) //check serial data availability
  {
    ha = Serial.read();
    if(ha == '9') //received data is char '9'
    {  
      digitalWrite(3, HIGH); //turn on
      delay(2000);
      digitalWrite(3, LOW); //turn off
    }
  } 
  else
  {
    //.:.manual control block
    if(digitalRead(2)){
      digitalWrite(3,HIGH);
      Serial.println("Plant is Being Watered as the soil is dry");
      delay(5000);
      digitalWrite(3,LOW);
    }
  }
}
  • this wont allow the normal program to run(the automatic detection thing)i wanted both to run at the – Dhruva Dec 14 '16 at 8:09
  • @Dhruva you can simply add your former program after it. I edit my answer – dhimaspw Dec 14 '16 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.