ive got a problem with reading only 3 digits in my code for example when the integer value is 145632 the program must only read 145 as an integer in serial port, can please someone help on how to do it i will really appreciate it
heres my code so far im just testing it using built-in Led , i need to know how can i read only 3 digits in a integer value, when i send 1001 in serial monitor, program must only recognise a value as 100 and do the conditions in the loops
//send string using serial monitor
String readString, data1, data2, data3;
void setup() {
Serial.begin(9600);
Serial.println("parse-test-3"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
delay(2);
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
}
}
if (readString.length() >0) {
Serial.println(readString); //see what was received
// expect a string like 123456789 with three data partts
data1 = readString.substring(0, 3); //get the first three characters
data2 = readString.substring(3, 6); //get the next three characters
data3 = readString.substring(6, 9); //get the next three characters
Serial.println(data1); //print to serial monitor to see results
Serial.println(data2);
Serial.println(data3);
readString="";
data1="";
data2="";
data3="";
}
}
The examples in Serial Input Basics are simple reliable ways to receive data. When all the data is in a char array you can parse it any way you want. There is also a parse example.
If you want to ignore all but (say) the first three characters of a number it is much easier to do that before converting the string to an integer.
Taking the first 3 characters of 145632 is all very well - but what should happen if the number is 14563 ? Perhaps you should be looking at the value of 014 ?
The String class is a very flexible and easy way to handle string data. However, the price paid for that ease of use is that it bloats the code size larger than it might be otherwise. Zoomcat's example compiles to 4212 bytes. A functional alternative to his code, but using char arrays instead, compiles to 2230 bytes:
//send string using serial monitor
char readString[10], data1[10], data2[10], data3[10];
void setup() {
Serial.begin(9600);
Serial.println("parse-test-3"); // so I can keep track of what is loaded
}
void loop() {
int charsRead;
while (Serial.available()) {
while (Serial.available()) {
if (Serial.available() > 0) {
charsRead = Serial.readBytesUntil('\n', readString, sizeof(readString) - 1);
readString[charsRead] = '\0'; // Now it's a string
}
}
if (strlen(readString) > 0) {
Serial.println(readString); //see what was received
// expect a string like 123456789 with three data partts
strncpy(data1, readString, 3); //get the first three characters
strncpy(data2, &readString[3], 3); //get the first three characters
strncpy(data3, &readString[6], 3); //get the first three characters
Serial.println(data1); //print to serial monitor to see results
Serial.println(data2);
Serial.println(data3);
}
}
}
In some cases, saving about 2K of memory might make a difference.
If I might suggest, even if you knew with 100% confidence that the value transmitted to my arduino was indeed the first three characters and the value was always between 100 and 999 inclusive, you should still send data using delimiters in order to parse the values correctly.
Building in @Robin2's excellent advice (and example) have you considered transmitting in a format that is more clearly defined?
for example rather than sending:
145632
and poling around with a stick, send it like this:
myData&145~632~
you can then parse that VERY easily with no confusion what you want.
If in the case where you couldn't control that (e.g. a return from a website) you might look for other markers in the return where I may register from more easily.
BulldogLowell:
If I might suggest, even if you knew with 100% confidence that the value transmitted to my arduino was indeed the first three characters and the value was always between 100 and 999 inclusive, you should still send data using delimiters in order to parse the values correctly.
Building in @Robin2's excellent advice (and example) have you considered transmitting in a format that is more clearly defined?
for example rather than sending:
145632
and poling around with a stick, send it like this:
myData&145~632~
you can then parse that VERY easily with no confusion what you want.
If in the case where you couldn't control that (e.g. a return from a website) you might look for other markers in the return where I may register from more easily.
I agree, but I think it would be better to use somewhat standard delimiters, like commas (CSV = comma separated values).
Hi guys here my code so far,im sorry for asking to much im trying to learn..what i want to know is to read and compare by last recent value in serial port becuase in this program i only compare by using 100.. i want if the program reads for exp 150 in the serial port next time i send number must compare with 150 and so on ..i want it to compare with last variable in the serial port , not only 1 number, if maybe i can know to save a value a register or something to compare with next time.
thank you i will aslo appreciate help again
CODE:
String readString, data1,data2;
int LEDpin = 13;
void setup() {
pinMode(LEDpin, OUTPUT);
Serial.begin(9600);
}
void loop() {
while (Serial.available()) {
delay(2);
if (Serial.available() >0) {
char c = Serial.read();
readString += c;
}
}
if (readString.length() >0) {
Serial.println(readString);
data1 = readString.substring(0, 3);
Serial.println(data1);
int x = data1.toInt();
Serial.println(x);
if ( x >100)
{
for (int A111 = save ;A111>=save;A111--)
We gave you a pass at the outset, but now you need to read Nick Gammon's two posts at the top of this Forum on the proper way to post code using code tags. It makes it easier on us.
Starting with A111 equal to save, while A111 is greater than or equal to save, do something and then decrement A111. Just how many times is this loop going to iterate?
Well, it is simple, shows the concept of capturing characters, it compiles, and it works. It is unknown if the data packet has an end of packet marker so could be the only way. And your code is...