Serial.print(stringOne.length());
The result is 11111111. Eight '1', which is correct its size is 8.
But how to change the 11111111 to 8?
And why mine is unlike others that displays a whole number?
Serial.print(stringOne.length());
The result is 11111111. Eight '1', which is correct its size is 8.
But how to change the 11111111 to 8?
And why mine is unlike others that displays a whole number?
You've done something else wrong. Post the whole code that produces this output.
My guess is that you are looping though each char somewhere else for the length of the whole string, and since you have print instead of println, they're all in a line
Hi Delta_G
void receiver()
{
while(esp8266.available()>0)
{
char c = esp8266.read();
StringOne += (String)c;
}
Serial.print(stringOne.lengh());
}
The whole coding is very long. Is it due to the += (String) ?
Hi TheJHumphr,
If I use Serial.println(), it becomes
1
1
1
1
1
1
1
1
Nope. The whole code. Or at least enough of it that it would compile and run and show the same problem. If there are some sections you can cut out and it will still run and do this problem then go ahead and cut those out. But post something complete. My guess is that stringOne is going out of scope between calls and is only ever getting one character in it.
This is what I would expect from a serial communication.
Characters arrive one by one with big gaps between them.
#define esp8266 Serial3
String stringOne;
void setup()
{
Serial.begin(9600);
esp8266.begin(9600);
}
void loop()
{
receiver();
if(stringOne != "")
{
toPrint();
stringOne = "";
}
}
void receiver()
{
while(esp8266.available()>0)
{
char c = esp8266.read();
stringOne += (String)c;
}
}
void toPrint()
{
Serial.print(stringOne.length());
}
Char c only reads a single character
You'll have to keep receiving until either a '\n' or a '\0' character (newline or terminating character)
An example given here
So you read faster than the esp supplies and as a result you only read one chsr at a time.
Somewhere else you clear that stringOne between successive calls to your function.
Change your toPrint method to be like this and I think you will understand better the problem you have.
void toPrint()
{
Serial.print(stringOne);
Serial.print(stringOne.length());
}
Delta_G,
The string and the eight '1's are mixed up for sure. But I still don't really understand the problem. Hmm.
Is it possible to perform counting on the number of '1'? Trying to make it simple first because I am new to Arduino
Your program reads through the input faster than it can be transmitted. You are only reading a char at a time. Do something like this:
void receiver()
{
if(esp8266.available()>0)
{
char c = esp8266.read();
while(c != '\n' || c != '\0'){
stringOne += (String)c;
char c = esp8266.read();
}
}
}
Not exact, but you have to wait until you receive a newline character or a terminating character depending on how you are sending it
Your problem is like this.
You tell the ESP or the serial monitor or something to send 8 bytes over Serial. While it gets its stuff together to send the first byte, your loop function runs about a thousand times over. It keeps calling receive but there's nothing available so nothing happens. stringOne is still "" so nothing gets printed.
Now the first character arrives. WHen receive gets called this time there's one available. It adds it to the string. Nothing else is available yet, and you don't try to read more than one byte anyway, so receive ends. Now loop continues. stringOne is NOT "" anymore. It has one character in it. SO after receive, that if statement is true this time and it calls toPrint, prints the one character. Tells you that it is one character long and returns. Then you clear stringOne and make it back to "" and loop ends and you go back to waiting and loop spins about a thousand times waiting on the next character to arrive just like we were to begin with. SO the next time around when it gets to there it's blank again and you only ever put in one character.
I think a look at Robins's Serial Input Basics thread might do you some good.