I have a working (and not very elegant) code that reads 2 ePIR motion sensors and turns on a hall light. Currently the code just reads a LOW(mapped analog because it bounces around .1v, not really zero) and uses delay() for a set time. But...if the timing is too short, then I run out of light and must re-trigger the sensor. And...
if the timing is longer I am wasting electricity.
I am sure there is a way to poll the sensors regularly and, if the lamp is already on, reset the timing duration (ie-59...58...57...PING...59...58...)
Here's the basic basic version -
//created by Will Rogers, June 14, 2011
//Motion Detector Relay Control
//using (2) Zilog ePIRs in hardware mode, pro mini, & relay protoboard from SparkFun
const int relayPin = 13;
const int mdPinA = 14; //pinA0
const int mdPinB = 15;// pinA1
const int delayPot = 16; //pinA2 - used for switching "ON" time for debugging
int mdValA = 1023;//analog approx. HIGH
int mdValB = 1023;
int delayVal = 0;
int testDelay = 5000; //test 5 seconds
int calibrateCount = 10;//count to 10 before LOOP
void setup() {
Â
 pinMode(relayPin, OUTPUT);
 pinMode(mdPinA, INPUT);
 pinMode(mdPinB, INPUT);
 pinMode(delayPot, INPUT);
Â
 digitalWrite(relayPin, LOW);//make sure relay is OFF
Â
 for (int i = 0; i < calibrateCount; i++){
 delay(1000);//v. inelegant
 } //allow the sensor to calibrate
}
void loop() {
Â
 delayVal = analogRead(delayPot);
 int delayTime = map(delayVal, 0, 1000, 0, 3); // 10s, 30s, 1min, 5mins
 mdValA = analogRead(mdPinA);//hallway
 mdValB = analogRead(mdPinB);//entry
Â
 if (mdValA <= 205 ) {
  digitalWrite(relayPin, HIGH);
 Â
  //all of this could go away if I could just restart a 60-second countdown if there is further motion
  switch(delayTime) {
   case 0:
    delay(10000);
    break;
   case 1:
    delay(30000);
    break;
   case 2:
    delay(60000);
    break;
   case 3:
    delay(300000);
    break;
  }
  //delay(testDelay);
  digitalWrite(relayPin, LOW);
 } else if (mdValB <= 205) {
  digitalWrite(relayPin, HIGH);
  switch(delayTime) {
   case 0:
    delay(10000);
    break;
   case 1:
    delay(30000);
    break;
   case 2:
    delay(60000);
    break;
   case 3:
    delay(300000);
    break;
  }
 } else {
  digitalWrite(relayPin, LOW);
  delay(500);
 }
}
Would attachInterrupt() be the way to go or tripTime = millis(); currentTime = millis(); if(currentTime-tripTime <= duration);...