Total Farm / Aquatics Control Center Project Guidance

Hello All!

I have a pretty ambitions project that I have been expanding on and constantly adding too for about a year now, and I am hoping to find some assistance with 1) optimizing my code and 2) some thoughts on moving forward and creative ways that I can expand my automation to some of the new projects that I have coming up or start doing a SCADA integration.

My code is pastebinned here: TFACC - Pastebin.com

Ok so the layout is pretty much the start of things i guess:

Arduino Mega 2650, currently the ONLY controller in the project

I have an 8 chan 5 VDC relay board on the main control panel.

I have a remote junction box with an LCD backpack, 3) 12 VDC LED's, a MCPB switch, and a three position switch installed in.

Inside the remote JBox I also have a 16 chan relay board mounted and most of the terminations run to terminal blocks.

I also have another remote PB station, its an AB fiberglass outdoor explosion proof box with a MCPB in it and a 12 / 24 VDC sonalert

The 12 VDC source that I am using is just a cheap switching power supply from amazon.

D1307 RTC (battery eater)

The Current Automation Scope

What is does well: Timed functions work quite well. The system calculates the time of sunrise and sunset each day, Then calculates a bunch of other variables from those times. Then the controller runs functions based on those calculated times: For example, The chicken coop door is opened and closed by a 12 V 24 inch stroke linear actuator. At 1 hour after sunrise the door is opened and stays open all day. At 40 mins after sunset the door closes automatically locking the chickens away for the evening.

There is a 12 V controlled deer feeder that throws scratch feed for the chickens 15 mins before the door closes, gathering all the chickens and guineas right by the door so that we can usher them inside.

There is a 12 V sprayer pump that sprays cooling mist on the chicks and the goats everyday during the summer.

The 120VDC overhead lights in the barn are kept on every night to make sure the chickens receive a calculated 14 hours of light based on the number of daylight hours they get naturally and determines what time the lights should go off.

What it doesnt do or do well yet (alot of this could be wiring / hardware / has not been programmed.)
Scan time: My code needs a serious look over to determine if there are faster or easier ways to do things. Current loop time is almost 4 seconds. There is a 1K MS delay that is in the loop for the timealarms library but 4 seconds is a looooooog cycle.

It doesnt deal with DST and Non DST well. I have to reset the time on the thing every 6 months.

It does not have any sort of display / monitoring unless I tie in my laptop.

It does not do any measurement or alarming.

I am building a duck pond this weekend and i would really like to be able to monitor water conditions. I would like to be able to monitor the levels of food stores in the food bins and give me a signal or alarm when its getting low.

The duck pond and the filtration system monitoring is going to also be part of an aquaponics ebb and flow system I want to tie in. That is going to add some PID controls.

I know this thread is like super long. But I am just trying to lay it all out for any suggestions that yall might have.

The code is posted for anyone that might want to gaze over it: TFACC - Pastebin.com

Hi,

Can you post your code please, off forum sites are not friendly with some platforms.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:
PS If your code is too big to put in code tags, attach the ino file to a new post.

Hi Tom,

Thanks for the reply! I have been meaning to make a drawing in CAD. At least something functional and draw out the terminal connections. I will start chewing that elephant and have something on paper in the next couple days.

In the meantime, my INO file is attached. the code is ugly as sin and its pretty monsterous, but as I said, it works. even if the scan times are super long. Ive marked up the comments with as much info as I can to help explain what is what. hopefully it helps.

TFACC_adding_tone_logic_for_scratch.ino (19.4 KB)

Hi,
Thanks, yes just a basic diagram showing your layout of sensors and pumps and displays will be good.

Thanks.. Tom... :slight_smile:

For a project like this it probably makes sense to break it down into several sub-projects, each with their own controller.

Like one that takes care of the chickens (food, door, lights).

One that takes care of your aquaponics (reading the many sensors, dispensing fish food, dispensing the needed extra nutrients and other additives).

This way you will make maintenance easier, and probably make the programming easier as well.

Something else (just reading your code): I've heard before that fluorescent lights are pretty torturous to chickens as they can actually see the flicker (their eyes are much faster than ours).

Overall I don't see any great optimisations possible for this code. The only thing is that the indentation is not always correct - do a Ctrl-T in the IDE to automatically fix that.

how accurate do you need to be ?
can you check your time once a day ?

many serial periferials take time to read and then report.
in some cases, you can just send an alert
-get ready to report-
and it will do it's tyhing
then you cycle back...
-what did you find out?
and then get the data.

it is like being put on hold while the librarian goes to look up some information.

wvmarle:
For a project like this it probably makes sense to break it down into several sub-projects, each with their own controller.

Like one that takes care of the chickens (food, door, lights).

One that takes care of your aquaponics (reading the many sensors, dispensing fish food, dispensing the needed extra nutrients and other additives).

This way you will make maintenance easier, and probably make the programming easier as well.

I think this might be the ticket. Going with multiple controllers. The only thing that I didnt want to have to do is get two controllers, and 2 RTC's. But I am thinking that if I rethink my hardware deployment I can bifurcate the SCA and SCL signals to multiple controllers or use one as like a "stratum 1" and have the other controllers sync time to it.

dave-in-nj:
how accurate do you need to be ?
can you check your time once a day ?

Dave,

Reasonably accurate but not to the second or to even the minute. I am not doing anything scientific or like process measurement here, just opening and closing a door at some time shortly after sunset. At this point I cannot check the time unless I actually take my laptop out to the barn and hook up to a serial monitor. At some point Id love to get a modbus connection or some sort of data polling method so that I can write a simple SCADA monitoring method. but with my program running so long I am worried that I might be tapping the edges of the Mega's floating point math abilities.

What I might do, is tie in that LCD back pack, and just have it display the system time that way I can track accuracy

wvmarle:
Something else (just reading your code): I've heard before that fluorescent lights are pretty torturous to chickens as they can actually see the flicker (their eyes are much faster than ours).

Overall I don't see any great optimisations possible for this code. The only thing is that the indentation is not always correct - do a Ctrl-T in the IDE to automatically fix that.

I am going to look into the fluorescent thing, Ive been running this way for a while now and production never seems to drop off until the height of the summer (central Texas is brutal in July and August) but I get full egg production through the winter when many friends and neighbors fall off. But It would be an easy swap to change out the lighting to an LED system.

I did notice a MARKED improvement in the performance the past day since i added the serialDebug variable and made most of the serial print statements IF (serialDebug) statements.

The pushbutton response times are much much faster.

RTCs are pretty small and cheap. It's going to be much easier to add one to each set.

Furthermore, unless your chicken coop and your aquaponics are very close and you can install the two Arduinos at less than two meters from each other, I2C is pretty much out. Knowing the typical size of these installations I expect you're at much greater distance apart, which was also why I suggested multiple devices. Saves dealing with long wires on your sensors.