Hi,
I am totally new to Arduino and have to build something for a school project. My idea was to make a light ( in this case a LED ) turn on and off by motion. So I bought a Arduino Uno and a PIR sensor and went to work. Although, I've come across a problem. I want the light to stay on when I activate the PIR sensor. Then when the light is on, I want to be able to turn it off by using the sensor again. So it's kind of a back and forth thing.
Whenever the PIR sensor detects motion, the LED turns on. But that's where I'm stuck. I can make the LED stay on by the following code:
boolean lightOn = false;
void setup() {
int sensor = 8;
pinMode(sensor,INPUT);
pinMode(3,OUTPUT);
}
void loop() {
int sensor = 8;
if ((digitalRead(sensor) == HIGH) && (lightOn == false)) {
digitalWrite(3,HIGH);
lightOn = true;
} else if ((digitalRead(sensor) == LOW) && (lightOn == true)) {
digitalWrite(3,HIGH);
lightOn = true;
}
}
But I don't know how I make it so that when the light is on, it will turn off when I'm activating the PIR sensor.
I tried another if statement with different states, but it will just loop and turn the light off again.
Does anyone know how I can solve this?