I have a strange issue with a code I am working on.I have never seen it before. I tweaked my code and it worked perfectly last week.I left my computer for a few days and now it is telling me this error that I wrote in the Subject bar. The section of code below is where the error is returning from.It is just a simple string to float conversion that worked last time.
The error it is sending me back is when I try to covert a string to a float and I don't know why. As I said it worked perfect last week and now no.????
I have uninstalled and re-installed but still the same.I will post a screenshot. Any Ideas????
Thanks in advance
if (sensor_stringcomplete1) { //if a string from the Atlas Scientific product has been recived in its entirety
[u][b]float pH = sensorstring1.toFloat(); //Convert String to a Float [/b][/u]
digitalWrite(12, HIGH); //turn on the ON Led
lcd.setCursor(-4, 1); //set the position on the LCD
lcd.print("pH= "); //Print this on the LCD
lcd.print(sensorstring1); //send that string to to the LCD
lcd.print(" ");
sensorstring1 = ""; //clear the string:
sensor_stringcomplete1 = false; //reset the flag used to tell if we have recived a completed string from the Atlas Scientific product
pHReceived = true;
if (pH < 6.5 || pH > 8.5) { //if pH is less than 6.5 and/or greater than 8.5
digitalWrite(12, LOW); //turn off the ON Led
digitalWrite (13, HIGH); //turn on the WARNING Led
delay (250);
digitalWrite (13, LOW); //turn off the WARNING Led
delay (250);
lcd.setCursor(-4, 1); //set the cursor
lcd.print("****Warning**** "); //print this message
delay(1000);
}
}
I have a strange issue with a code I am working on.I have never seen it before. I tweaked my code and it worked perfectly last week.I left my computer for a few days and now it is telling me this error that I wrote in the Subject bar. The section of code below is where the error is returning from.It is just a simple string to float conversion that worked last time.
The error it is sending me back is when I try to covert a string to a float and I don't know why. As I said it worked perfect last week and now no.????It will not let me compile.
I have uninstalled and re-installed but still the same.I will post a screenshot. Any Ideas????
Thanks in advance
Code:
if (sensor_stringcomplete1) { //if a string from the Atlas Scientific product has been recived in its entirety
**float pH = sensorstring1.toFloat(); //Convert String to a Float **
digitalWrite(12, HIGH); //turn on the ON Led
lcd.setCursor(-4, 1); //set the position on the LCD
lcd.print("pH= "); //Print this on the LCD
lcd.print(sensorstring1); //send that string to to the LCD
lcd.print(" ");
sensorstring1 = ""; //clear the string:
sensor_stringcomplete1 = false; //reset the flag used to tell if we have recived a completed string from the Atlas Scientific product
pHReceived = true;
if (pH < 6.5 || pH > 8.5) { //if pH is less than 6.5 and/or greater than 8.5
digitalWrite(12, LOW); //turn off the ON Led
digitalWrite (13, HIGH); //turn on the WARNING Led
delay (250);
digitalWrite (13, LOW); //turn off the WARNING Led
delay (250);
lcd.setCursor(-4, 1); //set the cursor
lcd.print("Warning "); //print this message
delay(1000);
}
}
You need to put code inside of code tags like the Read this before posting a programming question thread told you. You did read it? It also told you some other things.
Does the conversion method of String tell you when it's made a booboo?
I'm not a String Object fan nor a conversion or scan fan so I don't remember.
In any case perhaps check the result for being within expected range and print the String before you empty it?
At least that way you'll have an idea of WHAT did not convert.
You may have a hardware/wiring problem or your sketch may have run out of RAM and somehow didn't crash.
/*
// include the library code:
#include <LiquidCrystal.h> //LCD library
LiquidCrystal lcd(53, 51, 49, 47, 45, 43); //LCD pins
String inputstring = ""; //a string to hold incoming data from the PC
String sensorstring1 = ""; //a string to hold the data from the Atlas Scientific product
String sensorstring3 = ""; //a string to hold the data from the Atlas Scientific product
boolean input_stringcomplete = false; //have we received all the data from the PC
boolean sensor_stringcomplete1 = false; //have we received all the data from the Atlas Scientific product
boolean sensor_stringcomplete3 = false; //have we received all the data from the Atlas Scientific product
boolean pHReceived = false, ORPReceived = false;
float temp; //where the final temperature data is stored
float pH; //float to store the pH String
float ORP; //float to store the ORP String
int ONLed = 12;
int WARNINGLed = 13; //led pins
void setup() { //set up the hardware
lcd.begin(16, 4);
Serial.begin(38400); //set baud rate for the hardware serial port_0 to 38400
Serial1.begin(38400); //set baud rate for software serial port_1 to 38400
Serial3.begin(38400); //set baud rate for software serial port_3 to 38400
inputstring.reserve(5); //set aside some bytes for receiving data from the PC
sensorstring1.reserve(30); //set aside some bytes for receiving data from Atlas Scientific product
sensorstring3.reserve(30);
pinMode(2, OUTPUT); //set pin 2 as an output
pinMode(12, OUTPUT); //set pin 12 as an output
pinMode(13, OUTPUT); //set pin 13 as an output
}
void serialEvent1() { //if the hardware serial port_2 receives a char
char inchar = (char)Serial1.read(); //get the char we just received
if (inchar == '\r') {
sensor_stringcomplete1 = true; //if the incoming character is a <CR>, set the flag
}
else {
sensorstring1 += inchar; //add it to the inputString
}
}
void serialEvent3() { //if the hardware serial port_3 receives a char
char inchar = (char)Serial3.read(); //get the char we just received
if (inchar == '\r') {
sensor_stringcomplete3 = true; //if the incoming character is a <CR>, set the flag
}
else {
sensorstring3 += inchar; //add it to the inputString
}
}
float read_temp(void) { //the read temperature function
float v_out; //voltage output from temp sensor
float temp; //the final temperature is stored here
digitalWrite(A0, LOW); //set pull-up on analog pin
digitalWrite(2, HIGH); //set pin 2 high, this will turn on temp sensor
delay(2); //wait 2 ms for temp to stabilize
v_out = analogRead(0); //read the input pin
digitalWrite(2, LOW); //set pin 2 low, this will turn off temp sensor
v_out *= .0048; //convert ADC points to volts (we are using .0048 because this device is running at 5 volts)
v_out *= 1000; //convert volts to millivolts
temp = 0.0512 * v_out - 20.5128; //the equation from millivolts to temperature
return temp; //send back the temp
}
void loop() { //here we go...
if (pHReceived && ORPReceived) {
temp = read_temp(); //read the Temperature
if (temp) {
digitalWrite(12, HIGH); //turn on ther ON Led
lcd.setCursor(0, 0); //set the position on the LCD
lcd.print(temp); //print the temperature data
lcd.print("C ");
}
pHReceived = false;
ORPReceived = false;
if (temp > 24.00) { //if temperature is more than 24 degrees celcius
digitalWrite(12, LOW); //turn off the ON Led
digitalWrite (13, HIGH); //turn on the WARNING Led
delay (250);
digitalWrite (13, LOW); //turn off the WARNING Led
delay (250);
lcd.setCursor(0, 0); //set the cursor
lcd.print("****Warning**** "); //print this message
delay(1000);
}
}
if (sensor_stringcomplete1) { //if a string from the Atlas Scientific product has been recived in its entirety
float pH = sensorstring1.toFloat(); //Convert String to a Float
digitalWrite(12, HIGH); //turn on the ON Led
lcd.setCursor(-4, 1); //set the position on the LCD
lcd.print("pH= "); //Print this on the LCD
lcd.print(sensorstring1); //send that string to to the LCD
lcd.print(" ");
sensorstring1 = ""; //clear the string:
sensor_stringcomplete1 = false; //reset the flag used to tell if we have recived a completed string from the Atlas Scientific product
pHReceived = true;
if (pH < 6.5 || pH > 8.5) { //if pH is less than 6.5 and/or greater than 8.5
digitalWrite(12, LOW); //turn off the ON Led
digitalWrite (13, HIGH); //turn on the WARNING Led
delay (250);
digitalWrite (13, LOW); //turn off the WARNING Led
delay (250);
lcd.setCursor(-4, 1); //set the cursor
lcd.print("****Warning**** "); //print this message
delay(1000);
}
}
if (sensor_stringcomplete3) {
float ORP = sensorstring3.toFloat(); //Convert String to a Float
digitalWrite(12, HIGH);
lcd.setCursor(-4, 2);
lcd.print("ORP= ");
lcd.print(sensorstring3);
lcd.print("mV");
lcd.print(" ");
sensorstring3 = "";
sensor_stringcomplete3 = false;
ORPReceived = true;
if (ORP < -300.00 || ORP > 300.00) {
digitalWrite(12, LOW);
digitalWrite (13, HIGH);
delay (250);
digitalWrite (13, LOW);
delay (250);
lcd.setCursor(-4, 2);
lcd.print("****Warning**** ");
delay(1000);
}
}
}
My crystal ball tells me that the problem is in line 42.
(TRANSLATION: You only showed a part of your code, so I can only provide a part of the answer.)
If you are using the String class described at String() - Arduino Reference, the documentation does not describe the toFloat() method (function) that you are using. How it worked before is a mystery to me.
toInt() is listed there, but not toFloat().
Also:
The usual recommendation is to use a C string, not String. The forum can provide advice to you on this if you want. String does dynamic memory allocation. This can be problematic with a small memory, and there are some bugs to watch out for with the Arduino. However, these problems will show up during execution, not during compilation. A C string avoids these issues.
It is best to cut and paste any error messages rather than retyping them by hand. I could understand the mistyped error message this time, but next time you might not be so lucky.
The advice above may sound blunt but I am trying to help you.
long parseInt(); // returns the first valid (long) integer value from the current position.
// initial characters that are not digits (or the minus sign) are skipped
// integer is terminated by the first character that is not a digit.
float parseFloat(); // float version of parseInt