// ESET 462 Project Code // Backyard Light System // Defining Variables int photocellPin = 0; // the ldr and 10K pulldown are connected to a0 (voltage divider) int photocellReading; // the analog reading from the sensor at vd int LEDpin = 11; // connect LED chip to pin 11 (PWM pin) int LEDbrightness; // brightness of the LED chip int sensor = 2; // output pin of pir sensor int state = LOW; // no motion detected by default int sensor_status = 0; // stores status of pir sensor void setup(void) { pinMode(sensor, INPUT); // initialize sensor as input Serial.begin(9600); // Send debugging info via the Serial monitor } void loop(void) { // ******* LED Chip ******* // photocellReading = analogRead(photocellPin); // LED gets brighter the darker it is at sensor // inverts the reading from 0-1023 back to 1023-0 photocellReading = 1023 - photocellReading; //-------------------------------------------------------- sensor_status = digitalRead(sensor); // pir sensor value if (sensor_status == HIGH){ // if sensor detects motion if (state == LOW) { Serial.println("Motion detected!"); state = HIGH; // update variable state to HIGH } // While condition for when sensor detects motion while(sensor_status == HIGH){ photocellReading = analogRead(photocellPin); photocellReading = 1023 - photocellReading; //Powers LED based on available natural light LEDbrightness = map(photocellReading, 0, 1023, 0, 255);//maps 0-1023 to 0-255 since thats the range analogWrite uses analogWrite(LEDpin, LEDbrightness); delay(100); sensor_status = digitalRead(sensor); // pir sensor value } } else { // if sensor does not detect motion if (state == HIGH){ Serial.println("Motion stopped!"); state = LOW; // update variable state to LOW } //Powers off LED analogWrite(LEDpin, 0); delay(100); } }