Hello everyone, I need a big help.
I'm planning to make Arduino as a home alarm. I implemented: the keypad, RFID, PIR, RTC and finally the reed switch.
The problem is in the function that handles the reed switch. I thought, when the alarm had been triggered by keypad, to start a countdown as soon as the reed switch no longer felt a magnetic field (in other words when the door is opened). But as long as the countdown times out (10 seconds) I have to have the ability to disable the alarm. If I could not turn it off within 10 seconds alarm is triggered. How could I do?
I leave the sketch below.
void reed_switch() //this is the concerned function
{
if(digitalRead(33)==HIGH && activation==true && flag3==false)
{
int i=10;
flag3=true;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("PORTA APERTA!!!");
delay(2000);
//lcd.clear();
//here i would create and run a contdown
//how could i do?
}
}
When the alarm is triggered assign the value of millis() to a variable, e.g. triggertime.
In your loop assign millis() to a variable "currentmillis" and constantly check this current value of millis() with the previously stored value.
If the subtraction of "currentmillis - triggertime" stays "< 10000", which means "<10sec", you can still switch off the alarm, if 10sec are reached, the alarm sirene or whatever event will start.
Learn to understand the "BlinkWithoutDelay" example in the tutorials and you are set to get your function into code.
this is a forum where you get supported when you put in some own effort.
We spend our free time for free but we expect some effort on your side.
Your code is still the same as in the first post.
Have a look at the example I mentioned and give it a try with the steps which I proposed; then repost your new code and we will see what still is missing.
If this is not the way you want to go, there is a special section in this forum where you can buy some help.
your code is pretty complex and not easily to read as I don' have any experience with most of the devices which you use.
I can only help you with a kind of pseudo sketch which might incorporate all of the events that could trigger or delete an alarm. This could be done with buttons which for example simulate the output of an alarm sensor and one button is dedicated to delete the alarm if it occurs within 10sec as you said.
If that sketch then works as expected, you can replace the pseudo functions and the buttons with your specific alarm functions/devices.
So to start with, it might be easier if you could shortly describe with your words (keep it short and organized) how your system should work.
I give you a start, so you can modify/extend it easily:
Include libraries (RTC, LCD, ... -> are already in your code)
Variables (const, floating -> what variables are needed for what purpose)
Timers (instead of delays; they keep the sketch flow and don't stop the execution; the timers will be continuously updated and used e.g. for the count down of the triggered alarm)
pls describe what events will trigger an alarm and how you want this event be handled (automatically or manually, with or without count down timer)
I think that will be enough to get the pseudo code done.
... without a bit more information on your side we won't get any further.
As said below, we spend our free time to help you to learn how to fish. We won't fish for you ...
A bit more for you:
triggertime just appears in one line in your code "out of the dust" - you have to declare it first, and give it the millis() value when your event - I suspect "door open" - happens
what about the error codes which you get
as this sketch is way above "newbie-level", why don't you break your project down on a step by step method starting with the method I described before
ok, I see; was my fault as my PC had a strange behaviour - so after restart it downloaded the right file and I can see your comments now.
declare currentmillis and triggertime as "unsigned long" as millis() starts from the boot up and can be a big number
follow @aarg's advice: the if condition is without action ...
maybe you switch on an LED, as long as the result of the condition is < 10000 to see if the condition was met at all
i declared as unsigned long triggertime and currentime as global variables and when in the "if" conditions (currentmillis-triggertime)<10000) is true it activates a LED; if false the alarm is triggered.
If your alarm is ON while the condition is not true - that was exactly what you wanted.
When the condition is false, the 10sec have been expired (currentmillis-triggertime >= 10sec) and then the alarm should be ON, that's what you said in your initial post.
There isn't much to understand about millis(). It's just like a running clock. You read the clock and compare times so you can decide when to do things. Not complicated at all.