PIR Sensor light project / stuck

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?

Instead of having two local variables named sensor, in setup() and loop(), there should be one global variable. It's type should be byte, unless you plan to attach the sensor to pin 256 or higher, and should be const.

You need to separate the decision making process (should the LED be turned on or off) from the implementation process. The decision to turn the LED is based on the status of the PIR sensor (there is motion). The decision to turn the LED off is based on the PIR sensor (there is no motion) AND the length of time the LED has been on.

So, first you decide whether the LED should be on or off.

Then, you decide, if it should be off, if it's time to turn it off.

Then, you do it.

Look at the blink without delay example.

Of course, you could simply add a call to delay() to keep the LED on for some defined period of time, but that makes the whole system unresponsive.