Loading

Report as inappropriate

program for photoresistor control of this switch with hysteresis function. the dusk and dawn light levels were causing the servo to change state, on and off a bit as the A/D bounced around, so now it has an on point at 200/1024 counts, and off point at 500/1024 counts. in indirect daylight the light level is 1000+/1024. notice the if/elseif/else code that can be tweaked for conditions. also i connected this with PLX-DAQ to monitor the function of the switch without having to look at it.

/* photo resistor controlled servo

Connect the photoresistor one leg to pin 0, and pin to +5V
Connect a resistor (around 10k is a good value, higher
values gives higher readings) from pin 0 to GND. (see appendix of arduino notebook page 37 for schematics).


       PhotoR     10K-100K

+5 o---/\/\/--.--/\/\/---o GND
|
Pin 0 o-----------


*/

include

Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
int oldpos =0;// variable to store old POS
int lightPin = 0; //define a pin for Photo resistor

void setup()
{
myservo.attach(9); // attaches the servo to pin 9 of arduino
Serial.begin(9600); //Begin serial communcation
}

void loop()
{
Serial.print("Date , Time ,");
Serial.print(analogRead(lightPin));
Serial.print(",");
oldpos = pos;
if(analogRead(lightPin)<200){
pos = 105;
}
else if (analogRead(lightPin)>500){
pos = 25;
}
else {
pos = 60;
}

if(pos != oldpos){
myservo.write(pos); // tell servo to go to position in variable 'pos'
Serial.println(pos);
delay (150);
myservo.write(60); //center servo to prevent staining
delay(60000); //change to suit conditions 60000 = 1 minute
}}