Mosquito Anti Loitering Device

many of the PIR sensors I see have an adjustable timer that resets after (the set) period of no motion.

a second adjustment would be the sensitivity.

so, the hardware setting would affect the code...

easy to detect if you have multiple motions in any given period of time (start a timer with motion, measure time to next motion, if time > desired time, do nothing, else make a big loud noise kind-of-thing)

So, after say 10 HIGH results with a delay of 3 seconds each, you'll know there was motion for 30 seconds - then you can sound your buzzer...

Which conditions should i use to code this? while loop?

Although your PIR hardware may not be identical, adafruit have a good tut here. In particular, they show

code which keeps the state of the PIR in a variable.

max1994:
So, after say 10 HIGH results with a delay of 3 seconds each, you'll know there was motion for 30 seconds - then you can sound your buzzer...

Which conditions should i use to code this? while loop?

Think along these lines maybe: Start with the adafruit code I linked. Start the clock with millis() when motion has just started. Then probably add a if (pirState == HIGH) inside the if (val == HIGH) and check if your desired interval has elapsed then fire the buzzer. (If you're inside that if, it would mean the PIR is high at the moment, but was already high when you got there, as distinct from having just gone high. (Untested logic, you'll need to verify I'm not talking crap, 5am here 8) ))

You should probably look at blink without delay to see how to use millis() in a way that gives a "delay" without it being a real delay() which would essentially pause your code.

Why is it when
if (pirState == low )
{
pirState == HIGH;
}

max1994:
Why is it when
if (pirState == low )
{
pirState == HIGH;
}

That looks like an attempt to test pirState and if it is set to LOW then set it to HIGH but someone got it wrong. Look carefully at the number of = in each statement in the code.

UKHeliBob:

max1994:
Why is it when
if (pirState == low )
{
pirState == HIGH;
}

That looks like an attempt to test pirState and if it is set to LOW then set it to HIGH but someone got it wrong. Look carefully at the number of = in each statement in the code.

No, the actual code is correct with only 1x = in the assignment; OP has mis-quoted.

He's asking about the actual logic, I think, and here's why it's like that:

At startup, the pirState is initialised as low:

int pirState = LOW;             // we start, assuming no motion detected

Each time through loop(), the pir is read into val:

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

Let's take the case of the first ever motion detection since power up: that val will be high, and the pirState will be low, so we're here:

val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
    if (pirState == LOW) {
      // we have just turned on <<<<<<<<<<<<<<<<<<<<<<<< we just got here

We are really only concerned with NEW motion, so we set pirState as high:

      Serial.println("Motion detected!");
      // We only want to print on the output change, not state
      pirState = HIGH;

That means, that next time through loop, which is running 150k times a second, val will still likely be high because there is still motion, but it's not NEW motion and since pirState is high because we just made it so, nothing happens.

I'll leave it to you to work through the logic of the reverse, when motion is no longer detected (val is low) but pirState is still high the very first time we detect motion has stopped.

edit: as PaulS pointed out in the mosquito thread, pirState is a crap name for that variable. It's more to do with the state of the motion, not the sensor itself- which is actually val- so a name with "motion" in it might be more apt. (Or is there such a word as "apter"?)

Come to think of it, val's a shit name too, maybe val should be renamed pirState and pirState should be named newMotion or something.

Hi guys , i need help with my anti loitering detection project. i am suppose to program a program that when my pir sensor sense a motion for more than 30 seconds continuously , and then the buzzer will sound. And the buzzer is not allow to sound,if the motion is sense less than 30 seconds. But the way i program it the buzzer sound after 30 Seconds, as long it got a HIGH from the pir sensor, even though the motion was not sense continously for 30 seconds. Can someone please show me a example

int ledPin = 13;//Declare the led pin number
int inPutPin = 2;//Declare the inputpin number
int SpeakerPin = 11;//Declare the speaker pin number
int freq = 16400;//The frequency output when HIGH
int Zero = 0;//The frequency output when LOW

void setup()
{
  pinMode(ledPin, OUTPUT);//Set the led as output
  pinMode(inPutPin, INPUT);//Set the inputpin as input
  pinMode(SpeakerPin, OUTPUT);//set the speaker pin as output
}

void loop()
{
  
  int Val = digitalRead(inPutPin);//Read input value
  //If 6 HIGH results with a delay of 5 seconds each,i will know there was a continous motion for 30sec, therefore the speaker will then give out the high frequency noise
  if (Val ==HIGH)//If val equal to HIGH 
   {
    delay(5000);//5seconds
   }
   
   else if (Val ==LOW)//If val equal to LOW 
 {      
   digitalWrite(ledPin, LOW);//Turn off led

 tone(SpeakerPin, Zero);
 }
     
   if (Val ==HIGH)//If val equal to HIGH 
   {    
    delay(5000);//5seconds
   }
   else if (Val ==LOW)//If val equal to LOW 
 {      
   digitalWrite(ledPin, LOW);//Turn off led

 tone(SpeakerPin, Zero);
 }
   if (Val ==HIGH)//If val equal to HIGH 
   {
    delay(5000);//5seconds
   }
   else if (Val ==LOW)//If val equal to LOW 
 {      
   digitalWrite(ledPin, LOW);//Turn off led

 tone(SpeakerPin, Zero);
 }
   if (Val ==HIGH)//If val equal to HIGH  
   {
    delay(5000);//5seconds
    
   }
   else if (Val ==LOW)//If val equal to LOW 
 {      
   digitalWrite(ledPin, LOW);//Turn off led

 tone(SpeakerPin, Zero);
 }
    
   if (Val ==HIGH)//If val equal to HIGH  
   {
    delay(5000);//5seconds
   }
   else if (Val ==LOW)//If val equal to LOW 
 {      
   digitalWrite(ledPin, LOW);//Turn off led

 tone(SpeakerPin, Zero);
 }
  if (Val ==HIGH)//If val equal to HIGH 
  {
   digitalWrite(ledPin, HIGH);//Turn on led
   
  tone(SpeakerPin, freq);
  delay(30000);
     }
     else if (Val ==LOW)//If val equal to LOW 
 {      
   digitalWrite(ledPin, LOW);//Turn off led

 tone(SpeakerPin, Zero);
 }
    

   }

why did you put this here?

if (Val ==HIGH)//If val equal to HIGH 
  {
    delay(5000);//5seconds
  }

you have to get all those delays out of there and look for state changes instead of just reading the pin every so often...

try this, which compiles but I could not test...

int ledPin = 13;//Declare the led pin number
int inPutPin = 2;//Declare the inputpin number
int SpeakerPin = 11;//Declare the speaker pin number
int freq = 16400;//The frequency output when HIGH
unsigned long alarmStart;
unsigned long timerStart;
int lastVal;
int alarmState;

void setup()
{
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
  pinMode(inPutPin, INPUT);
  pinMode(SpeakerPin, OUTPUT);
}

void loop()
{
  int Val = digitalRead(inPutPin);
  if (Val == HIGH) 
  {
    if (lastVal == LOW)
    {
      timerStart = millis();
    }
    if (millis() - timerStart >= 30000UL) //30 seconds continuous sensor
    {
      if (!alarmState)
      {
        alarmStart = millis();
        alarmState = 1;
      }
    }
  }
  else 
  {
    alarmState = 0;
  }
  lastVal = Val;
  soundAlarm();
}
  
void soundAlarm() 
{
  if (millis() - alarmStart <= 10000UL) // 10 seconds alarm time
  {
    tone(SpeakerPin, freq);
  }
  else
  {
    tone(SpeakerPin, 0);
  }
}

BulldogLowell:
you have to get all those delays out of there and look for state changes instead of just reading the pin every so often...

Deja vu anyone?- reminiscent of this thread, this one and this one.

The OP started out with the adafruit code which does indeed use states..... Both BulldogLowell and I have advocated the state change approach in those threads. BL used the phrase "classic state change" in one of them; dead right.

OP seems to have regressed from the adafruit code which was on the right track but abandoned that approach in favour of the current delay-bound one.

Thanks alot BulldogLowell, But your program isn't working. But still thanks for your help

max1994:
Thanks alot BulldogLowell, But your program isn't working. But still thanks for your help

so sprinkle in some Serial.print() debugging and tell us what you mean by "isn't working" , yeah?

int ledPin = 13;//Declare the led pin number
int inPutPin = 2;//Declare the inputpin number
int SpeakerPin = 11;//Declare the speaker pin number
int freq = 16400;//The frequency output when HIGH
unsigned long alarmStart;
unsigned long timerStart;
int lastVal;
int alarmState;

void setup()
{
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
  pinMode(inPutPin, INPUT);
  pinMode(SpeakerPin, OUTPUT);
}

void loop()
{
  int Val = digitalRead(inPutPin);
  if (Val == HIGH) 
  {
    if (lastVal == LOW)
    {
      Serial.println("timerStart begun... state change on PIR");
      timerStart = millis();
    }
    if (millis() - timerStart >= 30000UL) //30 seconds continuous sensor
    {
      if (!alarmState)
      {
        Serial.println("Alarm Started, timer indicated continuous HIGH for 30seconds");
        alarmStart = millis();
        alarmState = 1;
      }
    }
  }
  else 
  {
    alarmState = 0;
  }
  lastVal = Val;
  soundAlarm();
}
  
void soundAlarm() 
{
  if (millis() - alarmStart <= 10000UL) // 10 seconds alarm time
  {
    tone(SpeakerPin, freq);
    Serial.println("Speaker On");
  }
  else
  {
    tone(SpeakerPin, 0);
    Serial.println("Speaker OFF");
  }
}

If you'd have provided the compiler output data... Perhaps this might have solved or provided enough more data... That might just have solved your issues..
This

But your program isn't working.

does nothing for anyone to try to give you some constructive advice as to where you need to change something...
But I'm sure You've already read this
http://forum.arduino.cc/index.php?topic=97455.msg731293#msg731293
And obviously #6 cannot apply to you...
Mr Google is the best friend a programmer or wannabe programmer could ever have...
It appears that you've not had the good fortune to really meet him... Try it and make it a habit prior to posting here to use it...
You'll be amazed at what you can learn... when/If you are willing to learn.

Docedison

BulldogLowell:
so sprinkle in some Serial.print() debugging......

..... just like your original code in your first of the 4 threads on this subject had.

max1994:

Serial.println("Motion detected!");

Serial.println("Motion ended!");

i mean no sound came out from the buzzer, after motion detected for 30sec

max1994:
i mean no sound came out from the buzzer, after motion detected for 30sec

So have you got serial prints in the buzzer part like BL showed, so you can see if the code is actually going there in the first place?

BulldogLowell:

Serial.println("Speaker On");

Serial.println("Speaker OFF");

the speaker does not sound, after the pir sense the motion continuously for 30sec

max1994:
the speaker does not sound, after the pir sense the motion continuously for 30sec

Yes I got that from the reply before, and I'm trying to help you establish why that might be. If you have serial writes in the buzzer part of the code, you'll be able to see if the logic flow is correct. If the flow is going there but it's not sounding then that's one problem; if it's not going there in the first place it's another issue altogether.

Help us to help you.

But if you won't answer the question "So have you got serial prints in the buzzer part like BL showed.....", then I'm out (of this and the other 3 threads on the same question.)

JimboZA:
Help us to help you.

But if you won't answer the question "So have you got serial prints in the buzzer part like BL showed.....", then I'm out (of this and the other 3 threads on the same question.)

it would be nice if you could say

YES, something happened in the Serial Monitor... it printed....

or NO! nothing printed from the Serial Monitor.

At this point I'm with JimboZA, I am beginning to think that your problem is that there may be no power to the arduino!

BulldogLowell:
I am beginning to think that your problem is that there may be no power to the arduino!

Or as Roy in The IT crowd would say, "Have you tried turning it off and on again?"

Hi guys , i am doing a anti loitering project. My buzzer is suppose to sound, if my pir sensor sense a continuously motion(HIGH) for 15 times. and this function in my program is working.However i need the function to reset if the continously motion was stop inbetween the (HIGH)15 time. my problem is that when the pir sense a motion for 12 times and the person left, and then when the next person walk into the range of the sensor after a period of time, it start counting by 12,13,14,15 and then the buzzer sound. i need the sensor to count by 1 to 15 again.

int ledPin = 13;//Declare the led pin number
int inPutPin = 2;//Declare the inputpin number
int SpeakerPin = 11;//Declare the speaker pin number
int freq = 16400;//The frequency output when HIGH
int Zero = 0;//The frequency output when LOW
int x = 0;
void setup()
{
  pinMode(ledPin, OUTPUT);//Set the led as output
  pinMode(inPutPin, INPUT);//Set the inputpin as input
  pinMode(SpeakerPin, OUTPUT);//set the speaker pin as output
}

void loop()
{
  
  int Val = digitalRead(inPutPin);//Read input 
  digitalWrite(ledPin, Val);
  
   if (Val ==HIGH)//If val equal to HIGH  
   {
    delay(2000);//2seconds
      x++; 
    if (x%15==0)
    {
      tone(SpeakerPin,freq);
      delay(30000);
    }
   }
  
    else 
    {
      tone(SpeakerPin,Zero);
    }
}