Any suggestions how to organize this?

I'm almost embarrassed to admit that I'm building yet another chicken coop automation project. This is my first project that involves multiple inputs an outputs. So far I've really only flashed LED's, faded RGB LED's, controlled servos, run OLED screen demos and stuff like that.

Just wondering how I should structure this. I have all the bits and pieces of code, I'm just not sure what the best way to organize it would be. Here are the basic blocks:

nRF24 listen/transmit (another arduino in the house will request info and send commands back to this one)

check LDR // there will be two LDR's with a north/south divider between them, equal values indicates noon
If sun is over head and door is closed, open it, toggle dooropen boolean, transmit door has been opened notification to command unit // We don't let them free range until around noon otherwise they lay eggs all over the yard
Else if no sunlight present and door open is true, close it, toggle door open boolean, transmit door has been closed notification to command unit

Check temp/humidity sensor
if temp > 70, run fan
else if temp < 0, turn on heat lamp (they were fine at -30 this winter so zero degrees is no problem 8))
else if temp <70 && temp > 0 turn off fan, turn off lamp

Actuate food dispenser (once daily or maybe manually via command unit for now)

My question is would multiple loops be advantageous here? It's going to be listening to the RF unit most of the time, and then checking the sensors maybe every five minutes.
Could I have a main loop that listens and counts roughly 5 minutes, jumps to a sensor check loop, and then jumps back to the listen loop? I am hoping to run this on solar power so low power consumption would be nice.

What I'd like to do with the command unit is request temp/door status, and open/close door manually. Maybe also have a "disco mode" button that lights it up with RGB LED's just for kicks :D. Kind of like the electric tree in the Lorax movie. Hopefully after I get some feedback I'll be able to post some code for critique.

Anyway, I would really appreciate any comments or observations you all might have. When this is done and there's a tutorial for it, make sure I don't miss any of you in the credits XD. Thanks!

If you need multiple timings, use the millis() instead of the delay().
There is famous example for that: http://arduino.cc/en/Tutorial/BlinkWithoutDelay

Are you using the nRF24L01+ ?
With this library ? NRF24: NRF24 library for Arduino

I would make functions for the sensors. Inside the functions it is possible to do some averaging or debouncing.
In the main loop() function I would collect all the information, and act upon that.

It is possible to check the sensors every 5 minutes. You can use millis() for that, to see if 5 minutes have passed. This is a simple solution.
It is also possible to continuously check the sensors, and remember when the door was openend or closed using millis(). So you can prevent the door being opened and closed every second. This needs more programming skills but has more possibilities. The data of the sensors is always available and the program decides what to do.

I have a sketch with millis() to create a 'tick' every second. I can do things now every second. I have a software counter to count to 60, so I can also do things every minute. And inside that a counter for every hour and inside that for every day (some things need to be updated only once a day). That is almost like a schedular running as a background task. All the important things, like webserver and communications are continuously tested in the loop() function without delay. Testing the millis() for a second 'tick' is just one of the things in the loop().

I do it similar to what Caltoa is saying. I just did a temperature monitor, logger with LCD display and it's programmed so that loop() mostly does nothing but keep checking if it should do anything. Some items rely on millis for timing and some rely on seconds. As it has an RTC and uses time.h I use the now() function just like millis(). In my sketch the busiest thing loop() does is turn the colon in the time display on or off every one second.

I'd just build a function to handle each subsystem & call them all unconditionally from loop. Let each of them keep track of time using millis to see if it's time to do something.

Unless you're using one of the sleep modes though, the Arduino is going to be running at full speed/power so you won't get much benefit power wise from doing things less frequently.

You can get the arduino to sleep, see Nick Gammon's site: Gammon Forum : Electronics : Microprocessors : Power saving techniques for microprocessors. This will introduce an additional complication because of your communication with the house unit. You may need to arrange external circuitry to wake the arduino when data transmission is detected and you'll likely need to arrange for the sending unit to send some discardable data to wake the receiver before sending real data.

I wrote an extended example using the blink without delay technique that illustrates how to manage several activities. It's attached to the first post in this Thread.

...R

Great stuff thanks guys!

Are you using the nRF24L01+ ?

Yes

With this library ? NRF24: NRF24 library for Arduino

I could, I have been studying maniacbug's stuff.

Posted on: Today at 11:29:21 amPosted by: wildbi
You may need to arrange external circuitry to wake the arduino when data transmission is detected and you'll likely need to arrange for the sending unit to send some discardable data to wake the receiver before sending real data.

Somewhere I did see that this RF unit had a sleep mode, now if I can just retrace my steps. So I would have to have two possibilities to wake it, signal on the RF or timer. I may have to consider putting an RTC on the command unit that will wake the field unit. The command unit will be plugged into the wall so power is not an issue.

I'd just build a function to handle each subsystem & call them all unconditionally from loop. Let each of them keep track of time using millis to see if it's time to do something.

Excellent, I am going to start there.

On a side note, how long before the IDE is replaced by http://www.wolframalpha.com/examples/? It'll be integrated into your home so you just say out loud "Computer program my arduino to blink an LED on pin 9 every five seconds" and it'll find the arduino and send it the .hex with no other input. I'm thinking we're close.