Go Down

Topic: Alarm system (Read 404 times) previous topic - next topic

JochemHam

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?


Nick Gammon


JochemHam

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.

PaulS

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.

HazardsMind

You need a coded latch.

byte latch = 0;

if(digitalRead(sensorPin) == HIGH) {

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

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

}
Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Go Up