Hello,
if you can, please help me with this script.
After I get the string, need to compare with number
or number as text
example
i get from serial 000xxxx
and need compare in script with "000xxx"
the whole script is here
thank you
dan
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
void setup() {
// initialize serial:
Serial.begin(9600);
}
void loop() {
// print the string when a newline arrives:
if (stringComplete) {
if (inputString == "0007324804") {
Serial.print("THEY ARE EQUAL");
}
Serial.println(inputString);
// clear the string:
inputString = "";
stringComplete = false;
}
}
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
delay (10);
inputString += inChar;
stringComplete = true;
//Serial.println(inputString);
}
}
- you're not calling serialEvent()
- this wont work: inputString += inChar;
create a char array (char inputString[10] and index it for each character you receive.
for(int i = 0; i < 10; i++)
{
inputString[i] = (char)Serial.read();
}
Hope it helps.
you're not calling serialEvent()
No, but the sketch hopefully is. serialEvent() - Arduino Reference
this wont work: inputString += inChar;
Why not?
Hi,
thanks for all,
but here isnt problem to capture data from serial, this is working.
The problem is how to compare string with text
if (stringComplete) {
if (inputString == "0007324804") {
Serial.print("THEY ARE EQUAL");
}
...................
Input string is captured and the comparsion does not work.
thanks
Dan
Have you looked at the input string to check that it is in fact the same?
It's my experience that not all chars arrive at once, so you may be setting Stringcomplete to true before they all arrive, and then doing the test.
Better to have some sort of test for string complete - like a length or delimiter check.
Input string is captured and the comparsion does not work.
Of coarse it is not working, your code is comparing one char at a time, instead of the full string.
It's my experience that not all chars arrive at once, so you may be setting Stringcomplete to true before they all arrive, and then doing the test.
He is going to need to because if they do not match, his code will clear the string.
Serial.println(inputString);
// clear the string:
inputString = "";
stringComplete = false;
He needs to send a period or some special char so that, only when that char is found, will it stop storing and try to match them.
A simple IF/ELSE should fix this.
There's a similar thread running next door which might help!
http://arduino.cc/forum/index.php/topic,165084.0.html
The problem is how to compare string with text
Below is some String comparison info. You might need to set "0007324804" as a String variable and then make the comparison.