Problem while controlling ir_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);
}
}

Your post was MOVED to its current location as it is more suitable

1 Like

Can u plz... help me regarding my doubt

How is the system powered? What servo do you have that is specified to work on 3.3V?

Steve

1 Like

Can you verify that the servo works using the Servo library sweep example?

(sp. "you","please")

1 Like

We r using nodemcu for that , we r using servo motor sg90

I didn't get means

We r using Servo.h

The Servo library has a simple, worked example called "Sweep".
Can you modify it to use your servo pin, and run it, to prove (verify) that you can control the servo?

(sp. "are")

1 Like

Ok let me try once I will try it now

That I checked it is working fine but I want that servo motor should control through moisture sensor (like if moisture sensor sense dry object servo motor rotate left and for wet rotate right)

Your code has write(0) and write(180). Is one of those 'left' and the other 'right'?

The SG90 is not intended to run on 3.3V. If yours does sort of work on 3.3V it's pure luck and it will be very weak and slow.

Steve

1 Like

I m getting only one direction I need like 0 to 180 then 180 to 0 then 0 to -180
0 to 180 (use wet object)
After removing wet object come back to 0
0 to -180( use dry object)
For this I want code

If possible plz... solve my doubt urgent I have project presentation after 2 days

Why not change the logic so that when the IR detector sees that an object has become present (not is present) then the moisture detector is used to determine whether it is wet or dry and turns the servo in the appropriate direction ?

1 Like

Something I m doing wrong can u please provide me code

Why not draw the truth table for whatever it is you're trying to achieve?

1 Like

There is no such position as -180. That would be total movement of 360 degrees and an SG90 can only move 180degrees in total. For any servo position 0 is as far as it can go in one direction and 180 is as far as it can go in the other direction.

If you really want it to swing one way, go to a centre, then swing the opposite way you need to set the centre as 90. Then write(0) goes one way and write(180) goes the other way.

Steve

1 Like

Here is a code-version that has debug-output that shows the states of the inputs and variable angle
in the WOKWI-simluator

If your real setup behaves different there is a high change that you have an hardware-issue
best regards Stefan

1 Like

I did some corrections in code but this code is not working for dry object can u find out the mistake plz

#include <Servo.h>

class Smart_Segregator
{
  public:
     int servo_pin=16;
     int moisture_sensor_pin=A0; // connect to the analog pin A0 of the nodemcu
     int ir_sensor_pin=5;
     int detect_moisture;
     int detect_ir;
 
     Servo servo;

     Smart_Segregator(int pin_1, int pin_2, int pin_3)
     {
       servo_pin = pin_1;
       moisture_sensor_pin = pin_2;
       ir_sensor_pin = pin_3;

       detect_moisture = 1;
       detect_ir = true;
     }

    void init()
    {
      Serial.begin(9600);
      servo.attach(servo_pin);
      servo.write(90);
      
      pinMode(moisture_sensor_pin, INPUT);
      pinMode(ir_sensor_pin, INPUT);

      Serial.println("segration is raedy");
    }

    void dry_waste()
    {
      servo.write(0);
    }

    void wet_waste()
    {
      servo.write(180);
    }

    void neutral_state()
    {
      servo.write(90);
    }
    
    void execute()
    {
      detect_moisture = analogRead(moisture_sensor_pin);
      detect_ir = digitalRead(ir_sensor_pin);

      if (detect_moisture <=1023 && detect_ir == true)
      {
        wet_waste();
      }

      else if (detect_moisture ==0 && detect_ir == true)
      {
        dry_waste();
      }

      else 
      {
        neutral_state();
      }
    }
}

segregator = Smart_Segregator(16,a0,5);

void setup() 
{
  segregator.init();
}

void loop() 
{
 segregator.execute();
}