Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * MotionActivatedLightSwitch.ino
- *
- * Created: 20/12/2012 4:38:29 PM
- * Author: grist.carrigafoyl
- * Target: Arduino Uno
- Activate a pin when motion is detected via a PIR sensor on another pin.
- Checks ambient light level via a photoresistor and only activates if the light level is low
- */
- #define PHOTORESISTOR 1 // Analog in
- #define PIR_PIN 8 // Digital in
- #define SWITCH_PIN 13 // Digital out
- // The light level below which the switch will trigger.
- #define LOW_LIGHT_LEVEL 300 // Based on experimentation.
- // Prototypes
- int read_adc(char pin);
- void setup()
- {
- pinMode(PIR_PIN, INPUT);
- pinMode(SWITCH_PIN, OUTPUT);
- }
- void loop()
- {
- byte pir_state;
- int curr_light_level;
- // How long the light stays on for. Simple constant for now
- int delay_time = 5000;
- // Loop forever
- for (;;) {
- pir_state = digitalRead(PIR_PIN);
- if (pir_state == LOW) { // Movement detected (pin is pulled high normally)
- curr_light_level = read_adc(PHOTORESISTOR);
- if (curr_light_level < LOW_LIGHT_LEVEL) {
- digitalWrite(SWITCH_PIN, HIGH); // On
- delay(delay_time);
- }
- }
- digitalWrite(SWITCH_PIN, LOW); // Off
- }
- }
- int read_adc(char pin)
- { // Read the analog value from the ADC input pin.
- int ret_val;
- ret_val = analogRead(pin);
- return(ret_val);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement