Need assistance creating functions

I am building a complete hydroponics controller and, and have hit a road block in planning out how the plumbing is managed in my code. The project consists of an Ethermega connected to Blynk so I can run the sketch independently while forwarding sensor data to my smartphone Blynk app, and also have the ability to issue commands through the app that will be executed on the Arduino. To date, I have a single DHT22 sensor, a DS3231 RTC and an 8 channel relay to control AC~ devices. I accomplished merging my sketch into a Blynk sketch and have acquired networkability to my liking, and now I want to tackle the plumbing circuits and respective code.

Now I have an 8 peristaltic pump unit that I made by combining 2 four head pump units, and I will likely drive the respective 12V motors with Sparkfun TB6612 motor drivers, but also have on hand a bunch of TIP120 darlingtons if I find that they will work better in the project. I am yet to discover to absolute current draw of each motor, but I doubt any will ever encounter a stall. Also, over spin is unlikely, I also have flyback diodes to use if needed. Also, there will be any of 3 different water pumps (160 GPH), and roughly 13 valves that I still need to manufacture, but I intend to control them with push or pull type spring loaded solenoids.

About once every 7 to 10 days, I traditionally change out reservoir contents and rebuild. This includes emptying spent nutrient and water mixtures, adding in fresh Reverse Osmosis (RO) filtered water, and adding in the nutrients and supplements according to a recipe. Though I do intend to one day get these recipes saved in the code, for now I'd just like to get the motors turning when manually queued by the Blynk app. My holdup is that I don't yet know how to use functions that can be recalled at a later time. I also have difficulty identifying the means of spelling out arithmetic problems, or knowing how many variables I need to gather to put the needed data together to do the desired action.

I suppose this may be a tall request and likely too broad in scope to cover here, but if anyone can pitch in some good study leads or tutorials, I'd be very grateful!

My holdup is that I don't yet know how to use functions that can be recalled at a later time. I also have difficulty identifying the means of spelling out arithmetic problems, or knowing how many variables I need to gather to put the needed data together to do the desired action.

Not sure how you got everything else to work the way you want With blynk and sensors and asking that question...Best is probably to put your project aside for the time being and read/practice with a Arduino or C++ programming tutorial.

myggle:
Now I have an 8 peristaltic pump unit that I made by combining 2 four head pump units, and I will likely drive the respective 12V motors with Sparkfun TB6612 motor drivers, but also have on hand a bunch of TIP120 darlingtons if I find that they will work better in the project.

Quick tip: your TB6612 is a much better solution than TIP120 darlingtons. Use the TB6612's if you got em.

As for the code, you need to lookup tutorials on driving DC motors and general coding logic.

Yes, you always encounter stall. When the motor is starting from rest, the instantaneous current is the stall current. As the motor begins to rotate, the moving magnetic field opposes the incoming voltage so the current will drop. Depending on the inertia of the mechanical system, this high current may persist for 0.1-0.5 seconds, maybe longer.

A peristaltic pump doesn't have a lot of inertia, so you will be on the shorter end of that scale. You don't need a lot of overhead capacity in the motor-driver chip, but you do need some.

Thanks for the pointers. @J-M-L, I've been planning this project for well over a year, and it was the need to drive my current gear that forced me to learn the code I needed to know to make all of that work the way I wanted, and it does. I'd say I know a fair amount of the language, but as stated, I only learned that which I needed to know. I am now in that same situation where I am gathering more gear and need to know how to work it into my sketch and circuit.

@ Power_Broker, thanks a bunch for that tip. My concern was that the TB6612 sucks up an extra Dpin per every PWM pin used, plus also the fact I would only ever turn those motors the same direction lead me to be concerned about the unused output and back EMF. Still, I have 4 TB6612 chips which can drive the 8 motors, I was just hoping to reclaim those 8 digital pins. By gones!

MorganS:
Yes, you always encounter stall. When the motor is starting from rest, the instantaneous current is the stall current. As the motor begins to rotate, the moving magnetic field opposes the incoming voltage so the current will drop. Depending on the inertia of the mechanical system, this high current may persist for 0.1-0.5 seconds, maybe longer.

A peristaltic pump doesn't have a lot of inertia, so you will be on the shorter end of that scale. You don't need a lot of overhead capacity in the motor-driver chip, but you do need some.

Wow, this awesome feedback just keeps pouring in, thanks a bunch all and Merry Christmas definitely! I did not know motors encounter stall draws upon startup.

myggle:
@ Power_Broker, thanks a bunch for that tip. My concern was that the TB6612 sucks up an extra Dpin per every PWM pin used, plus also the fact I would only ever turn those motors the same direction lead me to be concerned about the unused output and back EMF. Still, I have 4 TB6612 chips which can drive the 8 motors, I was just hoping to reclaim those 8 digital pins. By gones!

To free up all those digital pins, you can just directly tie the motor direction pins to the supply rails - one to Arduino 5V and the other pin to GND. This works because you'll only be driving the motor one way. The polarities on the pins never change because you will never need them to.

If you're worried about back EMF, use throwback diodes across the motor. Using the motor drive, the back EMF won't mess too much with the Arduino since the motor is driven off a separate power source (GNDs connected, though).

Thanks! Can I solder the diodes right to the motor joints to simplify the breadboard circuit or would this be harmful in some way?

myggle:
Thanks! Can I solder the diodes right to the motor joints to simplify the breadboard circuit or would this be harmful in some way?

I think you'll be fine soldering them directly to the motor, but I could be wrong. Try googling it.

As far as software. The first thing you want to do is write a detailed user manual for an end user who doesn't know the system.

Write the entire user manual, how to work everything, how to clean out the pumps or what have you. Everything. Getting all this down on paper will force you to go through all the steps necessary to run your system and put you light years ahead on the path to straightening out your thinking.

Then worry about writing your code. At this point, you could even hand off the code to someone else if you wanted.

-jim lee

My holdup is that I don't yet know how to use functions that can be recalled at a later time.

I can't comprehend what this statement means. Can you word it differently or explain?

knowing how many variables I need to gather to put the needed data together to do the desired action.

I suspect you will find this out when you write the code.

  • Scotty

Scotty, I mean that I wish to write functions into the sketch that can be triggered to execute either at a certain time, or by way of the mobile app. Perhaps a list of functions can be made that Blynk can say "do thing A, or B".

Jim, I began doing this very thing a few hours ago. I don't yet know how to write pseudocode per say, but I am just outlining basic device functionality, known variable names and certain formulas I use manually to determine how much of a given nutrient I need to add to bring the solution to a desired Conductivity range expressed in PPM. Realizing the complexity of my sketch in relation to the overall project, I am solely focused the portions that directly relate to the new devices that I am yet to add. I feel previous accomplishments in this project might detract from what is now sought, but please correct me you feel I need to include all of it.

Initially, I made the sketch with just the RTC, then I picked at it till I learned how to make the RTC and the temp/hum sensor play nice. Once I got that down, I added in the 8 relays and programmed each to trigger via time or air condition. After that I added Blynk and that was a nightmare, but after roughly 3 solid months of trying many different things, I got it to allow for my main sketch to run its loop, pass on the gathered data from the RTC, DHT and pin states, while also allowing me to override the sketch and change the relay pin states, and that is where I am currently. I expect once I get passed the current hurdle, it will be another nightmare working all of the new code into the old sketch, but I gather that is the pains of learning. Despite knowing much less than I learned thus far, I knew shortly after conceiving this project that the "plumbing" aspect would prove to be the most difficult to get through, and now that Atlas Scientific manufactures the circuits and probes that I would likely otherwise need to learn to build, the plumbing is with no doubt the hardest part of this project, but I am none the less very determined to learn what I need to learn.

My version of pseudocode;

---Weekly Res Change---

The purpose of this, is to plan out how to write code that will gather variable values, and combined with known variable values calculate a recipe of nutrients and amendments/supplements to pump into a plumbing circuit. There are 8 dosing pumps for nutrients, a 160 GPH water pump in each of 3 reservoirs, and many yet to be built solenoid valves. Also, future planned project addons are PH and EC circuits from Atlas Scientific to also capture these variable values on Arduino in RealTime.

Devices;
• 8 - 12 volt peristaltic dosing pumps
• 13 – solenoids for valves yet to be built
• 3 120VAC~ water pumps (160 GPH)

Known Variable names;
• ml() = milliliters, value is received from Blynk through Ethernet Shield
• Gallons() will likely be 12, but should changeable via Blynk
• Part() is a coefficient used to derive the total number of milliliters per total number of Gallons(). Due to parts per million (PPM) reading caps, it has been found to add supplements first in ml/gal, but an EC reading is required to know the remaining margin of allowable PPM before the PPM cap is exceeded. This ensures the primary building blocks are still added in respective proportions, though likely less than the manufacturer’s recipe suggests.

Formulas;
• ml(xx.y) per gallon is ml() * Gallons()
• Gallons(XX) is the duration of time needed for a 160 GPH pump to produce (12) Gallons(). 160 GPH/60 minutes=2.66 gallons per minute, 12 Gallons/2.66=4.5 minutes or 270,000 milliseconds.
• Part(x.y) is x.yrecipe valueGallons(12). Recipe may call for 3 parts A, 2 parts B and 1 parts C. The Arduino needs to calculate the formula to know how to produce the correct amount from each peristaltic pump from A head, B head and C head. Each having a different recipe value, the same coefficient and the same number of Gallons().

Perhaps some arrays can be used to store the recipe values as there are only roughly 10 different weeks of recipes. Also, I believe arrays will be needed for the Arduino to properly plot out liquid movement circuits by choosing which pump shall pump, and which valves will open from the likely default position of normally closed. This gives me a means of keeping pipes and hoses to a minimum, all while maintaining controlled loops from a site back to itself through a testing rail, simply feeding the plant, or pumping all of the spent nutrients out the waste line.

What I'm saying is, don't think about code anything 'till you laid out everything at the end user level. To a user, code is just magic & mumbo-jumbo.

You understand what you want this machine to do. You should understand how you'd like the end user to control it. This is the level you need to get "right" at this point. This is actually your foundation. Once you have the manual, then you can think about pseudocode.

-jim lee