Pages: [1]   Go Down
Author Topic: Ph meter code check please  (Read 2320 times)
0 Members and 1 Guest are viewing this topic.
here inside my body
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys

I have searched this forum with lots of different terms relating to my project and found lots of helpful info. But no answers directly to my questions. I have seen hydro/grow/reef/aquarium controllers but none seen to be be what I am looking for/ don't get completed. I have read a few  books on the arduino and my mind goes off on tangents of what I can control. This arduino stuff is pretty cool as I am sure you are aware.

I am doing a hydroponic project (yes another one). I would like to make this project as cheap as possible. So that means making stuff myself. First I want to control ph. In hydroponics ph nutrient lockout is a major factor for healthy plants. If they do not get the right ph the plants will not take in the right nutrients. I will add other things to this project but baby steps for the first one.

What I am trying to accomplish is have an analogue input of a ph meter/probe. I plan on building this ph meter which is 12v and use a voltage divider http://blea.ch/wiki/index.php/PH_Meter . From that number (voltage) turn on a pump to dose a ph solution into the main tank of water/nutrient to receive the desired ph in the tank. I will fiddle with the values of the ph meter (0-1023) and the seconds on (delay) when I have the ph meter completed, also the pin numbers might not be correct. I have tried to apply the principals of KISS (keep it simple stupid). I need some of you geniuses to read and hopefully correct my code.

One quick question, I have read that after 9 hours and 32 min or something like that the arduino resets its self. I also read this has been fixed in version 15. can someone confirm or deny this.

Thanks in advance. FYI this is my first project on the arduino and also my first code for many years so I am a noob. Question are in the comments.

Code:
int phprobe = 1; //phprobe connected to pin 1
#define phpump = 3; //ph pump connected to pin 3 (should I use #define??)
int value; //value for reading. Do I need this???

void setup ()
{
pinmode(phprobe, INPUT); //sets ph probe as input
pinmode(phpump, OUTPUT); //sets ph pump as output
}

void loop()
{
{
int val = analogRead(phprobe, high); //read ph probe ON
int inputvariable = 0; //do i need this??
if (inputvariable <#); //variable more than number 0-1023(i dont know this # yet)
{
analogWrite(phpump, high); //turn ph pump on
delay (5000); //delay for 5 sec
analogWrite(phpump, low); //turn ph pump off
}
if else (inputvarible >#); //if varible is below ^
{
analogWrite(phpump, high); //turn ph pump on
delay (1000); //delay for 1 sec
analogWrite(phpump, low); //turn ph pump off
}
delay(300000) /delay 5 min before remeasuring. is there a better way???
}

After this I plan on a EC or Ppm meter/dosing section of code. I hope to put my competed project on here including code but that might be a bit of time.

Thanks again.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#define phpump = 3; //ph pump connected to pin 3 (should I use #define??)
Not this way, no.

There are two arguments to the #define function - the name and the value. In this example, the name is "phpump" and the value is "= 3;". Everywhere else that "phpump" occurs, the value will be substituted.

Code:
pinmode(phpump, OUTPUT); //sets ph pump as output
will become:
Code:
pinmode(= 3;, OUTPUT); //sets ph pump as output
Of course the compiler will complain about this.

Code:
int value; //value for reading. Do I need this???
No, you don't.

Code:
int inputvariable = 0; //do i need this??
No, you don't.

Code:
if (inputvariable <#); //variable more than number 0-1023(i dont know this # yet)
Its val that you want to be comparing, not inputvariable, which has a value of 0. Unless # gets replaced by 0, the test will always succeed.

If it does, though, the body of the if statement is the ; on the end. By itself, ; is a no-operation. So, if inputvariable, or val, when you change the statement, is less than #, do nothing. Otherwise, do nothing. How useful is that? Get rid of the ;.

Code:
if else (inputvarible >#); //if varible is below ^
If else? That should be "else if", and inputvariable should be val. What do you propose to do if val is equal to #? What you should have is:
Code:
if(val < #)
{
   // do one thing
}
else
{
   // do the other thing
}
Of course, # needs to be replaced with an actual number.

Code:
delay(300000) /delay 5 min before remeasuring. is there a better way???
Literals are interpreted as ints when there is no decimal point, and nothing to indicate otherwise. In this case, there is no decimal point, and nothing to indicate otherwise, so the compiler will happily shove 300000 into an int. Now, this might be a problem, since 300000 wont fit into an int, without a bit of it overflowing.

Since the delay() function takes an unsigned long as its argument, you need to tack UL onto the end of the number to tell the compiler to treat 300000 as an unsigned long, instead of an int.
Code:
delay(300000UL) /delay 5 min before remeasuring. is there a better way???
It goes without saying, I hope, that you need a ; and 2 slashes for the comment.

Is there a better way? Yes. Look at the BlinkWithoutDelay example for a clue. Nothing will happen while the delay() function is "executing" (read that as doing nothing for a while).

You can't read the sunlight level and turn on lights, you can't test the temperature of the solution, or any of the myriad other things that you will eventually want to do.

Finally, putting each { and } on lines by themselves, and indenting code between the { and } will make the program much easier to read.

Oh, and look at the logic of your program. If the ph level is low, turn the pump on for 5 second. If the ph level is high, turn the pump on for 1 second. Now, if you were writing instructions for your neighbor to watch your tank while you went on vacation, would you describe what needed to be done this way?

And, last, but not least, comments should describe what the program is going to do, not what it did.
Code:
// Turn the pump on if the ph level gets low
if(phLevel < someValue)
{
   digitalWrite(pumpPin, HIGH);
}
is far better than:
Code:
if(phLevel < someValue)
{
   digitalWrite(pumpPin, HIGH); // Turn the pump on
}
Any moron can see that the digitalWrite function has turned the pump on. What we need to know/you need to document is the overall logic of the program, not a step by step description of each statement.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25752
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Any moron can see that the digitalWrite function has turned the pump on.
A bit harsh:
Code:
digitalWrite (LEDpin, HIGH);
Did the digitalWrite turn the LED on or off?
Answer: it depends how the LED is wired.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
A bit harsh:
Perhaps, and for that I apologize.

Quote
Answer: it depends how the LED is wired.
How would you wire an LED so that HIGH turned it off? Why would you wire it that way?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25752
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How would you wire an LED so that HIGH turned it off?
Anode to 5V, cathode to output pin.
The usual.

Quote
Why would you wire it that way?
Because source and sink currents are not always the same (though they are for the AVR)
Because you've got a common anode device.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

here inside my body
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you very much. I will go though and replace these things brought up. I need to be told these things other wise I will continue to fail. You can call me a moron if you keep giving me good advice  smiley.Thank you for your help and assistance. You get a shiny gold star  smiley

Thanks again
Logged

Pages: [1]   Go Up
Jump to: