LED dimming with Motionsensor

Hello,
this is what i try to do:
I want to connect a LED Stipe wich is dimming up when motion is detectet and stay on for about 2 minutes, when new motion is detectet it should stay on. When no motion is detectet whitin two minutes it should dimm down.
I am a Beginner, i have searched a lot but did not found any answer.
Here is the Code I used. But this does what it wants :slight_smile:
digitalWrite 13 is only to see if the Delay is working

int motion = 3;
int motionLed = 9;

void setup() {
//ok i need to state what each pin will be doing. the led pin will
//be an output and the motion pin will be an input.
pinMode(motion, INPUT);
pinMode(motionLed, OUTPUT);
pinMode(13, OUTPUT);
}

void loop()
{
//what will happen in the sketch
//if motion is detected we want to turn the led light on
//if no motion is detected turn the led off
//you also need to declare a variable to hold the sensor data
long sensor = digitalRead(motion);
//then the if statement to control the led
if(sensor == HIGH){
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(motionLed, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(100);
}
if(sensor == HIGH)
{
digitalWrite (9, HIGH);
}
{
digitalWrite (13, HIGH);
}
{delay (120000);}
{
{
digitalWrite (13, LOW);
}
} }
else
// fade out from max to min in increments of 5 points:
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
// sets the value (range from 0 to 255):
analogWrite(motionLed, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(100);
}
}

You can't use delay if you're looking for active retriggering.
The way it's written, it probably fades in every 2 minutes if motion is continuously sensed.

You need to show what motion sensor you are using. A lot of PIR boards have their own retrigger settings.

I have this one: HC-SR501 PIR Infrarot Bewegungsmelder Modul Motion Sensor Arduino Raspberry

http://www.ebay.de/itm/5x-HC-SR501-PIR-Infrarot-Bewegungsmelder-Modul-Motion-Sensor-Arduino-Raspberry/162452966930?ssPageName=STRK%3AMEBIDX%3AIT&_trksid=p2057872.m2749.l2649

Turn the time delay down on the PIR sensor, treat it like a button, and read up on state change detection.
You only want the light to fade in at the beginning (such as new button press), stay on as long as the sensor is active (but not keep fading in repeatedly), and fade out when that changes.

how do i do that?
This does not work:

int motion = 3;
int motionLed = 9;

void setup() {
//ok i need to state what each pin will be doing. the led pin will
//be an output and the motion pin will be an input.
pinMode(motion, INPUT);
pinMode(motionLed, OUTPUT);
pinMode(13, OUTPUT);
}

void loop()
{
long sensor = digitalRead(motion);
//then the if statement to control the led
if(sensor == HIGH) {
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(motionLed, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(100);
}}

// fade out from max to min in increments of 5 points:
//if(sensor == LOW)
else {
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
// sets the value (range from 0 to 255):
analogWrite(motionLed, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(100);
}}
}

Replace the PIR with a button. Use either pullup or pulldown, depending on whether your PIR is active low or high.
Make your LED behave how you want it to- push button makes it fade in, holding button down keeps it on, releasing the button makes it fade out.
At least try.