Pages: [1]   Go Down
Author Topic: Alarm system  (Read 374 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 am working on a Alarm system with arduino but I can't figure out how to make it work exactly. Do any of you guys maby have an axample for me?

Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Is this related to this project?

http://arduino.cc/forum/index.php/topic,136004
Logged

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

Yes that is part of my project. But I can't figure it out. I got it in a simple version like this:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int sensorPin = 2;     
const int speakerPin =  9;
const int pitchPin = 0;
const int resetPin = 7;
int ledPin = 13;

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

void loop()
{
   if (digitalRead(sensorPin) == HIGH)
   {
   digitalWrite(ledPin, 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
  {
    digitalWrite(ledPin, LOW);
   lcd.init();
   lcd.backlight();
   lcd.print("Online");
  }
   
}


But now I need it to stay HIGH when I have pressed the button. Then stay HIGH till I press the button again. But I have seen several tutorials on the button switch with debounce but I can't get it to work with my system.
Logged

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

Quote
But now I need it to stay HIGH when I have pressed the button.
You need what to stay HIGH?

Debouncing is not your problem. That fact that you are not detecting transitions is. You need to know when the switch is pressed this time through loop, but was not pressed last time. Obviously, to do that you need a current state variable and a previous state variable. Set the previous state variable to the current state at the end of loop. Read the current state at the top of loop. Compare the current state to the previous state. When they are different, a transition has occurred, from pressed to released or from released to pressed). The current state tells you which way the transition went.
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3422
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need a coded latch.

byte latch = 0;

if(digitalRead(sensorPin) == HIGH) {

    ~latch; // latch = !latch; //Alternative

    latch ? (/* IF stuff here */) : (/* ELSE here */);

}
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Pages: [1]   Go Up
Jump to: