i want to get a string from serial monitor, using that string , i want to reprint each and every character in d string using charAt(), the program compiles and runs, the problems comes in the output, while reprinting an unsupported character is displayed ( they are like a box )
int serial_string(String str)
{
while (Serial.available())
{
delay(10);
if (Serial.available() > 0)
{
char c = Serial.read();
str += c;
}
}
int n = str.length();
return n;
}
void loop()
{
if (Serial.available())
{
String str;
int n = serial_string(str);
for ( int i = 0 ; i < n ; i++ )
{
Serial.println((char)str.charAt(i));
}
}
}
this is d code i used and the output i got i have attached a picture.
In your loop() function, you:
Create an uninitialised local to loop() String variable called str.
Pass that to your serial_string() function, which:
Creates its own local String variable, also called str.
Appends whatever comes in on Serial to the local to serial_string() str.
Gets the length of the local to serial_string() str.
Returns the length of the local to serial_string() str, then throws the local to serial_string() str away, as it goes out of scope.
Back in loop(), you then:
For the number of characters in the now discarded local to serial_string() str,
Print the character at position i of the uninitialised local to loop() String variable called str.
while ( i++ < n )
{
int choice = char_type(i,str.charAt(i));
Serial.println(choice);
}
int char_type(int &i,char ch)
{
String temp;
if (ch == ' ') // if the character is a blank space
{
while ( str.charAt(++i)==' ') //till the next blank space
{
You are incrementing i in the while loop AND in the function called in the while loop. Please tell me that there is a reason for that.
You are mixing postfix increment and prefix increment operator usage. Please tell me that there is a reason for that.
You are creating and discarding a String instance in the function. Please tell me that there is a reason for that.
the words are "and","for","the","with","of". i need to collect these words and pass it to a function.
Collect them from what? Pass them to what function? Removing them from the original location, or leaving them in place?
You might look at the indexOf() and substring() methods of the String class.
PaulS:
You are incrementing i in the while loop AND in the function called in the while loop. Please tell me that there is a reason for that.
You are mixing postfix increment and prefix increment operator usage. Please tell me that there is a reason for that.
You are creating and discarding a String instance in the function. Please tell me that there is a reason for that.
prefix increment in char_type is because if the when the " " is encountered the while loop increments "i" till the " " is encountered, and the postfix increment is for normal iteration through the string.
The creation of an instance of the String in the function char_type is just for appending the character String temp ( small change in code instead of ++i, i changed it to ++j (where j = i))...
am pretty much confused now...
have any solutions...
arkks:
thank u., it works., is there a way to separate out words like 'the', 'of' from the string???
One way is to make an array with a list of words to ignore. Then as you parse the entry string you compare parsed words with those in the list and only print what is not matched.
Keep an eye on ram use though. Using C++ Strings can be a great way to 'mysteriously' crash your sketch.
BTW, it's nice to be sure but check the same thing twice?
int serial_string(String str)
{
while (Serial.available()) // only loops if Serial.available() is > 0
{
delay(10);
if (Serial.available() > 0) // so why check what must be true to have reached this line of code?
{
people, i tried soo many things and i ended up in even more problem during execution...
the purpose of this code is that : i need to get a string from serial monitor , i need to return a particular number(int) for every alphabet read or number read... this is no sweat., the place where i face problem is - added to this i need to return a unique number for words like "and","for","of","the","with" - when i try to implement this only, i face a lot of problem...
I'm not always up to writing what's going to take a lot of writing to explain so I'll try and lay out the basics and if you want to go the route then have patience with me.
You can go the beginners-intermediate PC route that revolves around steps:
1 - collect the input
2 - parse the input
3 - evaluate the parsed pieces
4 - assemble output based on the evaluations
5 - print output
That's what they teach programmers to do and the basis goes back to about 1890 card sorting. It's not a bad approach in that -it works- and it fits on PC's for as long as there have been PC'[s.
You can take a different view:
serial input that comes in 1 character at a time
evaluate/compare each character as it comes in
there are -plenty- of cycles between serial characters to do this
If I have a 2D char array of words to check against, say in alphabetic order:
a
i f
o f
s o
t h a t
t h e
t h e r e
w h e n
and a new word coming in (perhaps the last character was a space so the next is a new word) and the first character is a 't' then;
I look at the first character of each of my word list until I find the first that starts with 't' and keep a pointer or index to that list word. If I don't find a match then the word coming in through serial -can't- be on the list. If for example the new serial character is an 'm' then reaching a list word that starts with 'n' or higher would be cause for me to flag no match and leave the word-match routine.
But I found a list word beginning with 't' and saved my place. I return from loop() to run loop() again until the next serial character becomes available to check against the next letter in the list word.
branch:
If it matches then I have two letters that match and keep trying to match letters until I have a full match and serial gave me a space or End Of Line to signify that the word coming through serial ends with the same number of characters as the list word (don't want to match list word "the" with "theocracy", do you?) and have a complete match to process.
If the next character doesn't match then the next list word is checked against -all- the characters collected and if it does then continue (to match the list word "there" will always involve a failed match of the list word "the" when the 'r' comes in and subsequent checking of serial-input "ther" against the first four characters of list word "there") trying to match that list word to input.
If you can't match input -so far- then you have no match, end the matchup routine.
If this doesn't seem simple enough then good luck parsing Whole Words with various string commands. My view since over 30 years ago is it's really simpler and faster to compare characters and positions than words but your mileage may vary. A good look at the string commands will show you that they all work character by character -- how about that? XD
GoForSmoke:
I'm not always up to writing what's going to take a lot of writing to explain so I'll try and lay out the basics and if you want to go the route then have patience with me.
You can go the beginners-intermediate PC route that revolves around steps:
1 - collect the input
2 - parse the input
3 - evaluate the parsed pieces
4 - assemble output based on the evaluations
5 - print output
That's what they teach programmers to do and the basis goes back to about 1890 card sorting. It's not a bad approach in that -it works- and it fits on PC's for as long as there have been PC'[s.
You can take a different view:
serial input that comes in 1 character at a time
evaluate/compare each character as it comes in
there are -plenty- of cycles between serial characters to do this
If I have a 2D char array of words to check against, say in alphabetic order:
a
i f
o f
s o
t h a t
t h e
t h e r e
w h e n
and a new word coming in (perhaps the last character was a space so the next is a new word) and the first character is a 't' then;
I look at the first character of each of my word list until I find the first that starts with 't' and keep a pointer or index to that list word. If I don't find a match then the word coming in through serial -can't- be on the list. If for example the new serial character is an 'm' then reaching a list word that starts with 'n' or higher would be cause for me to flag no match and leave the word-match routine.
But I found a list word beginning with 't' and saved my place. I return from loop() to run loop() again until the next serial character becomes available to check against the next letter in the list word.
branch:
If it matches then I have two letters that match and keep trying to match letters until I have a full match and serial gave me a space or End Of Line to signify that the word coming through serial ends with the same number of characters as the list word (don't want to match list word "the" with "theocracy", do you?) and have a complete match to process.
If the next character doesn't match then the next list word is checked against -all- the characters collected and if it does then continue (to match the list word "there" will always involve a failed match of the list word "the" when the 'r' comes in and subsequent checking of serial-input "ther" against the first four characters of list word "there") trying to match that list word to input.
If you can't match input -so far- then you have no match, end the matchup routine.
If this doesn't seem simple enough then good luck parsing Whole Words with various string commands. My view since over 30 years ago is it's really simpler and faster to compare characters and positions than words but your mileage may vary. A good look at the string commands will show you that they all work character by character -- how about that? XD
can give a pseudo code, so that i can be even more clearer...
I don't have that much time to spare. That post took long enough. Either you get the idea of working with one letter at a time or you don't. It might help if you spend time with paper and pencil just working out the mechanics you would use. Use Scrabble pieces if it makes things easier.