Noob - Help with programming servo+led+photoresis

Hi, im new here and also new to programming.
I needed some help and see what i was typing in wrong.

I am trying to Cycle the LED on and off every 1 second and have the photo cell respond to the LED turning on and off.

So when the LED is off, the servo motor is a one extreme position. When the LED is on, the servo motor should move to its other extreme position.

Here is the coding i have:
Thanks in advance guys!

#include <Servo.h>

Servo servomotor;
int photoresistor = 0; // set photoresistor to analog pin 0
int motor = 9; // servo is connected to I/O pin 9
int ledPin = 2; // LED is connected to pin 2
int val = 0; // Input value
int setpoint = 0; // Trigger Point

void setup() 
{
  servomotor.attach(motor);
  pinMode(ledPin, OUTPUT);
}
void loop()
{
  digitalWrite(ledPin, HIGH); // LED is on
  delay(1000); // wait for 1 second
  digitalWrite(ledPin,LOW); // LED is off
  delay(1000); // wait for 1 second
  
val = analogRead(photoresistor); // Read analog input value

if(val>setpoint)
  servomotor.write(179); // There is light and servo moves to 179 deg.
else
  servomotor.write(0); // There no light and servo moves back to 0 deg.
}

So the problem i had was that servo isnt moving at all.

How do i make it so the photosensor does see the LED?

How do i make it so the photosensor does see the LED?

Make sure the LED is on when you read the analogue input.

what would the correct coding be?

Would i put LED coding somewhere else? or the val somewhere else?

Could i write something like

digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
if(analogRead(0) >0)
servom.write(179);
} 
else
{
servom.write(0);
}
}

OK. I’m confused.

You are still turning OFF the LED and then getting the reading from the sensor. Repeating: That will NEVER work.

Is this what you are trying to do?..

  • Turn on LED
  • Read the sensor
  • Control the servo based on what the sensor is seeing
  • Turn off the LED
    * Read the sensor
    * Control the servo based on what the sensor is seeing