Pir sensor fade effect

I need help with code, can someone help me to write code?

I want when ldr sensor sense dark, then turns pir sensor on and then every time when is dark
pir sensor, when sense movement fade led diode up to 30% of light and stay on for 1 minute.

Here is example of code, but i need it to work like i'm described above.
p.s. here you have sampe picture without ldr sensor to get idea...

const int ledPin= 13;
const int inputPin= 2;

void setup(){
pinMode(ledPin, OUTPUT);
pinMode(inputPin, INPUT);
}

void loop(){
int value= digitalRead(inputPin);

if (value == HIGH)
{
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
}
else
{
digitalWrite(ledPin, LOW);
}
}

Arduino-motion-sensor-circuit.png

Important is to let your code reflect the used sensors and so. (use of code tags is also appreciated just as using CTRL-T in the IDE for auto formatting.

const int ledPin = 13;
const int LDRpin = A0;
const int PIRPowerpin = 2;
const int PIRDATApin = 3;

const int LDR_DARK_THRESHOLD = 100;

void setup()
{
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);

  pinMode(PIRPowerpin, OUTPUT);
  digitalWrite(PIRPowerpin, LOW);

  pinMode(PIRDATApin, INPUT);
}

void loop()
{
  int LDRvalue = analogRead(LDRpin);

  if (LDRvalue < LDR_DARK_THRESHOLD)  // detect dark
  {
    digitalWrite(PIRPowerpin, HIGH); // switch PITR ON
    if (digitalRead(PIRDATApin) == HIGH)
    {
      digitalWrite(ledPin, HIGH);
      delay(1000);
    }
    digitalWrite(ledPin, LOW);
  }
  digitalWrite(PIRPowerpin, LOW);
}

Thank you, can you complement the code with led fade function, i need that led diode fade when pir detect movement from 0% to 30% light of 100% led diode light that can produce.

p.s. how do you connect ldr to work: From A0 to ldr and to ground or?

here is example how pir sensor fade needs to work.

Thank you, can you complement the code with led fade function, i need that led diode fade when pir detect movement from 0% to 30% light of 100% led diode light that can produce.

Better let you think about that first.

p.s. how do you connect ldr to work: From A0 to ldr and to ground or?

as a voltage divider - just google LDR Arduino and you will get a zillion images.

can this be useful ? Freelance, professional Arduino code writing. - Jobs and Paid Consultancy - Arduino Forum

robtillaart, i was connected ldr when you put you're code to me in this way like it's in this picture in attachment and ldr doesn't work, i don't understand when you say: "Better let you think about that first."
but if you really wan't help then help and don't play game with words, is that hard to help beginner to give him code and let him learn that code and understand it? Do you want that i learn from books coding in c++ with 1000 examples in all this books where i still can't find that kind of pir fading? For now i need that little help, if you can help.

I'm not lazy that i won't be google for image of ldr, i did that when i saw that code doesn't work with ldr connected the way like it's in those attachment picture.

knut_ny, thank you for help, this code in this link doesn't do fading.

ldrarduino.jpg

Adafruit to the rescue....

Here's how to wire and code an LDR and here's their PIR tutorial.

In the PIR code, where it turns the LED on you could just use this to fade the LED up with an analogWrite to 30% of 255. (Assuming you will be able to see it.....)

"doesn't do fading"

...this is fading..
for (byte i=40; i<255; i+=5)
{
analogWrite(led,i);
delay(delayTime);
}

..but instead of the for-loop u want the analog reading to control light?
is that series resistor 1k (Brown,Black,Red) ?
post the analog reading you get when so dark that led shall be fully on..
..and the reading when so light that led shoud be off.

I'll show you a suitable transfer function to use..

How can i make that LED stay turned on for one minute? Now fade in and immediately fade out, i tried with delay values but then fade is longer.

/*

  • PIR sensor tester
    */

int ledPin = 9; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status

void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input

Serial.begin(9600);
}

void loop(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30); // turn LED ON
}
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {

if (pirState == HIGH){
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}

tony,
please use [code]...[/code] tags

robtillaart, http://www.arduino.cc/en/Tutorial/Fading

this is original fade code implemented in my last post.

Happy now? (i'm still beginner if you remember and i yet don't understand code fully)

Happy now? (i'm still beginner if you remember and i yet don't understand code fully)

Sorry, I do not want to offend you, only asking to use code tags around your code so it becomes more readable and it prevents the source-code interpreted as forum tags. I understand you are new and are willing to learn, so I should give you the opportunity to learn yourself first.

Here a hint how to improve the fading. Your code makes steps of 5 and waits for 30 millis. You can make the fading more fluent by making step size 1 and wait 6 millis. The loop will take same time but makes smaller steps so it is smoother.

    for (int fadeValue = 255; fadeValue >= 0; fadeValue-- )
    {
      analogWrite(ledPin, fadeValue);        
      delay(6);
    }

on top, declare long time_to_off; // to remenber time when dimming to start

somwhere in code: when just finished loop turn on? or detector says 'quiet'?
this statment:
time_to_off=millis() + 10000; // or add other suitable value

the dimming part:
if (millis()>time_to_off) { the loop to dim leds inside brackets }

I solved the time to wait untill the light is turn off

Here is the code:

/*

  • PIR sensor fade effect
    */
    int ledDelay = 30000; //time to wait untill light switch off
    int ledPin = 9; // choose the pin for the LED
    int inputPin = 2; // choose the input pin (for PIR sensor)
    int pirState = LOW; // we start, assuming no motion detected
    int val = 0; // variable for reading the pin status

void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input

Serial.begin(9600);
}

void loop(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(60); // turn LED ON
}
delay(ledDelay); //time to wait untill light switch off
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(60);
}
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {

if (pirState == HIGH){
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}

How can i make brightness of fade for let's say 40% ?

try change "255" to "100"

These cheap PIR modules you get from E-bay and such can have an LDR attached directly to them as described in this post, so the only thing the Arduino would do is read the signal from the PIR and control the LED.