Thank you for reading this.. you are dealing with a beginner....
PROBLEM DESCRIPTION
I am trying to write some code to be able to extra data from a string and convert them into integers...
From the serial stream, the arduino will be receiving serially data of the type:
s-12.3,p-23.4,g12.3,
My code should be able to:
Detect the string part starting with the character p (in the above example p23.4,)
Get the integer between the character p and coma
Finally convert it into an integer n so the resulting value is -23
I have put together the code below but im not quite there...
My goal is to seek your help to rewrite this so that the string gets augmented only when we get the "p"
something like this...
if ((mostSignificantDigit) == 'p'){
readString += c; // but we should find a way to remove the character "c" from the string and keep the incoming digits
}
CODE:
String readString;
String formatstring;
int n;
void setup() {
Serial.begin(9600); // initialize communication to the display
}
void loop() {
// STARTING READING THE STRING FROM HERE:
while (Serial.available()) {
delay(10); //small delay to allow input buffer to fill
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') { //
break;
} //breaks out of capture loop to print readstring
if (c != 'p') { // removing the string identifier from the string so that it contains only digits
readString += c;
}
} //makes the string readString
if (readString.length() >0) {
char mostSignificantDigit = readString.charAt(0);
if ((mostSignificantDigit) == 'p'){
}
char carray[10];
readString.toCharArray(carray, sizeof(carray));
n = atoi(carray);
Serial.println(n);
readString=""; //clears variable for new input
}
}
Thank you for writing me back... Not really sure or what you mean ..could you please be kind enough to alter the code as suggested so that i can learn it? Again thank you so much for the help, patience and looking into this.
I managed to fix my own code by playing with the string... i attache the code here for future reference...
I'll look at the above commendation later but BIG THANK YOU for the answers...
/*
This program should be able to read string of the type
p12.3,
p-12.5
ignore strings with any other starting character like
f55,7,
g-23.6,
Expected output
for case of "p12.3," n = 12
for case of "p-12.5," n = -12
*/
String readString;
String formatstring;
int n;
void setup() {
Serial.begin(9600); // initialize communication to the display
}
void loop() {
// STARTING READING THE STRING FROM HERE:
while (Serial.available()) {
delay(10); //small delay to allow input buffer to fill
char c = Serial.read(); //gets one byte from serial buffer
readString += c;
if (c == ',') { //
break;
} //breaks out of capture loop to print readstring
} //makes the string readString
if (readString.length() >0) {
char mostSignificantDigit = readString.charAt(0);
if ((mostSignificantDigit) == 'p'){
formatstring = readString;
readString.setCharAt(0,' ' );
readString.trim();
char carray[10];
readString.toCharArray(carray, sizeof(carray));
n = atoi(carray);
Serial.println(n);
}
readString=""; //clears variable for new input
}
}