Maybe i am missing something but in the code below the first .toInt() returns the correct value, but the second doesn’t.
#include "Arduino.h"
#include <Streaming.h>
String axisValueString = "";
int commandCounter = 0;
int newXAxis; // The new XY position
int newYAxis; //
void setup()
{
Serial.begin(19200);
while(!Serial);
Serial << "System ready" << endl;
}
void loop()
{
if(Serial.available())
{
readCommand(Serial.read());
}
}
void readCommand(char ch)
{
if (ch == ',' || ch == '\n' || ch == '\r') {
if(ch == ',') {
newXAxis = axisValueString.toInt();
Serial << "Received comma" << endl;
Serial << "Got the first axis value = " << axisValueString << endl;
Serial << "Converted to INT = " << newXAxis << endl;
axisValueString = "";
}
if(ch == '\n' || ch == '\r') {
newYAxis = axisValueString.toInt();
Serial << "Received end of line" << endl;
Serial << "Got the second axis value = " << axisValueString << endl;
Serial << "Converted to INT = " << newYAxis << endl;
axisValueString = "";
}
}
else
{
if(ch != '.') {
axisValueString += ch;
}
}
}
Entered the following sequence: 123.45,678.90
This is the result:
System ready
Received comma
Got the first axis value = 12345
Converted to INT = 12345
Received end of line
Got the second axis value = 67890
Converted to INT = 2354
The second axis value is correct but the int is not correct
Harry.