Home Security System

When leaving home you have to switch HomeSecuritySystem via home terminal ( keyboard 4x4 + LCD 20*4) to status ,,Not at home,,. This should be done by pressing some key or key combination on keyboard. Then you have 2 minutes to leave home.
When you return home then one of three PIR sensors or one of three door sensors is triggered and then timeout of 2 minutes should start.
Person who entered house is able to stop this timeout and switch HomeSecuritySystem into ,,At home,, status. This must be done by entering correct pin number confirmed with hashtag (or another key).
When someone return home and do not enter pin number in two minutes then system sends sms via GSM module to my mobile.

I have two entrances to house so I would like to have two terminals where I can operate HomeSecuritySystem.

Expected basic components:
Arduino MEGA 2560
Power supply adapter
PIR sensor (HC-SR501) 3x
Door sensor 3x
LCD 20/4 2x
4*4 Keypad 2x
Wiring I have prepared in walls of my house.

Can someone help me with basic structure of the program? I do not know when to start because I do not know how to make something running as soon as some of the sensors is triggered. It can be triggered only for 0,5sec for example and program must be prepared and see that sensor was triggered no matter how long time.

You need to start by doing some simple stuff - look at digital inputs in the examples and how the program can be made to look for the digital signal , a loop looking at the input can still see rapid changes .
Bear in mind too most commercial IR sensors stay energised for a second or so.
If you still want faster , have a look at interrupts and how you can use that with a digital input .

Do you have any Arduino or C/C++ programming experience?

If not and you already have the Arduino please get a tutorial and go through it - I think they gave one here and Sparkfun has one you can download I think - get a few LED’s, resistors, switches etc so you can go through the tutorial - that will help you to understand any guidance you are given here in the forum. Also by the time you finish the tutorial you will probably have a good idea on how to start your program

Good luck!!

I always download sketches made by another people and then I adjust them to fit my purpose. I have arduino based 3D printer (but this is easy to do, it is complete solution), I did some temperature data logger based on information from some other projects, I tried some very simple things with Arduino starter kits.
I remember something about C programming from my Electro-technical university study but these are very basics.
I know that this my program will probably have something to do with ,Interupts, - I will have a look at this topic since I do not know how to use them.

You want to learn the "do many things at once" lesson.

Simplest form is the IDE Example sketch BlinkWithoutDelay, those examples are beginner material so figure out how devilishly hard it must be -- the lesson itself is subtle.

From Gammon Forum : Electronics : Microprocessors : How to do multiple things at once ... like cook bacon and eggs

Now a seasoned cook would NOT do this:

Put coffee on. Stare at watch until 1 minute has elapsed. Pour coffee.
Cook bacon. Stare at watch until 2 minutes have elapsed. Serve bacon.
Fry eggs. Stare at watch until 3 minutes have elapsed. Serve eggs.

The flaw in this is that whichever way you do it, something is going to be cooked too early (and get cold).

In computer terminology this is blocking. That is, you don't do anything else until the one task at hand is over.

What you are likely to do is this:

Start frying eggs. Look at watch and note the time.
Glance at watch from time to time. When one minute is up then ...
Start cooking bacon. Look at watch and note the time.
Glance at watch from time to time. When another minute is up then ...
Put coffee on. Look at watch and note the time.
When 3 minutes are up, everything is cooked. Serve it all up.

In computer terminology this is non-blocking. That is, keep doing other things while you wait for time to be up.

That is preceeded with commonsense explanation of using delay() to blink leds only going so far and it is followed by code and explanations of techniques used.

The blocking vs non-blocking code applies to how many cycles a process might HOG while say processing a large array before checking sensors, answer to that is process the array an element at a time during consecutive passes through void loop() which allows sensor checks frequent enough to be responsive enough to meet needs.

Learn this and state machines and you won't need unnecessary interrupts, save those for real needs like perhaps software serial.

This is perfect example! Thank you very much for your support! :art:

Before you go buying a lot of parts, learn what you can do because your parts list may change. Like, are you sure you need a Mega2560? There are chips we call "pin multipliers" and there are sensors that can detect taps -through- a door (keypad can be attacked) and other amazing things.

I have a small series of non-blocking examples that can all work and play well together in a sketch, they include button (input) and led (output) utilities and a loop counter to show just how fast the sketch is running, it's kind of a health meter. You want, I post.

It's a matter of view, literacy and PRACTICE to get fairly good at automating with AVR's.

Sklipky:
This is perfect example! Thank you very much for your support! :art:

If you want this really or C or anything, you have to do it over a number of times, different ways to get your brain and reflexes to grow connections, short term becoming long term. You become as good as your practice. Practice makes perfect. When you write things down it forces you to think then how much more with computer code? Well, at least by the time it's been debugged, LOL! Lot of thinking by then!

C is important to know with small memory environments and specialized hardware. It can be very close to the metal, right down to chip registers by name and pins by bit. But you gotta know bit lingo to use the best register voodoo, shifts, masks and bitwise logic. So pursue with C and before you get into any big projects, learn about C++ classes and object-oriented code. Just do not use dynamic allocation on Arduino and you will have what you need to write proper libraries and much bigger, better projects just because it organizes so well.

Sklipky:
I know that this my program will probably have something to do with "Interrupts" - I will have a look at this topic since I do not know how to use them.

No, actually your own program - code - will have essentially nothing at all to do with interrupts, so clearly you have very little programming experience. :grinning:

You will of course be using interrupts but in a very roundabout fashion. You have been advised to study and understand how to use "state machine" code in the "do many things at once" concept. Timing is performed by repeatedly monitoring the value of the millis() function and comparing it to set points; the counter that is inside millis() is implemented using timer interrupts but that code is already debugged and (presumably) optimised so you need to have no further interaction with those interrupts.

GoForSmoke:
Before you go buying a lot of parts, learn what you can do because your parts list may change. Like, are you sure you need a Mega2560?

It's a matter of view, literacy and PRACTICE to get fairly good at automating with AVR's.

I wanted to use this board and other somponents since I have them in my drawer. I know that it might change.

I will start with some basic coding then and I will see if I am able to make program according to my wishes.
When I was doing electrical installation in my house I prepared at least 2 cables J-Y(St)Y 4x2x0,8 for each of two terminals. Which means that in terminal will be only One Keyboard and One Display 20/4. Arduino itself will be hidden somewhere in house. Robbers cannot fint Arduino in 60 seconds (or another set up time) to stop it from sending me sms with alert.

Arduino company prepared Example Sketches to show features of the hardware and code and some to show C structures and basic code techniques with those. Even if you know C well it would pay to shoot through sections 1 to 3 for many things microcontroller. Section 5 covers some basic logic structures and arrays.. yes we have people writing days-long projects without knowing arrays and loops so that we have to get to unlearn as well as learn to make progress. So you have sections 1,2,3 and 5 as be sure to cover and section 4 that teaches a BAD HABIT for microcontrollers, the use of C++ String objects that limit what can be achieved on these chips. Avoid section 4 or figure that some day you will be getting told not to use String in Arduino code even if the little examples do work.

Those Examples are in your IDE and have pages on the Arduino site linked by the forum RESOURCES pulldown menu and TUTORIALS choice gets you to a links page with FOUNDATIONS and HACKING page links as well as TUTORIAL EXAMPLES link to the pages for those sections 1,2,3 and 5. FOUNDATIONS shows deeper stuff; variables, pins, etc and HACKING points out things to try and do.

Last I worked GSM code for someone the SIM900 library took 37 seconds to link up and start a data transfer during which it blocked everything else from running. In that case it was for a greenhouse to send data every 10 or 15 minutes up to web, 37 seconds is tolerable but if it was my place I would have given GMS a dedicated board (like Nano) or chip so it could go block itself without holding up the rest from running.

I think that this is also good example: