Hi I am doing a project of home automation is the following: I have outdoor lights that through a LDR lights an LED, for blinds I have a servo that through a LDR rotates 180 degrees and an alarm system in the which there is a PIR sensor and an LED, when the PIR sensor detects motion, the LED lights.
All this and I have a problem is that when I plugged the LDR Exterior Lights i the LED Exterior Lights alarm system, ie, the PIR sensor will not and does not turn the LED lights.
No problem if the code or the circuit, here I leave the code:
#include <Servo.h>
int pinLedLight = 13;
int pinLedAlarm = 8;
int pinServo = 12;
int pinLDRLight = A0;
int pinLDRServo = A1;
int pinPirSensor = 2;
int pirState = LOW;
Servo servo;
int LDRThreshold = 20;
void setup() {
Serial.begin(9600);
servo.attach(pinServo);
pinMode(pinLedLight, OUTPUT);
pinMode(pinLedAlarm, OUTPUT);
pinMode(pinPirSensor, INPUT);
pinMode(pinLDRLight, INPUT);
pinMode(pinLDRServo, INPUT);
}
void loop() {
ldrFunction( analogRead(pinLDRLight) );
servoFunction( analogRead(pinLDRServo) );
alarmFunction ( digitalRead(pinPirSensor) );
}
boolean calculateOnOff(int val, int threshold) {
if (val >= threshold) {
return true;
} else if (val < threshold) {
return false;
}
}
int ldrStatus = false;
void ldrFunction(int val) {
boolean onOff = calculateOnOff(val, LDRThreshold);
if (onOff) {
digitalWrite(pinLedLight, LOW);
if (ldrStatus == false) {
Serial.println("Luces jardin encendido");
ldrStatus = true;
}
} else {
digitalWrite(pinLedLight, HIGH);
if (ldrStatus) {
Serial.println("Luces jardin apagado");
ldrStatus = false;
}
}
}
boolean servoStatus = false;
void servoFunction(int val) {
boolean onOff = calculateOnOff(val, LDRThreshold);
if (onOff) {
servo.write(90);
if (servoStatus == false) {
Serial.println("Persianas On");
servoStatus = true;
}
} else {
servo.write(0);
if (servoStatus) {
Serial.println("Persianas Off");
servoStatus = false;
}
}
}
void alarmFunction(int val) {
if (val == HIGH) {
digitalWrite(pinLedAlarm, HIGH);
if (pirState == LOW) {
Serial.println("Pir encendido");
pirState = HIGH;
}
} else {
digitalWrite(pinLedAlarm, LOW);
if (pirState == HIGH){
Serial.println("Pir apagado");
pirState = LOW;
}
}
}