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 timervoid 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!