Hobby Servo with Photo Resistor

Hello, I am LinktheInventor, and I would like to know if there is code out there to make a Hobby Servo run by the Photo Resistor ( I hope that makes sense).
I am using the Arduino Uno (which I got with the Adafruit ARDX - v1.3 Experimentation Kit for Arduino (Uno R3) - v1.3), and I don't know much code. I wan't this combination to do this:
1.The light gets below a certain level.
2. The photo resistor "reads" the light and sends a "signal" to the Hobby Servo, which closes a door. ( Pet cages is what I am mostly thinking of)

If you have any questions, ask them below! :slight_smile:

Thanks!

I assume that you want the value of the photo resistor to determine the position of the servo arm.

If that is the case their is some code. I will work up an example for you.

Not sure exactly what you mean, no.... But here's what I did a while ago: I mounted an LDR on the horn of a servo, and coded it so that if the LDR was in the dark, the servo would start sweeping until it was in the light and then it would stop. Is that the kind of thing you mean?

You won't get full code for a school project supplied here, since you're supposed to be learning, but here's a couple of links to save you searching. Here is how to read an LDR, and here is how to run a servo.

Leave it to you figure out how to combine the two....

I will work up an example for you.

You sure you should be doing that for a someone's school project, Drew?

This code might work. I don't have time to test it. This will at least give you a place to start. As for his school project this code is PART of it. I'm sure he will add to it.

#include <Servo.h> 
 
Servo myservo;  
int sensorValue = 0;
int sensorPin = A0; 
 
void setup() 
{ 

myservo.attach(9); 

} 
 
void loop() 
{ 
  
  sensorValue = analogRead(sensorPin);
  
  sensorValue = sensorValue / 5.7; 
 
 myservo.write(sensorValue);
 
delay(1200);

}

JimboZA:

I will work up an example for you.

You sure you should be doing that for a someone's school project, Drew?

I understand that it is my project, but I just have some questions that me (and my family) can't answer (we have basic understanding of it,but we are willing to learn). Thanks for the link though!

Make sure to tell me if the code works!

Thanks,
Drew Davis

How did the code work?

Also after rereading your post it appears that you want to make the door close after a certain level of light is reached. If that is the case tell us and I (or you) can adapt the code.

The door closing is a good project, since it will be interesting mechanically too- the angles of linkages etc to get it close smoothly etc etc.

One question.... how will you make sure the inhabitants are on the right side of the door?... or even more important, not getting jammed in the closing door?

He could use a motion sensor… If movement is detected & light is right shut the door.

This would work….

Good luck LinktheInventor!