I want to light one led with two sensors, the first one is a PIR and the other one is a Photocell sensor.
The idea is to turn on the led if the PIR sensor detects movement, and also to turn the led on if the room gets darker(detected by the photocell).
I managed to make a code, but with TWO led's, a different one for every sensor.
int ledPin = 13;
int ledPin1=12;
const int ldrPin = A0;
int inputPin = 2;
int pirState = LOW;
int val = 0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(inputPin, INPUT);
pinMode(ldrPin, INPUT);
}
void loop() {
int ldrStatus=analogRead(ldrPin);
if(ldrStatus<=400)
{
digitalWrite(ledPin, HIGH);
delay(500);
Serial.println("LED ON");
}
else digitalWrite(ledPin, LOW);
val = digitalRead(inputPin);
if (val == HIGH) {
digitalWrite(ledPin1, HIGH);
if (pirState == LOW) {
Serial.println("Motion detected!");
pirState = HIGH;
}
} else {
delay(5000);
digitalWrite(ledPin1, LOW);
if (pirState == HIGH){
Serial.println("Motion ended!");
pirState = LOW;
}
}
}
1 Answer 1
int ledPin = 13;
int ldrPin = A0;
int PIR_Pin = 2;
void setup()
{
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
pinMode(PIR_Pin, INPUT);
Serial.println("Started Monitoring!");
}
void loop()
{
boolean lightStatus = ( analogRead(ldrPin) <= 400 ) ;
digitalWrite(ledPin, lightStatus);
if(lightStatus) Serial.println("Light Triggered!");
if(digitalRead(PIR_Pin) == LOW)
{
Serial.println("Motion Triggered!");
digitalWrite(ledPin, HIGH);
delay(5000);
digitalWrite(ledPin, LOW);
}
}
(削除) To keep it simple serial code is removed, if needed can be added easily that should not be a big issue. (削除ここまで)
Still this can be done in much more efficient way. This is just one solution.
Note: haven't complied/error checked the code as i am writing this on a phone. Hope it should work as intended, errors if any, shall be minor ones. Comment below if any issues.
Update 1: added code to send triggered status over serial comm.
Update 2: corrected "Boolean" to be as "boolean"
-
1Yes, it works. Now I just need to print some messages so I can figure out which sensor turned the led on, because I will have 8 sensors in total, 4 of each. Thank you for the help!gollum– gollum2018年01月11日 10:13:05 +00:00Commented Jan 11, 2018 at 10:13
-
@gollum updated answer with code for serial comm. Do compile n test.25mhz– 25mhz2018年01月11日 10:30:12 +00:00Commented Jan 11, 2018 at 10:30
-
I got an error: "Boolean was not declared in this scope". @25mhzgollum– gollum2018年01月11日 15:34:01 +00:00Commented Jan 11, 2018 at 15:34
-
-
That should be "boolean" my bad, dint check that.25mhz– 25mhz2018年01月11日 15:38:38 +00:00Commented Jan 11, 2018 at 15:38
pirState
come from? It hasn't been defined or initialized. And what signal is oninputPin
? The general logic for doing something depending on either of two conditions isif( condition1 || condition2 ){ // do something here }
I want to light one led with two sensors
.... you wantmonitor two sensors and light one LED
.... you said that you have done it with two LEDs, so just modify your code so that both sensors have to satisfy a goal before the LED is activated