difficult solution???? re: PIR's

I got this off arduino playground. it works pretty cool, but not cool enough. I’m really new and REALLY appreciate your help. I wired this up and here is what happens. once stabilized when the PIR detects motion it turns on the LED. for about 5 seconds. if you keep moving in front of the PIR the LED STAYS ON.

see if you get this. what I want is, when PIR is triggered a 1s HIGH to PIN 13, here’s the HARD PART!!

you walk in front of the PIR, it triggers for 1s, you keep moving, but pin 13 goes low, UNTIL you STOP moving.

it’s kinda backwards you walk in front of pir, led goes on for second, constant motion doesn’t keep led on, led won’t go on AGAIN till you have completely stopped and THEN move.

AND NO the trim on the PIR is not a solution. (to unstable)

thanks

/* 
* //////////////////////////////////////////////////
* //making sense of the Parallax PIR sensor's output
* //////////////////////////////////////////////////
*
* Switches a LED according to the state of the sensors output pin.
* Determines the beginning and end of continuous motion sequences.
*
* @author: Kristian Gohlke / krigoo (_) gmail (_) com / http://krx.at
* @date:   3. September 2006 
*
* kr1 (cleft) 2006 
* released under a creative commons "Attribution-NonCommercial-ShareAlike 2.0" license
* http://creativecommons.org/licenses/by-nc-sa/2.0/de/
*
*
* The Parallax PIR Sensor is an easy to use digital infrared motion sensor module. 
* (http://www.parallax.com/detail.asp?product_id=555-28027)
*
* The sensor's output pin goes to HIGH if motion is present.
* However, even if motion is present it goes to LOW from time to time, 
* which might give the impression no motion is present. 
* This program deals with this issue by ignoring LOW-phases shorter than a given time, 
* assuming continuous motion is present during these phases.
*  
*/

/////////////////////////////
//VARS
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;        

//the time when the sensor outputs a low impulse
long unsigned int lowIn;         

//the amount of milliseconds the sensor has to be low 
//before we assume all motion has stopped
long unsigned int pause = 5000;  

boolean lockLow = true;
boolean takeLowTime;  

int pirPin = 3;    //the digital pin connected to the PIR sensor's output
int ledPin = 13;


/////////////////////////////
//SETUP
void setup(){
 Serial.begin(9600);
 pinMode(pirPin, INPUT);
 pinMode(ledPin, OUTPUT);
 digitalWrite(pirPin, LOW);

 //give the sensor some time to calibrate
 Serial.print("calibrating sensor ");
   for(int i = 0; i < calibrationTime; i++){
     Serial.print(".");
     delay(1000);
     }
   Serial.println(" done");
   Serial.println("SENSOR ACTIVE");
   delay(50);
 }

////////////////////////////
//LOOP
void loop(){

    if(digitalRead(pirPin) == HIGH){
      digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state
      if(lockLow){  
        //makes sure we wait for a transition to LOW before any further output is made:
        lockLow = false;            
        Serial.println("---");
        Serial.print("motion detected at ");
        Serial.print(millis()/1000);
        Serial.println(" sec"); 
        delay(50);
        }         
        takeLowTime = true;
      }

    if(digitalRead(pirPin) == LOW){       
      digitalWrite(ledPin, LOW);  //the led visualizes the sensors output pin state

      if(takeLowTime){
       lowIn = millis();          //save the time of the transition from high to LOW
       takeLowTime = false;       //make sure this is only done at the start of a LOW phase
       }
      //if the sensor is low for more than the given pause, 
      //we assume that no more motion is going to happen
      if(!lockLow && millis() - lowIn > pause){  
          //makes sure this block of code is only executed again after 
          //a new motion sequence has been detected
          lockLow = true;                        
          Serial.print("motion ended at ");      //output
          Serial.print((millis() - pause)/1000);
          Serial.println(" sec");
          delay(50);
          }
      }
 }

Please use the code button </>

so your code looks like this

To achieve what you want you probably need code something like this pseudo code

if (PIRis active) {
   lastMillis = millis(); // this "resets the clock" every time there is movement
}

if (millis() - lastMillis >= interval) {  // this only triggers if the clock was not reset for a time
   // there has been no action for interval millisecs
}

…R

sorry bout that. robin, can you explain how I do that? the code button

COOL paul, I will check it out

where is the best place to study putting all the sketches into 1 sketch? I have several that I can make operate individually that I am very satisfied with. but, being new I know it will take some study. thanks!

so far I have eliminated 30 of 32 components I used in my analog version of my project.

oh, ?? is there anyway to disable the ON LED? unless it is super efficient and doesn't use much power I won't need it when I am done?

So your sketch can be in three states:

1 - I havent been triggered 2 - I have been triggered, and it's been les than 1s since then 3 - I have been triggered, and I'm waiting for motion to cease.

enum State {
  OFF = 0,
  ON = 1,
  WAITING = 2
} state;

unsigned long triggerTime;

void setup() {
  state = OFF;
}

void loop() {
  boolean weGotMovement = sense_movement();
  
  switch (state) {
    case OFF:
      if(weGotMovement) {
        turn_led_on();
        triggerTime = millis();
        state = ON;
      }
      break;
    case ON:
      if(millis() - triggerTime >= 1000) {
        turn_led_off();
        state = weGotMovement ? WAITING : OFF;
      }
      break;
    case WAITING:
      if(!weGotMovement) {
        state = OFF;
      }
      break;
  }
}

Fredric58:
sorry bout that. robin, can you explain how I do that? the code button

Read the two posts at the top of this Forum by Nick Gammon on guidelines for posting here, especially the use of code tags ("</>") when posting source code files. Also, before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read.

If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button.