Hi - I'm new to Arduino and C++, so forgive me if this is dumb.
What I'd like to do:
I would like to use a momentary switch (it's an NO key turn switch that only engages as long as you're turning it) to arm / disarm my alarm project.
I would like it to set an ARMED state if you turn the key for 2 seconds - only then would the motion detector start reading and all subsequent alerts after it goes HIGH.
To put it in a DISARMED state, you simply turn the key again for 2 seconds. In this state, it just sits there with the LCD displaying a message and waits to be armed. It works like a toggle switch.
The issue is that I'm not sure if I would make a function for each state (ARMED & DISARMED) and stuff the codes in there below the loop, then just have a conditional in the loop that listens on the key switch and sets the states. I also don't know how to make this momentary switch only engage a state after 2 seconds of continued signal state HIGH (and toggle back to disarmed after 2 more seconds engaged).
I was reading some things about edge detection, but before I start going crazy, I figured I'd ask better programmers what the best strategy would be.
Background on project (in case it helps):
I made a silent alarm system thing. It's an Arduino Nano that has an LCD display (2line/16pin), a motion sensor (hcsr501), SIM900, and an LED connected.
When it detects motion, the LCD displays that it's in an alarm state, an LED goes on, and the SIM900 sends me a text. Right now, the alarm state is only set to function for a couple seconds for testing. It has a serial readout too but I haven't done much with it yet.
Here's the code so far:
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(7,8);
char msg;
/* LCD params */
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int led = 9; // the pin that the LED is atteched to
int sensor = 6; // the pin that the sensor is atteched to
int state = LOW; // by default, no motion detected
int val = 0; // variable to store the sensor status (value)
void setup() {
pinMode(led, OUTPUT); // initalize LED as an output
pinMode(sensor, INPUT); // initialize sensor as an input
Serial.begin(9600); // initialize serial
mySerial.begin(19200); // initialize SIM900
/* LCD */
lcd.begin(16, 2);
lcd.print("Hello!");
lcd.setCursor(0, 1);
lcd.print("Unarmed");
}
void loop(){
val = digitalRead(sensor); // read sensor value
if (val == HIGH) { // check if the sensor is HIGH
digitalWrite(led, HIGH); // turn LED ON
delay(100); // delay 100 milliseconds
if (state == LOW) {
Serial.println("Motion detected!");
state = HIGH; // update variable state to HIGH
lcd.begin(16, 2);
lcd.print("Intrusion");
lcd.setCursor(0, 1);
lcd.print("Detected");
SendMessage();
}
}
else {
digitalWrite(led, LOW); // turn LED OFF
delay(200); // delay 200 milliseconds
if (state == HIGH){
Serial.println("Motion stopped!");
state = LOW; // update variable state to LOW
lcd.begin(16, 2);
lcd.print("Armed");
lcd.setCursor(0, 1);
lcd.print("Clear");
}
}
if (mySerial.available()>0)
Serial.write(mySerial.read());
}
void SendMessage()
{
mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000); // Delay of 1000 milli seconds or 1 second
mySerial.println("AT+CMGS=\"+529995117743\"\r"); // Replace x with mobile number
delay(1000);
mySerial.println("Intrusion detected!");// The SMS text you want to send
delay(100);
mySerial.println((char)26);// ASCII code of CTRL+Z
delay(1000);
}