Mosquito Anti Loitering Device

hi guys, i need help with my project. i am suppose to create a mosquito anti loitering device ,by using a Arduino Uno,Honey well Is-215T Passive Infrared Motion Sensor, and a a speaker. the speaker is suppose to send out a 16.4Khz noise which is only audible to Human at the age of 15-25,after the Is-215T infrared sensor have sense some one around it range for more than 30sec. i would like to know whether there are any mistake in my program. thanks!

int inputPin = 2; // declare the pir sensor pin number
int speakerPin =10; // declare the speaker pin number
int val =0; //variable for reading the pin status
int pirState =LOW; // assuming no motion detected
int ledPin = 13; // declare the led pin number
  void setup() 
  {
    pinMode(ledPin, OUTPUT); //set the LED as output
    pinMode(inputPin, INPUT);// set the sensor as input
    pinMode(speakerPin, OUTPUT);//set the speaker pin mode to output
    Serial.begin(9600);
  }
  
  void loop() 
  {
    val = digitalRead(inputPin);//read input value
    if (val == HIGH)//check if the input is HIGH
    {
      digitalWrite(ledPin, HIGH);//turn LED ON
      playTone(300, 16400);
      delay(30000);
  
      
      if (pirState == LOW)//we have just turned on
      {
        Serial.println("Motion detected!");//print as an ASCII-encoded label
        pirState = HIGH;
      }
      
    }
    else
    {
      digitalWrite(ledPin, LOW); //turn LED OFF
      playTone(0, 0);
      delay(300);
      if (pirState == HIGH)//we have just turn off
      {
        Serial.println("Motion ended!");//print as an ASCII-encoded label
        pirState = LOW;
     
    }
   }
  }
  void playTone(long duration, int freq)
  {
    duration *=1000;
    int period = (1.0/freq)* 1000000;
    long elapsed_time = 0;//elapsed time,the period of time 
    while (elapsed_time < duration)
    {
      digitalWrite(speakerPin, HIGH);
      delayMicroseconds(period / 2);// to find time High
      digitalWrite(speakerPin, LOW);
      delayMicroseconds(period / 2);//to find time High
    elapsed_time += (period);
    }
  }

The best way to see if your program has mistakes is:

  • Compile it: if it has compile errors then yes it has mistakes
  • If / when it compiles, run it: if it doesn't do what you want then yes it has mistakes.

It compiles for me, so no compile errors, but there is no way that anyone except you can say if it runs right.

BTW, please put your code in code tags. Go back to top post, choose Modify, select the code, and hit the # button above the :wink: :sweat_smile: smileys.

Then your code
will
{look like this}

As Jim said.
A member came up with this:

How did you confirm the sensitivity to a single Mosquito they are not large enough to be much of an IR emitter and to the best of my limited knowledge exothermic in growth or digestion of food...
IMO your test conditions are questionable... Better, I think to detect the noise generated by their wings in flight...

Doc

i would like to know whether there are any mistake in my program. thanks!

Of course there is. Pay attention to how you get values for pirState. Making shit up doesn't cut it.

Docedison:
How did you confirm the sensitivity to a single Mosquito they are not large enough to be much of an IR emitter and to the best of my limited knowledge exothermic in growth or digestion of food...
IMO your test conditions are questionable... Better, I think to detect the noise generated by their wings in flight...

Doc

Dear docedison, although the device is call mosquito anti loitering device, it has nothing to do with mosquito. It is a security control system use to prevent teenager to loiter around private property

It is a security control system use to prevent teenager to loiter around private property

Which will be broken, stolen, or ignored in nothing flat.

PaulS:

It is a security control system use to prevent teenager to loiter around private property

Which will be broken, stolen, or ignored in nothing flat.

Better to leave a rake or a lawnmower nearby. They seem to repel most teens.

BulldogLowell:

PaulS:

It is a security control system use to prevent teenager to loiter around private property

Which will be broken, stolen, or ignored in nothing flat.

Better to leave a rake or a lawnmower nearby. They seem to repel most teens.

Shit. Where did I put the Windex?

      if (pirState == LOW)//we have just turned on

Actually what you know is that the sensor was active when you checked 30 seconds ago. Was it active for a fraction of a second ? Is it still active ? Who knows ?

UKHeliBob:

      if (pirState == LOW)//we have just turned on

Actually what you know is that the sensor was active when you checked 30 seconds ago. Was it active for a fraction of a second ? Is it still active ? Who knows ?

Not so Paul, in this case... that pirState status test is inside a val=high "if"

(I think this is adafruit's code.)

what about high traffic areas where people will continue to pass by ?
it would need to sense a no-motion and then re-set to detect the gaps between people walking

If my 10 year old is any indication, you can set the 'no-motion' gap to about .01ms

Also, the constant sound would be annoying and quickly discovered. Think about variable timing.

Most professional "Perimeter denial" devices use a combination of both doppler radar and IR sensors...
I've designed Perimeter Denial devices with a 500 meter range using a 75 KG body mass as the doppler range and the IR function was for Hot bodies... like automobiles... or close body radiation
The Mosquito device looks like a short range device working under the assumption that radiated IR from a human sized "model" was close enough to exceed the normal ambient IR radiation of a hot day or night where the ambient radiation is greater than the ~ 86 deg average body radiation of a fully clothed body...

Doc

it would need to sense

What would? Look at the code. pirState is not assigned the value read from any pin. It is assigned true or false based on whim.

PaulS:

it would need to sense

It is assigned true or false based on whim.

No it's not: it's set when val is high, first time through. Cleared when val is low, first time through

No it's not: it's set when val is high, first time through. Cleared when val is low, first time through

The point is that it is NOT assigned a value based on reading a PIR sensor, so the name is patently stupid.

the speaker is suppose to send out a 16.4Khz noise which is only audible to Human at the age of 15-25

How does it discriminate against 0-14 year olds?

Hi guys, i am suppose to create a anti loitering alarm sensor.Does anyone know how should i code my programming so that my Speaker will only sound ,after my PIR sensor have sense a motion(HIGH) more than 30Sec. And if the motion was gone(LOW) in between the 30 sec the speaker will not sound

what have you tried?

Sorry I can't be of too much help, but this question has me wondering as well:

Can the PIR sensor detect motion for lengths of time? I think it only turns HIGH/LOW, so you would have to read the value from it multiple times over 30 seconds, and make sure it's HIGH every time.

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...

Edit: BulldogLowell's time difference recommendation is better, so never mind me. :blush: