PIR Weird behavior

Hi guys, i wanna do a very simple program, when i activate a PIR sensor a Led must turn on and stay HIGH until i re-activate the PIR for the second time but i have some problems. This is the code:

int LEDpin = 13;
int PIRPin = 2; 
boolean motion = false;
void setup() {

    pinMode(PIRPin, INPUT);
    pinMode(LEDpin, OUTPUT);
    Serial.begin(9600);

}

void loop() {
    Serial.println(motion);

    if (digitalRead(PIRPin) == HIGH){
        if(motion) {

            motion = false;
        }
        else if (motion == false){

            motion = true;

        }
    }

    if (motion){
                digitalWrite(LEDpin, HIGH);

    }
    if (motion == false){

            digitalWrite(LEDpin, LOW);

    }
}

The idea is to set a boolean variable true when i activate the PIR and use it in order keep track of the last used. I use python out in order to "debug" the code (i print the boolean variable).

import serial
from time  import asctime
arduino = serial.Serial('/dev/tty.usbmodem411',9600)
while True:
            data =  (arduino.readline())
            data = data.strip()
            data = str(data)
            data = (data.split('b')[1])
            print(data)

I thought what i will see only 0s and 1s but sometimes i see a couple of 01010101 but no changes, i think that maybe the PIR is too fast to re-set the boolean variable and so Arduino doesn't know that is going on. I hope that this is understandable. Thank you.

P.S. By the way i'm using a Arduino One board

Francesco

Hi, welcome to the forum.

You should not check if the PIR is HIGH, but you should detect the moment that the PIR turns HIGH. That means you have to remember the previous state of the PIR and check for a change.

I did not verify the next code.

int previouspir = LOW;
boolean motion = false;

void setup()...

void loop()
{
  int pir = digitalRead(PIRpin);

  if( pir == HIGH and previouspir == LOW)
  {
    // The pir is high, but was previous low.
    // It just turned high.
    motion = !motion;          // true turns false, and false turns true.

    digitalWrite( LEDpin, motion ? HIGH : LOW);
  }
  else if( pir == LOW and previouspir == HIGH)
  {
    // the pir just turned low.
  }
  previouspir = pir;              // remember the state for the next time.

Thank you very much, but i don't understand what

    digitalWrite( LEDpin, motion ? HIGH : LOW);

do.

This part's called a ternary operator.....

motion ? HIGH : LOW

It checks the value of "motion", and if it's true, uses the first option (ie, HIGH), otherwise it gives the second, ie the LOW.

So it's a shorthand for this:

if (motion == true)
{
digitalWrite(LEDpin, HIGH);
}
else
{
digitalWrite(LEDpin, LOW);
}

Thank you Jimbo! Have a nice day :)

Ok guys the codes works, but there is a way to make the PIR "faster". I mean between the first activation and the second there is a delay, is it possible to reduce it? E.g

The PIR sees my hand and now it is HIGH but it's HIGH for something like 3/4 seconds so it cannot "see" nothing for 3/4 second.

I think that might depend on the hardware brand, but I'm not sure.... has it got any pots to adjust?

It has two thing that you can rotate but my PIR is the classic chinese so maybe as you wrote it's a hardware problem. Do you use some specific electronic's brand? Thanks in advance

Most have two orange potentiometers. One is the sensitivity, the other is how long the signal is kept high (or low).

Ok thank you guys