Home Security System - Interrupt Timer Function.

Good afternoon,
I am working with an Arduino Uno currently to make a home security system. I am using version 1.06 IDE for the older compiler for the coding.
There is one small detail that is keeping me from finishing the project; the single thread that the program is able to run, or the lack of multi-threading to be precise.

I want to make a function that starts some sort of timer, checks how many seconds has passed, or uses real time data to decide if 60 seconds has passed. If a key press is not detected in the allotted time then it will trigger a relay to start a very loud siren.

What have you lot done, and what would be your best advice to tackle this situation?

I have set up an event listener for the keypad but I am not sure If I can use that to interrupt a function loop in any way.

I have been working on different solutions the past week trying to figure this out. I have tried a loop that has a delay in it and that does not work because I still need the arduino to function during the loop.

Before I came to arduino I was a C# dev, so losing the ability to multi-thread has slowed down my progress in these matters.

I appreciate any advice you can offer me,
Andrew

Look at the Blink Without Delay example for some inspiration. You get a function called millis() that you can call and it will give you the number of milliseconds since the board was last reset. When an action happens where you want to start a timer, record that number in a variable. Then on each pass of loop you can check that time against the current time returned by millis() to see how long it has been since that action.

That you Delta_G!!! This is exactly what I am looking for. Thank you, I will update you when I get it implemented and I can share the code if people want it.

Do you know what is going on behind the scenes for this one? How is the millis() function working or where can I find it in the file system? I like to know what code I am running, or I will add to the code to make it fit what I need.

You apparently do not understand what interrupts are. What you do not understand is best avoided most strictly. :grinning:

The millis() function actually uses interrupts (so you have no need to :grinning: ). The timer counts clocks from the crystal and updates an internal counter which is read by calling millis(). It tells you how many milliseconds have elapsed since start-up. It is an unsigned long and so takes several days to roll over.

So to "start your timer", you simply read millis() and store it in a(n unsigned long) variable. There is no need whatsoever to interfere with the workings of millis(); you actually have no need even to know how it operates at all.

Each time the code in loop() cycles, you read millis() and compare the value to that start time plus the interval (an unsigned long) for which you wish to wait. If it has not passed that time, you move on through the loop(), but as soon as the specified time has passed, you act upon it and either set a new event time by again storing the present millis() value, or set a switch to no longer perform the timing check as you pass through the loop().

The "threads" you bemoan are simply successive steps in the loop(), each of which makes decisions according to state switches without looping or waiting. In effect, the threads are turned inside out at the point of any process switch (which of course, includes "wait" or "sleep" primitives).

I would offer that posting your code would help.
you can rename it as a TXT file and add that to your post, or read 'how to use this forum'
to post is in the body of your message, please, PLEASE do NOT post it as text in a message.

how it works is simple.

you have a trigger.
say a motion sensor.

if (sensor goes high)
THEN = millis()

now you start counting

duration = millis() - then

pretty simple so far ?

if (duration > 60 seconds)
alarm_horn = 'blast their ear drums !'

but... you have to actually test if the alarm has already tripped.

if (ARMED == HIGH)
if (alarmSatusReady == LOW){
if (sensor goes high){
alarmSatusReady = HIGH
THEN = millis()
}
}
}

duration = millis() - then

if (duration > 60 seconds)
alarm_horn = 'blast their ear drums !'

if (acknowledgement = HIGH)
armed = LOW
if (alarmSatusReady == HIGH){

HAHA! well said Dave. I like your interpretation. I will be implementing this tonight hopefully.

I am not content just using a piece of code that I do not understand, Paul__B. I do very much appreciate your input though it is quite helpful. So the millis() fnuction is actually implementing an interrupt. I would like to know were I can look at the millis() code if you know where it is at, it has to be saved to the hard drive somewhere or arduino IDE could not use it. unless it is just packed into the IDE itself.

Thanks again to all that chipped in,
Andrew

agluck:
I would like to know were I can look at the millis() code if you know where it is at, it has to be saved to the hard drive somewhere or Arduino IDE could not use it. unless it is just packed into the IDE itself.

Fair enough, just as long as you do not have any grandiose ideas about altering things. :grinning:

You will find it in /usr/share/arduino/hardware/arduino/cores/arduino/wiring.c (or /usr/share/arduino-1.6.6/hardware/arduino/avr/cores/arduino/wiring.c for version 1.6.6).

Paul__B:
Fair enough, just as long as you do not have any grandiose ideas about altering things. :grinning:

You will find it in /usr/share/arduino/hardware/arduino/cores/arduino/wiring.c (or /usr/share/arduino-1.6.6/hardware/arduino/avr/cores/arduino/wiring.c for version 1.6.6).

Thank you very much, I do not plan on changing it yet. :wink:
I can use this to not only understand Arduino a bit more but I can learn better programming practices.

I hope to upgrade eventually to just programming AVRs directly without the Arduino boot loader. But I need to understand these chips better first. Programming them is considerably different than computers. (Unless I just don't have my mind shifted to it right yet)

Picking apart the .NET framework as I used it helped me as a .NET developer more than anything else did. I got a great understanding of how the framework functioned at its core. Classes and books obviously helpped but they never dove into why it works. I started programming in high school, but did not really get into it deeper until I was 22. I am 24 now. So I am still pretty fresh on the scene.