How to control ph

Hi there, I'm new to the forum and to arduino. Ive got a arduino mega 2560 which I'm wanting to use for a aquarium controller. I would like to control the pH. I bought a pH Stamp from Atlas Scientific , which communicates via the serial1. It is working i get the pH readings. what i would like to do is control the pH using a solenoid valve. So when it can maintain the pH between pH6.00 and pH6.50 for my calcium reactor. Ive got know idea how to do it. any help would be appreciated. Thanks. Here is the code I'm using.

float Ph_val = 0.0;
char inData_Ph[24];
char string_Ph[4];

void setup()
{
Serial.begin(38400);
Serial1.begin(38400); // pH stamp
}

void loop(void)
{
Serial1.println("c");
Serial1.print(13,BYTE);
delay(1000);

if (Serial1.available() > 0)
{
for (int i=0;i<4;i++) {
inData_Ph = char(Serial1.read());
}
}
sscanf(inData_Ph, "%*3c%s ", string_Ph);
Serial1.flush();
if (atoi(string_Ph) != 0) {
Ph_val = atof(string_Ph);
}
Serial.print("Sensor output: [");
Serial.print(inData_Ph);
Serial.println("]");
}

If you use the code tags (the button labelled '#') then the code will be formatted correctly...

Also I think we need to know a little more about your sensor and means of altering pH to understand the issue.

There are a number of issues with this code. Take a look at this thread where PaulS helped another user through similar problems: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1294830203

A google search of the forum for PH produced a lot of reading material. A google search of the old forum may have even more.

http://www.google.com/search?q=ph+site%3Ahttp%3A%2F%2Farduino.cc%2Fforum%2Findex.php&hl=en&num=100&lr=&ft=i&cr=&safe=images

I get the readings from the stamp which is correct, I would like to take the readings and control the pH. that's what I'm struggling with.

This my current code

#include <glcd.h> // library header
#include "fonts/Arial14.h" // proportional font
#include "fonts/SystemFont5x7.h" // system font
#include "bitmaps/rob2.h" // bitmap
float Ph_val = 0.0;
char inData_Ph[15];
char string_Ph[4];
int led = 7;
int led_on= 26.0; //Turn on the heater at this temp
int led_off_temp = 28.5; //Turn off heater at this temp
int ph = 6;
int ph_led_on = 4.00; //Turn on the Solinoid at this PH
int ph_led_off = 5.00; //Turn off the Solinoid at this PH

void toggle(int Temp)
{
if (digitalRead(Temp) == HIGH,DEC)
digitalWrite(Temp, LOW);
else
digitalWrite(Temp, HIGH);
}

void setup()
{
pinMode(led, OUTPUT);
pinMode(ph, OUTPUT);
Serial.begin(38400);
Serial.println("Ready");
Serial1.begin(38400); // pH stamp
GLCD.Init(NON_INVERTED); // initialise the library
GLCD.ClearScreen();
GLCD.DrawBitmap(rob2, 32,0, BLACK); //draw the bitmap at the given x,y position
delay(2000);
GLCD.ClearScreen();
GLCD.SelectFont(System5x7);
}

void loop()
{
float Tank = map(analogRead(0),250,700,14,441); // analog pin reads 250-700, corresponds to 1.4C to 44.1C
float Sump = map(analogRead(5),250,700,14,441); // analog pin reads 250-700, corresponds to 1.4C to 44.1C
Tank /= 10.0; // divide by 10; map() uses integers
Sump /= 10.0; // divide by 10; map() uses integers
if(Tank < led_on){ // turn heater on if temp is below heater_on_temp
digitalWrite(led, HIGH);
}
if(inData_Ph > ph_led_on){ // turn sol on if ph is below ph_led_on
digitalWrite(ph, HIGH);
}
{

Serial1.println("c");
Serial1.print(13,BYTE);
Serial1.print(Sump);
Serial1.print(13,BYTE);
delay(500);

if (Serial1.available() > 0)
{
for (int i=0;i<4;i++) {
inData_Ph = char(Serial1.read());
}
}
sscanf(inData_Ph, "%*3c%s ", string_Ph);
Serial1.flush();
if (atoi(string_Ph) != 0) {
Ph_val = atof(string_Ph);
}

  • if(Tank> led_off_temp){ //turn heater off if temp is above heater_off_temp*
  • digitalWrite(led, LOW);*
  • if(inData_Ph < ph_led_off){ //turn Solinoid off if temp is above PH Off*
  • digitalWrite(ph, LOW);*
  • }*
  • Serial.print("Main Temp: "); *
  • Serial.println(Tank);*
  • Serial.print("Sump Temp: "); *
  • Serial.println(Sump); *
  • Serial.print("PH: [");*
  • Serial.print(inData_Ph);*
  • Serial.println("]");*
  • delay(1000); *
  • GLCD.CursorTo(0,0);*
  • GLCD.Puts("Temp:Tank:");*
  • GLCD.print(Tank);*
  • GLCD.Puts("'C");*
  • GLCD.CursorTo(5,1);*
  • GLCD.Puts("Sump:");*
  • GLCD.print(Sump);*
  • GLCD.Puts("'C");*
  • GLCD.CursorTo(0,2);*
  • GLCD.Puts("Ph:");*
  • GLCD.print(inData_Ph);*
    }
    }
    i still cant control the ph, with this code it says ISO C++ forbids comparison between pointer and integer. I need help to sort this out.

i still cant control the ph, with this code it says ISO C++ forbids comparison between pointer and integer. I need help to sort this out.

It is a pronoun with no referent. What are you talking about? The compiler? If so, show the EXACT message.

Serial1.flush();

Do you know what this does? If not, why are you using it?

if (Serial1.available() > 0)
{
  for (int i=0;i<4;i++) {
    inData_Ph = char(Serial1.read());
  }
}

this is clearly broken as it read 4 characters without checking they are all available - there is likely to be only one available and so 1 valid and 3 nonsense bytes are read.

Also the assignment should be inData_Ph[i] = char(Serial1.read()); shouldn't it?

This code is also clearly broken:

  if(inData_Ph < ph_led_off){    //turn Solinoid off if temp is above PH Off
   digitalWrite(ph, LOW);
  }

its comparing a character array to a number. You need to convert the string to a number before doing arithmetic comparisons.

And the declaration of ph_led_off is inconsistent:

int ph_led_off = 5.00;

4.00 is a floating point constant, but the variable is declared as int.

Thanks for the help so far.

inData_Ph is the name of an array. Without the brackets and index, it is a pointer. ph_led_on is an integer. You can't compare the two. That's the error you mentioned.

How is this done? Im very new to this.Ive been taking bits and pieces of others peoples codes to try and get this to work.

Serial1.flush();

Do you know what this does? If not, why are you using it?

I don't know what it means but when i take it away the data that i get on the GLCD is jumbled.

when i verify i get this message in the compiler

PH_TEMP_TEST_control.cpp: In function 'void loop()':
PH_TEMP_TEST_control:47: error: ISO C++ forbids comparison between pointer and integer
PH_TEMP_TEST_control:69: error: ISO C++ forbids comparison between pointer and integer
PH_TEMP_TEST_control:70: error: expected `)' before 'ph'

my code with small changes

#include <glcd.h> // library header
#include "fonts/Arial14.h" // proportional font
#include "fonts/SystemFont5x7.h" // system font
#include "bitmaps/rob2.h" // bitmap
float Ph_val = 0.0;
char inData_Ph[15];
char string_Ph[4];
int led = 7;
int led_on= 26.0; //Turn on the heater at this temp
int led_off_temp = 28.5; //Turn off heater at this temp
int ph = 6;
int led_on_ph= 5.00; //Turn on the Solinoid at this PH
int led_off_ph = 4.00; //Turn off the Solinoid at this PH

void toggle(int Temp)
{
if (digitalRead(Temp) == HIGH,DEC)
digitalWrite(Temp, LOW);
else
digitalWrite(Temp, HIGH);
}

void setup()
{
pinMode(led, OUTPUT);
pinMode(ph, OUTPUT);
Serial.begin(38400);
Serial.println("Ready");
Serial1.begin(38400); // pH stamp
GLCD.Init(NON_INVERTED); // initialise the library
GLCD.ClearScreen();
GLCD.DrawBitmap(rob2, 32,0, BLACK); //draw the bitmap at the given x,y position
delay(2000);
GLCD.ClearScreen();
GLCD.SelectFont(System5x7);
}

void loop()
{
float Tank = map(analogRead(0),250,700,14,441); // analog pin reads 250-700, corresponds to 1.4C to 44.1C
float Sump = map(analogRead(5),250,700,14,441); // analog pin reads 250-700, corresponds to 1.4C to 44.1C
Tank /= 10.0; // divide by 10; map() uses integers
Sump /= 10.0; // divide by 10; map() uses integers
if(Tank < led_on){ // turn heater on if temp is below heater_on_temp
digitalWrite(led, HIGH);
}
if(inData_Ph > led_on_ph){ //Turn on the Solinoid at this PH
digitalWrite(ph, HIGH);
}

Serial1.println("c");
Serial1.print(13,BYTE);
delay(500);

if (Serial1.available() > 0)
{
for (int i=0;i<4;i++) {
inData_Ph = char(Serial1.read());

  • }*
  • }*
    sscanf(inData_Ph, "%*3c%s ", string_Ph);
  • Serial1.flush();*
  • if (atoi(string_Ph) != 0) {*
  • Ph_val = atof(string_Ph);*
  • }*
  • if(Tank> led_off_temp){ //turn heater off if temp is above heater_off_temp*
  • digitalWrite(led, LOW);*
  • if(inData_Ph < led_off_ph){ //Turn off the Solinoid at this PH *
  • digitalWrite(led ph, LOW);*
  • }*
  • Serial.print("Main Temp: "); *
  • Serial.println(Tank);*
  • Serial.print("Sump Temp: "); *
  • Serial.println(Sump); *
  • Serial.print("PH: [");*
  • Serial.print(inData_Ph);*
  • Serial.println("]");*
  • delay(2000); *
  • GLCD.CursorTo(0,0);*
  • GLCD.Puts("Temp:Tank:");*
  • GLCD.print(Tank);*
  • GLCD.Puts("'C");*
  • GLCD.CursorTo(5,1);*
  • GLCD.Puts("Sump:");*
  • GLCD.print(Sump);*
  • GLCD.Puts("'C");*
  • GLCD.CursorTo(0,2);*
  • GLCD.Puts("Ph:");*
  • GLCD.print(inData_Ph);*
  • }*
    }

Please use the code button ('#') when posting code snippets

 // like this
int led_on= 26.0;  //Turn on the heater at this temp       
int led_off_temp = 28.5; //Turn off heater at this temp
int ph = 6;
int led_on_ph= 5.00;  //Turn on the Solinoid at this PH
int led_off_ph = 4.00; //Turn off the Solinoid at this PH

You need to learn exactly what you can and can not store in a integer variable, and what happens when you (attempt to) store non-integer data in an integer.

my code with small changes

Clearly not enough, since you haven't addressed any of the issues pointed out already.

Failing to use the code tags (put in by the # button) is causing the forum to interpret your code, with disastrous results. Learn to post code properly is you want help.