Monitor two sensors to do different tasks simultaneously ?

Hi all. I want to use two PIR sensors, basically if sensor #1 is tripped, I want to turn on light #1, delay for a while then fade off. if sensor #2 is tripped I want to turn on light #2, delay for a while then fade off.

Thinking about how I would program it, if sensor #1 has tripped and the arduino is in the delay stage of the code with the light on, it wont be able to run through the rest of the code and poll the other sensor pin to see if that has tripped also?

is there a simple way around this?

thanks XD

IMO delay() should never have been included in the processing language. Its a travesty and noobs can’t see past it. I have one Arduino (Duemilanove) monitoring 3 electric meters, 1 gas meter, 2 light sensors, 5 digital thermometers, blinking an led that varies by electric use, updates an LCD display every few seconds and outputs everything it knows by serial every 2 seconds, besides running the central heating in its spare time. Not a solitary delay in the whole sketch, because basically it wouldn’t work if there was.

Delay comes with AVR-GCC and it is useful in interfaces where a few miliseconds are needed before the next operation and millis would end up making the code more complex and not better.

Delay was never meant to be used for full seconds.

Not a solitary delay in the whole sketch, because basically it wouldn't work if there was.

my LCD library has this on it:

if (! (_displayfunction & LCD_8BITMODE)) {
    // this is according to the hitachi HD44780 datasheet
    // figure 24, pg 46

    // we start in 8bit mode, try to set 4 bit mode
    delayMicroseconds(4500); // wait min 4.1ms

    // second try
    delayMicroseconds(4500); // wait min 4.1ms
    // third go!

    // finally, set to 4-bit interface
  } else {

Not a single delay?

Thanks for the responses. I see what you mean about noobs getting stuck and set in the 'delay' way, this is the first out of a few simple projects that I won't be able to use it.

Thanks Nick for the link, looks good I will check it out

Hi again guys. I'm struggling here. I have cobbled together some code mainly from Nick's tutorial.

I'm trying to get the basics going for now, as I said I want to monitor two sensors but I'm just looking at one for now.

The aim of the code was to read a digital pin, and if that pin is high to set another digital pin high, and also store the time the event happened.
The i added another if statement, which would look at the millis since the event, and if they are equal or above 5000 to turn off the led.

It doesn't seem to work, the led just goes on and off when I change the pin state, but does not stay on for 5000 ms. I want it to work from the pulse output of a PIR so if the pin is low I don't want to turn off the led until the time has elapsed.

What am i doing wrong?

// Which pins are connected to which LED
const byte greenLED = 9;
const byte redLED = 10;

int const onTime = 5000;    //constant on time)

int val = 0;     // variable for reading the pin status

// Variable holding the timer value so far. One for each "Timer"
unsigned long left;
unsigned long right;

void setup () 
  pinMode (greenLED, OUTPUT);
  pinMode (redLED, OUTPUT);
  pinMode(2, INPUT);
  left = millis ();
  right = millis ();

}  // end of setup

void loop()

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

  if (val == HIGH)
    digitalWrite(9, HIGH);
    left = millis ();


  if (left >= 5000); 
    digitalWrite(9, LOW);


You have several problems; this line:

  if (left >= 5000);

has a semi colon that should not be there. The code after it gets executed regardless, so the led will be turned off every cycle round loop.

left is set to millis earlier, that means that once the semicolon issue is fixed, once the sketch has been running for 5 seconds the condition will always be true. Compare millis()-left to 5000 instead.

Lastly, while the input remains high, left gets reset on every trip around loop, so the 5s count only starts when the input goes low. Of course, this may be what you want.