Photoresistor to turn off servo motor

Hey, I have a quick question, I'm pretty new to Arduino. I have been trying to use a photoresistor to measure the light and when its too dark the motor doesn't move, but I've been having issues with it. Here's my code so far. At least the loop part.

void loop() {
sensor_Value = analogRead(A0);
Serial.println(sensor_Value); //prints photoresistor value

if (sensor_Value >= 50){
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
val = 0;

Sorry, I just realized I put this in the wrong tab, my bad.


Your topic was Moved to it's current location / section as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

When you post code, post the whole sketch in code tags. Use the IDE autoformat tool (ctrl-t of Tools, Auto Format) before posting code.

A schematic of the project will help, too.

What issues do you have?
What Arduino controller are you using?
What servo are you using?
What are you using for power?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

Does the servo EVER move?

If so please describe in detail what happens when the sensor value is >=50 and you move the pot and then what happens when it’s <50 and you move the pot. And how is that different from what you want to happen?



When the light value is >=50 you want the servo to go to a position set by the potentiometer?

When the light value is <50 you want the servo to go to zero position?

You are only controlling the servo when >=50, you are doing nothing to the servo in the else… part of the code.

Tom… :slight_smile: