Trying to make a servo flip a switch.

MorganS:
In case it's too subtle: the semicolons (:wink: don't belong on the end of if()

thanks for catching that. Did not realize that I did that.

ChrisTenone:
You might need some timers. If the way it works is: turn the light on if it's dark, and off if it's light. Any light would cycle the light off then on, which is not what you want I'm sure. Use a timer: it should be light for 30 seconds or so before turning off the light. Don't use delay, or it will still flash.

Oh man, adding delays(not how i wanted to do it) worked. Is it possible to have a delay at the start (of being dark 4hours) then having another statement to keep it dark without having a delay, so the when it passes to be sunny the light will turn off(so it will be more dynamic)?

#include <Servo.h>

// servo set up
Servo servo1;

//light sensor set up
const int photoSen= 0; //photoresistor pin
const int sunny= 70; // light val for sunny, can be changed
int lightLevel; //light level

void setup() {
  // put your setup code here, to run once:
pinMode(photoSen, INPUT);
servo1.attach(9);
//serial monitor
Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
lightLevel = analogRead(photoSen);
Serial.println("lightLevel");
Serial.println(lightLevel);

 if(lightLevel > sunny){ //stays off if sunny
  servo1.write(180);
  //delay(5000);
 }
 if(lightLevel <= sunny){//turn on
  servo1.write(120);
  delay(5000);
 }
}

this is for front porch lighting. basically what i want it to do is

this is not actual code
lightlevel>sunny: lights off
lightlevel<sunny, delay(4hr): lights on for 4hr for when there are lights on in the house
lightlevel<sunny, nodelay: keep lights on after delay without the delay to turn off the light when sunny>lightlevel

wvmarle:
If you give your LDR an appropriate resistor you can condense loop() into two lines. The digital input, being a Schmitt trigger, takes care of the hysteresis. A 10k resistor is a good starting point.

void loop() {

servo.write((digitalRead(photoSen)) ? 120 : 180);
 delay(5000);
}




You may have to switch the 120 and 180 to make it work the correct way around.

what is the question mark for?