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);
}
}
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?
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)
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
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 ?
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.