Simple pH class.
Diff: pH_Sensor.cpp
- Revision:
- 1:1314058dbadb
- Child:
- 2:767f53c397ad
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pH_Sensor.cpp Sat Dec 05 07:31:02 2015 +0000 @@ -0,0 +1,54 @@ +#include "pH_Sensor.h" + +void pH_Sensor::disableContinuousMode() { + printf("pH sensor will NOT run in continous mode.\n"); + if(pH_Serial.writeable() <= 0) printf("Not writable\n"); + // disable continuous mode + pH_Serial.printf("C,0"); + pH_Serial.printf("%c", '\r'); + printf("Waiting five seconds... "); + wait(5); +} + +void pH_Sensor::setup() { + pH_Serial.baud(9600); + disableContinuousMode(); +} + +// Send default message to pH sensor, asking for data. +void pH_Sensor::request() { + printf("Sending pH request...\n"); + if(pH_Serial.writeable() <= 0) printf("Not writable\n"); + // request one reading + pH_Serial.printf("R"); + pH_Serial.printf("%c", '\r'); +} + +float pH_Sensor::read() { + float pH = 0.0; + request(); + printf("Reading pH information.\n"); + if (pH_Serial.readable() > 0) { //if we see that the Atlas Scientific product has sent a character. + string sensorstring = ""; + printf("Receiving sensor string... ["); + char inchar; + while((inchar = (char)pH_Serial.getc()) != '\r') { + sensorstring += inchar; + printf("%c", inchar); + } + printf("] ...sensor string received!\n"); + printf(sensorstring.c_str()); + // +1 is to get rid of control character + pH = atof(sensorstring.c_str()+1); //convert the string to a floating point number so it can be evaluated by the Arduino + + if (pH >= 7.0) { //if the pH is greater than or equal to 7.0 + printf("high\n"); //print "high" this is demonstrating that the Arduino is evaluating the pH as a number and not as a string + } + else { //if the pH is less than or equal to 6.999 + printf("low\n"); //print "low" this is demonstrating that the Arduino is evaluating the pH as a number and not as a string + } + } else { + printf("pH sensor is not readable\n"); + } + return pH; +} \ No newline at end of file