zoomkat:
Arrch:
chiques:
Hi Arrrch,
Does this mean its not possible to convert a string to float in Arduino?Consider it a sign to stop using Strings and take a few minutes to learn about strings.
If it only takes a few minutes to learn the strings answer, how about posting up the needed code.
I don't usually write code for people; not part of my philosophy on teaching. I realize your philosophy differs greatly from that.
Luckily I happen to have simple example sitting on my computer:
/*
* Example for Serial2Int
* When reading from the serial monitor, there are two important things to note:
* (1) Bytes are read one at a time. So when sending "246", will be read by your
* code as '2', then '4', then '6'. If you want to identify them as related in some
* way, you need a way to determine that. This example uses start and stop bytes.
* (2) Sending a number through the monitor sends it's ASCII representation, not
* the value itself. So typing 3 and hitting enter would send '3' or 51 as per the
* ascii table. To account for this, we will be using atoi(), which takes a null
* terminated array of chars, also known as a string, and produces the int equivalent.
*/
// To send a number through the serial monitor, put it between brackets
const char startByte = '<';
const char stopByte = '>';
// Maximum characters in an int + null terminated character
const byte maxBuffer = 6;
void setup() {
Serial.begin(57600);
Serial.println("[Serial2Int]");
}
void loop() {
// Stores the characters between the start and stop bytes
static char buffer[maxBuffer];
// Keeps track of spot in buffer
static byte index=0;
if (Serial.available() > 0 ) {
char inChar = Serial.read();
if (inChar==startByte) { // If start byte is received
index=0; // then reset buffer and start fresh
} else if (inChar==stopByte) { // If stop byte is received
processData(buffer); // and process the data
index=0; // this isn't necessary, but helps limit overflow
} else { // otherwise
buffer[index] = inChar; // put the character into our array
index++; // and move to the next key in the array
buffer[index] = '\0'; // then null terminate
}
/* Overflow occurs when there are more than 5 characters in between
* the start and stop bytes. This has to do with having limited space
* in our array. We chose to limit our array to 5 (+1 for null terminator)
* because an int will never be above 5 characters */
if (index>=maxBuffer) {
index=0;
Serial.println("Overflow occured, next value is unreliable");
}
}
}
void processData(char buffer[]) {
unsigned int value = atoi(buffer); // convert string to int
Serial.print("Value: ");
Serial.println(value);
}