Hello All:
Please take a look at my final term paper for my Light-o-Stat Project by following the attached link.
The majority of my documentation can be seen in the paper, but here are some other things too.
The Guts of the Project: The Mess that I made with an Arduino and Breadboard
int servoPin = 2; // Control pin for servo motor
int minPulse = 800; // Minimum servo position
int maxPulse = 2300; // Maximum servo position
int neutralPulse = 1500; //neutral servo
int pulse = 0; // Amount to pulse the servo
int prevPhotosensor; //last reading of sensor-last state
int increment = 50; //pwm changes
int goodLED= 6;
int darkLED= 7;
int brightLED = 5;
long lastPulse = 0; // the time in milliseconds of the last pulse
int refreshTime = 20; // the time needed in between pulses
int photoSensor = 0; // the value returned from the analog sensor
int sensorPin = 1; // the analog pin that the sensor's on
int average = 10; // reads per cycle
int potSwitch = 0; //potentiometer switch
int potMin = 0;
int potMax = 0;
int potPin = 0;
void setup() {
pinMode(servoPin, OUTPUT); // Set servo pin as an output pin
pinMode(potSwitch, INPUT);
pinMode(goodLED, OUTPUT);
pinMode(darkLED, OUTPUT);
pinMode(brightLED, OUTPUT);
pulse = neutralPulse; // Set the motor position value to the minimum
digitalWrite(goodLED, LOW);
digitalWrite(darkLED, LOW);
digitalWrite(brightLED, LOW);
Serial.begin(9600);
}
void loop() {
delay(100);
int x;
int photoTotal;
photoTotal = 0;
for (x = 0; x<average; x++) {
photoSensor = analogRead(sensorPin); // read the analog input
potSwitch = analogRead(potPin); //pulse = (photoSensor *19)/10 + minPulse;
photoTotal = photoSensor + photoTotal; // convert the analog value
}
photoSensor = photoTotal/average; //average of photo sensor readings
potMin = potSwitch-25;
potMax = potSwitch+50;
Serial.print("Amount of Light = ");
Serial.println((int)photoSensor);
Serial.print("Desired amount of light = ");
Serial.println((int)potSwitch);
if (millis() - lastPulse >= refreshTime) {
digitalWrite(servoPin, HIGH); // Turn the motor on
delayMicroseconds(pulse); // Length of the pulse sets the motor position
digitalWrite(servoPin, LOW); // Turn the motor off
lastPulse = millis(); // save the time of the last pulse
}
//good State
if(photoSensor <= potMax && photoSensor >= potMin){ //if light value is in desired range, do the following
digitalWrite(goodLED, HIGH);
digitalWrite(darkLED, LOW);
digitalWrite(brightLED,LOW);
Serial.println("Light is good");
//do nothing with pulse;
}
// bright State
if(photoSensor < potMin){ // if the actual light is greater than desired, do this...remember that the values are reversed, larger number=darker and smaller number = brighter
digitalWrite(goodLED, LOW);
digitalWrite(darkLED, LOW);
digitalWrite(brightLED,HIGH);
Serial.println("Too Bright");
//pulse clockwise
if(prevPhotosensor > photoSensor) { // if moving the shades makes it darker, it i doing the right thing, continue in this direction
prevPhotosensor = photoSensor;
pulse = pulse - increment;
if (pulse < minPulse)
pulse = minPulse;
}
}
// dark State
if(photoSensor > potMax){ // if level of light is below the minimum desired, it is too dark
digitalWrite(goodLED, LOW);
digitalWrite(darkLED, HIGH);
digitalWrite(brightLED,LOW);
Serial.println("Too Dark");
//pulse counter-clockwise
if(prevPhotosensor < photoSensor) { //if moving the shades makes it lighter, it is doin the right thing
prevPhotosensor = photoSensor;
pulse = pulse + increment;
if (pulse > maxPulse)
pulse = maxPulse;
}
}
}
<pre>

