Problem while controlling sensor+servo motor+soil moisture sensor

Guys I need help plz…help me actually I m creating one project and in that I m using moisture sensor+ir sensor+servo motor.
I need help regarding code like I want to control servo motor with moisture sensor ,if any dry object will come in contact with moisture sensor then ir sensor have to detect it and rotate the sevo motor left and if any wet object will come in contact with moisture sensor then ir sensor have to detect it and rotate the servo motor right side. I wrote one code but in that code my servo motor is rotating only right side when any wet object is coming in contact.so plz… help me for modifying the code Or plz…send the correct code so that it will work for both condition left(dry) and right (wet)

#include <Servo.h>
Servo servo;
int moisture_sensor = D2;
int servo_motor =16;
int ir_sensor=5;
int val;
void setup(){
servo.attach( servo_motor );
 pinMode(moisture_sensor,INPUT); 
 pinMode(ir_sensor,INPUT); 
}
void loop(){
  val=digitalRead(moisture_sensor );
  if (digitalRead(ir_sensor)==LOW && val==0)
  {
servo.write(0);
  }
  else if (digitalRead(ir_sensor)==HIGH && val==1)
  {
servo.write(180);
    }
else
{
servo.write(0);
}
}

Please edit your post and add triple backticks ( ``` ) before and after your sketch.

1 Like

Ok thanks for replying

Is it correct now

Can u plz… provide me the correct code for that

No, it is not.

Copy and paste the triple backticks that @John93 provided you with, on a separate line before and after your code.

Pete.

1 Like

Now check sir I corrected

Now you must read this
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

1 Like

You’re using an arduino sketch, it’s not gonna work with blynk unless you modify it to work with blynk

1 Like

Ok buddy thanks a lot for the help :pray::innocent:

1 Like

Just search the forum and you will find a lots of help

1 Like