multiple pir sensors

Yep its me again...

This time, this is the setup:

I want the two sensors to trigger the leds, left and right. I have writen some pretty steady code. But now the leds light up after each other and not at the same time, which is my goal :slight_smile:

My code is pretty simple and i think i understand why this is happening. But now i dont know how to fix it.

And this is the code until now:

// Sketch om met bewegingssensoren leds aan te spreken
// Michael Schouman
//

const int led1 = 9;
const int led2 = 11;

const int pir1 = 2;
const int pir2 = 3;

void setup()
{
pinMode(led1, OUTPUT);
pinMode(pir1, INPUT);
digitalWrite(pir1, LOW);

pinMode(led2, OUTPUT);
pinMode(pir2, INPUT);
digitalWrite(pir2, LOW);
}

int pinin = 0;
long countint = 0;

void loop()
{
// Eerste set
pinin = digitalRead(pir1);
while (pinin == 0) {
pinin = digitalRead(pir1);
}

  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(led1, fadeValue);         
  delay(100);
  }
  
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(led1, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);
  }

// Tweede set  
pinin = digitalRead(pir2);
while (pinin == 0) {
pinin = digitalRead(pir2);
}

  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(led2, fadeValue);         
  delay(100);
  }
  
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(led2, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);
  }  
  
}

As always, any help is awesome!

Thanks!

pinMode(pir1, INPUT);
digitalWrite(pir1, LOW);

All this does is turn off the internal pull-ups.
Is that what you intended?

Well thats so all is off on first boot right?

You haven't said what sort of PIR you're using.
You may need a pull-up.

while (digitalRead(pir2) == 0) {}

Simpler - gives the bugs fewer places to hide.

Oh haha sorry.

Im using the parallax PIR motion sensor.

http://www.parallax.com/Store/Sensors/ObjectDetection/tabid/176/CategoryID/51/List/0/Level/a/ProductID/83/Default.aspx?SortField=ProductName%2CProductName

OK, no apparent need for pull-ups on inputs, but the digitalWrite to disable them, because they're disabled by default at reset.

There's no way your program can light both LEDs at the same time - your program blocks each time it waits for the PIR input to go high, then blocks whilst the fade up and down occurs.

You need to look at the Blink-without-delay tutorial and understand it, and work out a way to allow you to look at the PIRs, one after the other, and interleave the two fades.

Well i understand the fact that the Adruino does a loop and does one thing after another.

Ok i dont really see the link between the blink without delay sketch and the sketch im working on.

What im staring at now are two leds that blink at the same time. But i really cant figure out how i can let two leds do their own thing.

Im sorry Groove this one is going over my head...

Ok i dont really see the link between the blink without delay sketch and the sketch im working on.

If you look at the blink example, you can see that nowhere does the "loop ()" block; there are no "delay()"s or polling loops ("while (digitalRead(x) == LOW) { }").

This means the processor is free to go off and do other things - just make sure those other things don't block too.

Google "finite state machine" - it's a lot simpler than it sounds.

It means you have to break down what you want to do into phases, or states.

If you look at your sketch, first you're waiting for a trigger. This would be the idle state.
When you get the trigger, you start ramping up the brightness of your LED. This is another state.
When your LED reaches maximum brightness, you start ramping down the brightness. Another state.
When your LED reaches minimum brightness, you go back to the idle state.

Well the Arduino halts everything when a delay command is given, right? So that is the problem in my sketch, it cant do anything because i gave it a delay command?

An example: uncompiled, incomplete and obviously untested, but I think it's got all the necessary bits.
To make it work for multiple devices, simply add an array of "struct".

// assumes "state" is initialised to "INIT"
// (INIT would normally == zero for simplicity)
// NB doesn't handle timer wrap-around.
switch (state)
{
  case INIT:
    brightness = 0;
    state = IDLE;
  break;

  case IDLE:
    if (digitalRead (PIRpin) == HIGH) {
      state = RAMP_UP;
    }
  break;

  case RAMP_UP:
    brightness = 0;
    brightnessIncrement = 5;
    nextAction = 20;
    next = millis() + nextAction;
    state = RAMPING_UP;
  break;

  case RAMPING_UP:
    if (millis () > next) {
      brightness += brightnessIncrement;
      if (brightness >= 255) {
        brightness = 255;
        state = RAMP_DOWN;
      }
      next += nextAction;
    }
  break;

  case RAMP_DOWN:
    brightness = 255;
    brightnessIncrement = -10;
    nextAction = 30;
    next = millis() + nextAction;
    state = RAMPING_DOWN;
  break;

  case RAMPING_DOWN:
    if (millis () > next) {
      brightness += brightnessIncrement;
      if (brightness <= 0) {
        brightness = 0;
        state = IDLE;  // or "INIT" for safety
      }
      next += nextAction;
    }
  break;

  default:
    //illegal state - should not happen.
  break;
}
analogWrite (LEDPin, brightness);

I feel so dumb to ask this, but i dont get the first line. What is state and what is init?

// assumes "state" is initialised to "INIT"

I guess those things were left as "an exercise for the reader"
:smiley:

enum yourState {INIT, IDLE, RAMP_UP, ....};
// defines a series of different values of type "int" 
// By default the first, "INIT" has the value zero. 
...
// now define a variable of type "yourState"
yourState state = INIT; 
....

I guess those things were left as "an exercise for the reader"

Haha, well i think so. Its not easy i have to tell you guys. I only have some webdevelopment skills, no more then that. :wink:

Ok, i have cheated on this subject. I hooked up 2 Arduinos...

Thanks for the help but this seemed really advanced and i have to admit my defeat.