lots of things going on at once home automation

I'm planning an Arduino home automation project where I'm going to be monitoring lots of inputs (things like magnetic door sensors, push buttons, temperature sensors, light sensors, the time itself, a PIR etc) and performing certain actions based on their input or state.

How do I program most effectively for this so that multiple events can happen simultaneously?

Like if I want it to be possible for the Arduino to monitor button 1 and when button 1 is pressed servo 1 is activated for 30 seconds. And WHILST servo 1 is doing its thing, someone presses button 2 and the Arduino concurrently to operating servo 1 activates servo 2 too?

Is it possible to deal with such scenarios? If so, how best?

I've already touched on this in this post - but I'm still not totally convinced I understand...:)

THANKS

Yes it's perfectly possible. Look at the 'blink without delay' example sketch.

You might also get some ideas from the "help me with a loop" thread. The code version that I just posted there is single-threaded to suit the problem, but the version I'm using for my own project maintains a schedule queue to handle exactly the kind of multiple process situation you describe.

Search the forums for "state machine". Many of the examples are concerned with single state variables, whereas you'll need several by the sound of it, but the basic technique is the same and should show you how to proceed.

It depends on your idea of simultaneous, how many things, how much you want it to do.

Arduino can watch one sensor many 1000 times a second and still have time to act. There is a good bit of space for program and tables but not much RAM. Good news is that you can connect more memory and even small controllers and other chips to expand one for not a lot of money if you do it yourself. Are you handy with tools and soldering?

GoForSmoke: It depends on your idea of simultaneous, how many things, how much you want it to do.

Arduino can watch one sensor many 1000 times a second and still have time to act.

I think this is where my understanding of Arduino and thus how I phrase my question leads to me getting this kind of response "the arduino can do X many thousands of times a second". :-)

That's great - so in answer to my question, yes, the Arduino can easily monitor many inputs and control several LEDs doing different things at once.

THIS sounds very useful and a great answer to my question. Thanks. I think you understood what I meant, here.

dxw00d: Yes it's perfectly possible. Look at the 'blink without delay' example sketch.

Thanks, I'll do that.

DANE: How do I program most effectively for this so that multiple events can happen simultaneously?

The technique is demonstrated in the 'blink without delay' example but isn't explained anywhere as far as I know.

The basic approach is that your loop() function runs repeatedly. Each time it runs, it looks for events that it needs to respond to.

For example, if you have something that needs to be done at a particular time, or repeatedly after a particular interval, the sketch can check the current time against the due time to decide whether it is time to do anything.

Similarly, if you have actions that need to be taken based on inputs being in a particular state or changing state, you can read the current input, detect changes by comparing with the previous state, apply whatever debounce algorithm you want, and detect whether your input event has happened.

Similarly you can test whether there is any input available on whatever serial streams you're using, or on radio devices etc.

Because your sketch is designed for poll for each of these events rather than stop wait for them to happen, you can take any number of chunks of 'polling' code and stick them into the same loop() and do them all independently.

When you detect and event that needs to be handled, you do whatever needs doing and then drop out of the loop().

If some of your actions can't be completed immediately, for example "turn LED on for a second" then you design the "turn LED on" and "turn LED off a second later" as two separate events.

Sorry that this explanation probably isn't terribly clear, but that's the underlying approach you are being advised to take when people steer you towards 'blink without delay'. It enables your sketch to do lots of things independently.

I don't how close to simultaneous you want. Realize that when you are watching and doing real world things that sometimes sensors get falsed so it's better to read several times in a row to be sure of a positive than 1 tick and call it data. With keys or buttons it is called 'debounce'. It is very real in serious applications. Being able to do that before anyone notices with an unspecified number of other tasks going on may or may not fit your idea of simultaneous. That is why I try and relate how fast the Arduino is. 16000000 cycles per second and a single analog read may take 100 or so. You switch from one analog pin to another, you do at least 1 read that you throw away while the circuit stabilizes then another read for data and maybe a few more.

Good news is you can use the Arduino board to program as many sub- or co- controllers as you need to add items and tasks. You can make it wireless, you can add SD card adapter for big storage or use the on-chip EEPROM for small storage and report to PC on demand. With X10 you can use your house wiring for power and communication, switch outlets on and off to run motors and lights all without running wire or transmitting radio.

Maybe your house should know it is you and adjust lights and chairs to match your preferences even before you sit. Buttons are nice but for many things you should not have to touch a button. Mitsubishi Labs has a white paper out, how they make TV remotes that turn on when they are picked up or waved around and adjust their own screen brightness by how bright the room is. Should your house be less smart?

Any time you run out of pins or processing power, add another controller to handle the new tasks.

What you should be concerned with is if you can work with the Arduino environment.

Don't get too bogged down in the simultaneous question - compared to human reaction time and the response time needed for home automation tasks, the Arduino is blisteringly fast, epically fast - it just isn't an issue, as long as you never ever use delay() ;)

Thanks wildbill.

I guess what I was concerned about was that my project would not be able to have a servo controlling a bell tinkling for 20seconds and then start another ringing at the 10s mark, keep it ringing for 20s, thus finishing to tinkle 10s after the first one.

I have been suitably reassured. :slight_smile:

my project would not be able to have a servo controlling a bell tinkling for 20seconds and then start another ringing at the 10s mark, keep it ringing for 20s, thus finishing to tinkle 10s after the first one.

Raise your expectations a bit, please. :grin:

Well, if you need more program and memory then how about Teensy++? http://www.pjrc.com/teensy/index.html

DANE:
Thanks wildbill.

I guess what I was concerned about was that my project would not be able to have a servo controlling a bell tinkling for 20seconds and then start another ringing at the 10s mark, keep it ringing for 20s, thus finishing to tinkle 10s after the first one.

I have been suitably reassured. :slight_smile:

As long as you don’t use delay()s to handle timing in your sketch, that shouldn’t be a problem at all.