Go Down

Topic: Simple code for float from Atlas Scientific PH stamp (Read 2502 times) previous topic - next topic

Eric4

Since this is my first post, and being rather "new" on Arduino, I thought lets make it a contributing first post  :)

I have read through tens of posts in various fora to find a way to get the PH value, spitted out by the serial PH stamp, into a float for further manipulation. Most important for me was to get it simply to an LCD.
Info on the PH stamp you can find http://atlas-scientific.com/product_pages/embedded/ph.html

All related posts use some sort of string etc, a lot of code which in my case either didn't work (my inexperience) or was way too complex and memory hogging. Trying all sorts of code, and digging in the Arduino reference pages I stumbled upon a function that did exactly what I needed: simply transferring the ASCII characters into a float value.... serial.parseFloat()

Take a look at the code below (just a piece out of my total code for a Reef Controller).
What I only needed was the value itself.
Tested it, and works so far without visible side effects afaik

Quote

// -------------------------------------------------------------------------------
// 
// Simple way to get Atlas Scientific serial PH stamp value to LCD or PC
// Author: Eric van Riet - The Netherlands
// 
// ***************ONLY THE PART FOR PH IS LISTED!*****************
// ASSUMED THAT OTHER LIBRARIES FOR LCD ETC ARE IN YOUR OWN CODE!!

// -------------------------Set measurement interval------------------------------
long phReadInterval = 1000;
long previousMillisPH = 0;
int PHState = HIGH;
// -------------------------------------------------------------------------------

// ---------------Variable to hold PH value---------------------------------------
float PH_Val;
// -------------------------------------------------------------------------------

void setup()
{
  Serial.begin(38400);                 // Set baudrate for hardware serial port 0 (PC)
  Serial1.begin(38400);                // Set baudrate serial port 1 to 38400 (PH)
  lcd.begin (20,4);                    // initialize the lcd


// -------------Get PH value from Atlas Scientific PH serial stamp----------------
void getPHvalue()  {            //get PH every interval (1000 ms)
   unsigned long currentMillisPH = millis();
 
   if(currentMillisPH - previousMillisPH > phReadInterval) {
      previousMillisPH = currentMillisPH;  
      if (PHState == HIGH)  {
         PHState = LOW;
         }
      else  {
         PHState = HIGH;
         Serial1.print("r\r");
         while (Serial1.available() > 0) {
             // Serial1.parseFloat looks for the next valid float number
            // in the serial stream. In this case on hardware serial 1
            PH_Val = Serial1.parseFloat();
            // look for the carriage return. That's the end of your sentence:
            if (Serial1.read() == '\r') {
            Serial.println(PH_Val);  // can be used for debug, pc display etc.
            }
         }
      }
   }
  }
// -------------------------------------------------------------------------------


void loop()
{
  getPHvalue();

   lcd.setCursor(0,1);
   lcd.print("PH value: ");
   lcd.print(PH_Val);

}





I trust somebody else can find use for this.

robtillaart

You cannot start parsing when one character comes in. You should split the problem in 2 parts.
1) read one word from serial - combine them into one line until a separator is found (newline or space or so)
2) check if the word you got is a float number and convert it.

So try to recode this - note you can test the two steps separately !
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Eric4

Hello Rob,

I know there are other ways to do this, but this also "works" fine
My knowledge is not nearly deep enough to get all the insight, but I found this quite easy to implement.

Besides the timing I put in there it is basically only two steps:
- send command to return value ( r followed by <cr>)
- "listen" at serial and wait for value.

The Atlas stamp does this (part of their datasheet):
R   Instructs the pH Circuit to return a single pH reading.
*This instruction takes 378 milliseconds to complete
Full proper syntax:      r<cr> or R<CR>
The pH Circuit will respond:      XX.XX<CR>

I am always open for new things to learn, also to conduct this in the "proper way". Any pointers are welcome

cynaps

Working on a similar project. Would also like to know the correct way of doing this.

I have also worked with the atlas PH stamp and getting the float from serial was a significant hurdle for me as a beginner. Thanks to Nick Gammon's serial tutorial ( http://www.gammon.com.au/serial ) I was able to use his code to create a sketch that read from serial without blocking.

Go Up