Why no servo movement?

Hello!

Can someone help me to understand why I am not getting servo movement while my photocell is in light? Everything works perfectly in the dark!

#include <VarSpeedServo.h>

VarSpeedServo servo; //Initializes Servo
const int ledOne = 5; //sets LED 1 input to 5
const int ledTwo = 6; //sets LED 2 input to 6
const int spkrOne = 3; //Connect Speaker 1 to output 3
const int spkrTwo = 4; //Connect Speaker 2 to outpu 4
const int photoCell = 18; //Connect Photo Voltaic to analog 0
int photoValue = 150; //Adjust this to equal the value read from the cell when dark 512 = 2.5 volts
int currentLed = 1; //LED toggle
unsigned long currentTime;
long previousTimeS;
long previousTimeL;
long ledInterval = 240000; //4 minute LED timer
long servoInterval = 600000; //10 minute servo timer

void setup(){
servo.attach(9); //Initializes the servo
pinMode(spkrOne, OUTPUT); //Setting all outputs
pinMode(spkrTwo,OUTPUT);
pinMode(ledOne, OUTPUT);
pinMode(ledTwo, OUTPUT);
digitalWrite(ledOne, HIGH); //Starts LED sequence with one on.
servo.write(0, 40); //starts servo at 0 degrees, speed at 40
}

void loop(){
while(analogRead(photoCell) > photoValue){ //Suspends LED program while the photocell is in light
digitalWrite(ledOne, LOW);
digitalWrite(ledTwo, LOW);
previousTimeS = millis() - 600000;
previousTimeL = millis() - 240000;
}

currentTime = millis(); //sets current time based on how long the program has been running
if(currentTime - previousTimeL > ledInterval){ //Determines if enough time has lapsed to switch LEDS
if(currentLed == 1){ //Determines which state the LEDs are in and switches them
digitalWrite(ledOne, LOW);
digitalWrite(ledTwo, HIGH);
currentLed = 2;
}
else{
digitalWrite(ledOne, HIGH);
digitalWrite(ledTwo, LOW);
currentLed =1;
}
previousTimeL = currentTime;
}
if(currentTime - previousTimeS > servoInterval){
if(servo.read() == 0){
servo.write(180, 40);
}
else{
servo.write(0, 40);
}
previousTimeS = currentTime;
}

if(servo.read() == 0){
digitalWrite(spkrOne, HIGH);
digitalWrite(spkrTwo, LOW);
}
if(servo.read() == 180){
digitalWrite(spkrOne, LOW);
digitalWrite(spkrTwo, HIGH);
}
}

Thank you!

Can someone help me to understand why I am not getting servo movement while my photocell is in light?

Perhaps because the program is stuck in this while loop

  while(analogRead(photoCell) > photoValue)
  {      //Suspends LED program while the photocell is in light
    digitalWrite(ledOne, LOW);
    digitalWrite(ledTwo, LOW);
    previousTimeS = millis() - 600000;
    previousTimeL = millis() - 240000;
  }

I'm sorry Bob, I'm a big time beginner and having a hard time wrapping my head around this. I thought it was a problem with that section but couldn't quite figure out how to solve it. I understand that while in light, my servo timer is going to be 0 - 10 minutes (-10 minutes). If I remove "previousTimeS = millis() - 600000" it should solve the problem, I think.

The problem is that while the photoCell value exceeds 60 the only code that will be executed is that between the braces of the while loop and that does not include any servo commands.

If you need to do things that depend on whether the light is on or off then set a boolean variable to true when the light is on and false when it is off then test its value before executing code that depends on its value.

You have potential problems with your timing variables too. All of them should be declared as unsigned long as they can never be negative.

while(analogRead(photoCell) > photoValue){

This is not a useful way to read an analog pin because there is no opportunity to find out what value is being obtained

Separate the code into separate pieces

photoCellValue = analogRead(photoCell);
Serial.println(photoCellValue); // optional

while (photoCellValue .....

...R