So I have a small chicken coop that I want to have the door automatically open and close with with a lever hooked up to a servo motor. The door slides very easy, but if necessary I will use more than one. I know very little about arduino. This is a basic code I found for the motor online, as it only needs to move 180 degrees. So when there is light I want the door open and when it is dark I want the door closed. Could anyone help me with the coding?
int pos = 0;
Servo servo_9;
void setup()
{
servo_9.attach(9);
}
void loop()
{
// sweep the servo from 0 to 180 degrees in steps
// of 1 degrees
for (pos = 0; pos <= 180; pos += 1) {
// tell servo to go to position in variable 'pos'
servo_9.write(pos);
// wait 15 ms for servo to reach the position
delay(15); // Wait for 15 millisecond(s)
}
for (pos = 180; pos >= 0; pos -= 1) {
// tell servo to go to position in variable 'pos'
servo_9.write(pos);
// wait 15 ms for servo to reach the position
delay(15); // Wait for 15 millisecond(s)
}
}
Wire the photo resistor in series with a 10K resistor and put one end of the joined resistors on 5V and and the other on GND of the Arduino. It does not matter which way round. Connect the common connection of the 2 resistors to an analogue pin on the Arduino and try the AnalogInput example sketch in the IDE. It is helpful to print the value read from the analogue pin to see what is going on.
Is this post related to your other thread IR sensor
First things first: make sure that in your actual circumstances, you can reliably distinguish light from dark..
UKHeliBob:
Wire the photo resistor in series with a 10K resistor and put one end of the joined resistors on 5V and and the other on GND of the Arduino. It does not matter which way round. Connect the common connection of the 2 resistors to an analogue pin on the Arduino and try the AnalogInput example sketch in the IDE.
Over in this thread, both Robin2 and groundFungus advocated using a digital input with its internal pullup enabled. That worked for me with the only ldr I had lying around.
It may not work with every ldr and every combination of light vs dark, but @OP it would certainly be worth your while trying it the digital way with input pullup first.
My objective was to get the OP used to reading the value from an analogue pin as a precursor to testing the value with a view to acting on the value. I figured that niceties like the internal resistor and using a digital pin could wait until he/she has the basics working.
As is often the case it is difficult to know how much the OP knows/understands before suggesting what may seem simple to you and me but complicated to someone else.
UKHeliBob:
I figured that niceties like the internal resistor and using a digital pin could wait until he/she has the basics working.
Good, valid point; I took the opposite view to suggest the simple way first (simple in the sense of not needing to find a resistor and dust off the breadboard). It had never occurred to me to even try an ldr on a digital pin until I read that other post a few days ago.
@OP, by the way, there are (at least) 3 ways to get a servo to go from one side to the other:
A simple "wam bam" approach where the servo is commanded simply with a write(0) or write(180)
A blocking style of sweep such as you posted, where nothing else can happen in the sketch while the for() loop is running
A non-blocking style of sweep using a similar apporach to blink without delay, where the servo gets the opportunity to move each time through loop(), and moves or not.
You need to consider which is appropriate. The top one is the simplest but may not be mechanically feasible, the middle one is a bit more complicated but blocks, and the bottom one is elegant but possibly unnecessary.
And if you really need to control the speed of movement, which is the point of the Sweep approach, there's the simpler alternative of using VarSpeedServo.h with its (non-blocking) speed parameter instead of Servo.h.