Pages: [1]   Go Down
Author Topic: PIR -Sensor  (Read 494 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey guys,

I want to build an alarm system with arduino. It works with a PIR-Sensor. But if the sensor detects movement it only gives HIGH for like 30 seconds. I want it to stay HIGH untill I press a button. Do you guys have any idea how I should do this? I search everything but didn't found the right solution. I hope you guys can help me. This is my work so far.



#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int sensorPin = 2;     //The PIR-Sensor
const int speakerPin =  9;     
const int pitchPin = 0;
LiquidCrystal_I2C lcd(0x27,16,2);

void setup()
{     
  pinMode(sensorPin, INPUT);   
}

void loop()
{
  int val = digitalRead(sensorPin);
 
  if (val == HIGH)
  {     
    int frequentie = analogRead(pitchPin);
    int frequency = map(frequentie, 0, 1023, 100,5000);
    int duration = 250;
    tone(speakerPin, frequency, duration);
    delay(100);
    lcd.init();
    lcd.backlight();
    lcd.print("ALARM");
  }
  else{
  lcd.init();
  lcd.backlight();
  lcd.print("Centrale Online");
  }
   
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46266
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I want it to stay HIGH untill I press a button.
Slap that sensor around, and tell it behave, then.

Or, detect the transition of the sensor from LOW to HIGH, and forget about the transition back to LOW.

The detection of the reset switch is what should cause the flag, set when the PIR transitioned to HIGH, to be reset.

It is the state of that flag that determines what action to take, not the HIGH/LOW state of the PIR sensor.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes I know but I can't figure out how to let it go to high but not back to low. Maby you can give a code sample?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46266
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
yes I know but I can't figure out how to let it go to high but not back to low.
Stand in front of it and keep dancing.

You can't make the sensor stay HIGH. What you can do is detect when it goes HIGH, rather than whether it IS HIGH or LOW.

Code:
int prevState = LOW;
int currState;
bool screaming = false;

void loop()
{
   currState = digitalRead(pirPin);
   if(currState != prevState)
   {
      // Something moved (or quit moving)
      if(currState == HIGH)
      {
         // Aha, something moved. Scream until the reset key is pressed
         screaming = true;
      }
   }
   prevState = currState;

   int resetState = digitalRead(resetPin);
   if(resetState == HIGH)  // Or LOW, if you wired the switch that way
   {
      screaming = false;
   }

   if(screaming)
   {
      Scream();
   }
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok PaulS. Thank you so much. I will try to get it right this time. I understand the sample you send me so it should be fine.
tnx again!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am very sorry but I am still kinda noob with the language but how to I make the screaming group you described?

I want     
    int frequentie = analogRead(pitchPin);
    int frequency = map(frequentie, 0, 1023, 100,5000);
    int duration = 250;
    tone(speakerPin, frequency, duration);
    delay(100);
    lcd.init();
    lcd.backlight();
    lcd.print("ALARM");

this in the screaming group.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46266
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but how to I make the screaming group you described?
Code:
void Scream()
{
   // Put some code here
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey, I tryed to use your good advice but it doesn't work. Can someone see why?


#include <Wire.h>
#include <LiquidCrystal_I2C.h>
int calibrationTime = 30;
const int sensorPin = 2;     
const int speakerPin =  9;
const int pitchPin = 0;
const int resetPin = 7;
int prevState = LOW;
int currState;
bool screaming = false;

int resetState = digitalRead(resetPin);
LiquidCrystal_I2C lcd(0x27,16,2);

void setup()
{
  pinMode(resetPin, O
}

void Scream()
{
   int frequentie = analogRead(pitchPin);
   int frequency = map(frequentie, 0, 1023, 100,5000);
   int duration = 250;
   tone(speakerPin, frequency, duration);
   delay(100);
   lcd.init();
   lcd.backlight();
   lcd.print("ALARM");
}


void loop()
{
   currState = digitalRead(sensorPin);
   if(currState != prevState)
   {
      if(currState == HIGH)
      {
         screaming = true;
      }
   }
   prevState = currState;


   if(resetState == HIGH)  // Or LOW, if you wired the switch that way
   {
      screaming = false;
      lcd.init();
      lcd.backlight();
      lcd.print("...");
   }

   if(screaming)
   {
      Scream();
   }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46266
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
   if(resetState == HIGH)  // Or LOW, if you wired the switch that way
Probably be a good idea to actually read the reset switch, eh?

Reading it before setup(), init(), or loop() is a waste of time.

resetState does NOT have to be a global variable.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh I totally overlooked it. But it is still not working. I must be something wrong!

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
int calibrationTime = 30;
const int sensorPin = 2;     
const int speakerPin =  9;
const int pitchPin = 0;
const int resetPin = 7;
int prevState = LOW;
int currState;
bool screaming = false;

int resetState = digitalRead(resetPin);
LiquidCrystal_I2C lcd(0x27,16,2);

void setup()
{
  pinMode(resetPin, INPUT);
}

void Scream()
{
   int frequentie = analogRead(pitchPin);
   int frequency = map(frequentie, 0, 1023, 100,5000);
   int duration = 250;
   tone(speakerPin, frequency, duration);
   delay(100);
   lcd.init();
   lcd.backlight();
   lcd.print("ALARM");
}


void loop()
{
   currState = digitalRead(sensorPin);
   if(currState != prevState)
   {
      if(currState == HIGH)
      {
         screaming = true;
      }
   }
   prevState = currState;
   int resetState = digitalRead(resetPin);


   if(resetState == HIGH)  // Or LOW, if you wired the switch that way
   {
      screaming = false;
      lcd.init();
      lcd.backlight();
      lcd.print("...");
   }

   if(screaming)
   {
      Scream();
   }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46266
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I must be something wrong!
Yes. That code does something. You expect it to do something. What you want it to do is pretty clear. What it actually does is not.
Logged

Pages: [1]   Go Up
Jump to: