Pages: [1]   Go Down
Author Topic: PIR sensor with timer, first time and need help  (Read 660 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 2
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-lol. here is my coding so far;
Code:
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


* arduno forum.png (15.77 KB, 549x549 - viewed 24 times.)
« Last Edit: May 06, 2013, 05:49:03 am by AWOL » Logged

Offline Offline
Newbie
*
Karma: 2
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

a little help? anyone?
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
// incomplete, untested

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

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

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

Offline Offline
Newbie
*
Karma: 2
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
  }
 

}
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

You want to use >= here rather than ==, because this code might not execute just as the difference exactly equals 2000.
Logged

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

Ontario, Ohio
Offline Offline
Full Member
***
Karma: 1
Posts: 208
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: May 06, 2013, 06:02:15 pm by TeslaIaint » Logged

Offline Offline
Newbie
*
Karma: 2
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Ontario, Ohio
Offline Offline
Full Member
***
Karma: 1
Posts: 208
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: