Go Down

Topic: PIR sensor with timer, first time and need help (Read 748 times) previous topic - next topic

aveaqim

May 06, 2013, 12:43 pm Last Edit: May 06, 2013, 12:49 pm by AWOL Reason: 1
Hi..i have just bought an arduino uno yesterday and im in need of an assistance.. Im just trying to create a PIR motion light sensor, but having problems with the timer. Note, i am very very new at this.. and i am really weak with the coding. I want my sensor to detect someone walking inside, and automatically on the LED. So far, i am only able to have it on for 5 sec(since i put a delay). But i want it to only light up when there is motion, and when there is no motion for 5 secs, it will turn off. The loop goes on and on.Kind of like in a meeting room, when there's somebody in there, it lights up, and when there isn't,it goes off. I have attached my schematic down below.
P/S : i've already checked http://arduino.cc/forum/index.php?PHPSESSID=d70b9d90ed1b63893949c6c3079318f7&topic=69447.0 , but im not really sure if it is the same. Any help is appreciated, thank you. Be nice pls XD. here is my coding so far;
Code: [Select]

int ledPin = 13;// choose the pin for the LED
int pirPin = 2;               // choose the input pin (for PIR sensor)
int val = 0;                    // variable for reading the pin status

void setup() {

 Serial.begin(9600);
 pinMode(ledPin, OUTPUT);      // declare LED as output
 pinMode(pirPin, INPUT);     // declare sensor as input

 

}

void loop(){

 val = digitalRead(pirPin);  // read input value

 if (val == HIGH) {            // check if the input is HIGH

   digitalWrite(ledPin, HIGH);  // turn LED ON
   delay(5000);

 } else {

   digitalWrite(ledPin, LOW); // turn LED OFF

   delay(300);    
 }

}


Moderator edit: code tags

aveaqim


wildbill

You'll need to get rid of the delays and use millis instead. Take a look at the blink without delay example to see millis in use. For your code, whenever the PIR is high, store millis in a variable and turn the LED on. Independently, check that variable against millis to see whether five seconds have passed and if so, turn the LED off.

PeterH

If I were you, I'd use the technique demonstrated in the 'blink without delay' sketch to turn the output off after a delay.

Code: [Select]

// incomplete, untested

void handleMotionSensor()
{
    static unsigned long lastMotionTime  = 0;

    if(motionDetected())
    {
        lastMotionTime = millis();
        lampOn();
    }
    else
    {
        if(millis() - lastMotionTime >= LampDuration)
        {
            lampOff();
        }
    }
}

I only provide help via the forum - please do not contact me for private consultancy.

aveaqim

yeah i've went through the whole forum ( sigh) and came up with this coding like the guy above me posted..the only problem is..when i put my hand for a short while(1-3 secs)..it turns off 2 seconds later..nothing wrong there, but ive noticed if i put my hand a little while longer(lets say 10 secs), the time it takes for it to turn off is longer than 2 secs..any ideas why?

int ledPin = 13;// choose the pin for the LED
int pirPin = 2;               // choose the input pin (for PIR sensor)
int val = 0;
unsigned long detectTime;

void setup() {

  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(pirPin, INPUT);     // declare sensor as input

}

void loop(){

  val = digitalRead(pirPin);
  // read input value

  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);
    detectTime = millis();
      // turn LED ON
  }
 
  if ( (millis() - detectTime) == 2000){
   
    digitalWrite(ledPin,LOW);
  }
 

}

PeterH

Code: [Select]

if ( (millis() - detectTime) == 2000)


You want to use >= here rather than ==, because this code might not execute just as the difference exactly equals 2000.
I only provide help via the forum - please do not contact me for private consultancy.

TeslaIaint

#6
May 07, 2013, 12:59 am Last Edit: May 07, 2013, 01:02 am by TeslaIaint Reason: 1
You might want to add another led that lights when motion is detected and turns off when there is no motion. This is just for debugging. Or you could use serial print. The reason the light stays on longer than your two seconds may be because the pir sensor is still detecting motion. Depending on your motion sensor, it's on board micro-controller may be programmed that way.

aveaqim

I need to put serial print to determine when motion starts and ends? So it is most probably then sensor and not the coding right? I'm gonna give it a shot and we'll see how it goes..thanks guy

TeslaIaint


I need to put serial print to determine when motion starts and ends? So it is most probably then sensor and not the coding right? I'm gonna give it a shot and we'll see how it goes..thanks guy


Yes, with the serial print, you can see when motion starts and stops. Or you can do the same thing with an additional LED so you can look at it instead of the computer screen. Your preference. Let us know how it goes.

Go Up