I'm not sure if that is really what I need, but basically I want the arduino to read a value sent over a software serial from a pH sensor board. I have tried using atof() but it seems to truncate everything after the decimal point (ie. 8.00 instead of 8.13) and I absolutely need accuracy to at least 0.1 . I can't just use serial.print() because I need the arduino to compare the ph value to specific value and then make adjustments as needed.
...
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
#define rxpin 12
#define txpin 11
SoftwareSerial mySerial(rxpin, txpin);
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
String inputstring ="";
String sensorstring ="";
boolean input_stringcomplete = false;
boolean input_stringtemp = false;
boolean sensor_stringcomplete = false;
const int pumpPin = 9;
const int ledPin = 13;
char* Value = "0";
void setup(){
lcd.begin(16, 2);
Serial.begin(38400);
mySerial.begin(38400);
inputstring.reserve(5);
sensorstring.reserve(30);
pinMode(9, OUTPUT);
pinMode(13, OUTPUT);
}
void serialEvent() {
char inchar = (char)Serial.read();
inputstring += inchar;
if(inchar == '\r') {input_stringcomplete = true;}
if(inchar == 't') {input_stringtemp = true;}
}
void loop(){
Serial.println("ph adjust");
mySerial.print("r");
mySerial.print('\r');
delay(500);
if(mySerial.available()){
Value[0] = mySerial.read();
float phVal = atof(Value);
Serial.println(phVal);
lcd.print(phVal);
while (phVal > 5.8){
digitalWrite(pumpPin, HIGH);
delay(100);
digitalWrite(pumpPin, LOW);
delay(2000);
lcd.clear();
if (phVal <= 5.8)
digitalWrite(ledPin, HIGH);
break;
}
mySerial.print("e");
mySerial.print('\r');
delay(1000);
lcd.clear();
mySerial.flush();
}
}
I wish I knew more about how strings and serials operate so I could write a function that would extract the value I need. So again my questions are these: (1) How do I convert a string to a float with greater than 0.1 accuracy, (2) is there another way to extract the data from the serial buffer, or (3) if the answer is not fit for this forum where should I look to find out more about how serials and strings are constructed. I would really appreciate any advice, changes to code, or noteworthy tutorials. (sorry for the previous formatting of this post, i'm still learning)
Thanks