Hai. We are doing a project about a suspension that can be automatically turned on and close when it detects rain. Also, there will also be a heat lamp as a heater so that the laundry still dry even under a closed suspension. (It’ll also be automatically turned on simultaneously with the rooftop.) We are using a raindrop sensor and 2 servo motors. The problem is, we don’t really know how to do the coding that can involve raindrop sensors and 2 servos. We need both of the servos to be turned 180 degree simultaneously. Can someone help us with the coding?
We already programmed one servo motor, so it means when the sensor detects water, only one of the servos spinned. Our power source is a solar panel.
Here is the code that spins 1 servo motor
#include <Servo.h>
#include <Servo.h>
Servo tap_servo;
int sensor_pin = 4;
int tap_servo_pin =5;
int val;
void setup(){
pinMode(sensor_pin,INPUT);
tap_servo.attach(tap_servo_pin);
}
void loop(){
val = digitalRead(sensor_pin);
if (val==0)
{tap_servo.write(0);
}
if (val==1)
{tap_servo.write(180);
}
}
the solar panel is 5V. Can you help us change the code so it spins both of the servos?
Add stuff for this other servo in the global variable area and also in the setup and loop functions.
Finally test it with just the other servo attached and see that it behaves correctly before trying to run the two at once because I have doubts if your solar panel is capable of driving two servos.
If it does not work then post the new code and say what it does and what you expected it to do.
Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.
Can you post some images of your project?
So we can see your component layout.
ok so now we have changed the plan a little bit. We are making one of the servo turn 180 and the other 360. We did the coding, but when we installed it, the 360 servo spins on its own without having any water on the raindrop sensor. Is there anything we’re doing wrong?
Here’s the coding
#include <Servo.h>
Servo servo360;
Servo other_servo;
int sensor_pin = 4;
int servo360_pin = 5;
int other_servo_pin = 9;
int val;
void setup(){
pinMode(sensor_pin,INPUT);
servo360.attach(servo360_pin);
other_servo.attach(other_servo_pin);
}
void loop(){
val = digitalRead(sensor_pin);
if (val==0)
{servo360.write(0);
other_servo.write(0);
}
if (val==1)
{servo360.write(180);
other_servo.write(180);
}
}