const int LIGHT_SENSOR_PIN = A0; // Arduino pin connected to light sensor's pin
const int RELAY_PIN = 3; // Arduino pin connected to Relay's pin
const int ANALOG_THRESHOLD = 80;
const int MOTION_SENSOR_PIN = 7;
int motionStateCurrent = LOW; // current state of motion sensor's pin
int motionStatePrevious = LOW; // previous state of motion sensor's pin
// variables will change:
int analogValue;
void setup() {
Serial.begin(9600);
pinMode(MOTION_SENSOR_PIN, INPUT); // set arduino pin to input mode
pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode
}
void loop() {
motionStatePrevious = motionStateCurrent; // store old state
delay(100);
motionStateCurrent = digitalRead(MOTION_SENSOR_PIN); // read new state
analogValue = analogRead(LIGHT_SENSOR_PIN); // read the input on analog pin
if (analogValue < ANALOG_THRESHOLD && motionStatePrevious == LOW && motionStateCurrent == HIGH) {
digitalWrite(RELAY_PIN, HIGH); // turn on Relay
Serial.println("Motion detected!");
} else {
digitalWrite(RELAY_PIN, LOW); // turn off Relay
//Serial.println("Motion stopped!");
}
}