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;
        lcd.print("PORTA APERTA!!!");       
                                         //here i would create and run a contdown
                                         //how could i do?

Antifurto.ino (8.82 KB)

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.

thanks for the help :slight_smile:

i tried it but without success >:( Could you please download my code and try to do it?

please help me :confused:


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.

I’m sorry for my request :confused:

this is my new code and obviously it doesn’t work.

Antifurto.ino (9.05 KB)


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:

  1. Include libraries (RTC, LCD, ... -> are already in your code)
  2. Variables (const, floating -> what variables are needed for what purpose)
  3. 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)
  4. 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.

i’m sorry again…

now it’s all explained in the sketch… anyway the alarm is only activated by keypad, and deactivated by keypad or by RFID

help me to create this countdown to complete my project please :slight_smile:

Antifurto.ino (9.05 KB)

now it’s all explained in the sketch…

I don’t see any explanation in there at all. There isn’t a single comment line in the whole thing.

... 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:

  1. 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
  2. what about the error codes which you get
  3. 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

sorry… i’ve uploaded the wrong file

Antifurto.ino (11.9 KB)

.. do you see any comments in your sketch - I don't, but maybe my 62years old eyes can't detect them anymore ..

you have to download the .ino file in my last post. i've just checked it out and there are comments in the sketch

This code block is empty:

        if((currentmillis-triggertime)<10000)                         //your suggested countdown, but it doesn't work.


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.

  1. declare currentmillis and triggertime as “unsigned long” as millis() starts from the boot up and can be a big number
  2. 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.

it still doesn’t work… any help?

So the "if condition" works as it lights the LED.

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.

So what exactly doesn't work now?

the countdown doesn’t work…

see the sketch

Antifurto.ino (12 KB)

Problem resolved using a countdown library... i've been searching it for days.

I don't understand a fucking thing with millis.... thanks to all

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.