Case: I've a simple sketch: I send a number from serial monitorto arduino, arduino does a simple add (c=c+1) and then arduino sends the result to serial monitor.
Problem: The result printed on serial monitor show de value c+1 but it also print '1'. For exemple: if I write 22 on serial monitor, arduino sends 231; if I write 55 on serial monitor, arduino sends 561. This '1' appears 1s after de result c+1 on serial monitor. I don't want this '1' be shown or sent to serial monitor. I think the problem is connected with Serial.parseInt() command because this problem not happens with Serial.read() when receiving strings.
Obs: I'm using a Arduino MEGA 2560.
This is the code:
void setup() {
Serial.begin(9600);
Serial.write("Write the number: ");
}
void loop() {
if (Serial.available())
{
int c=Serial.parseInt();
c=c+1;
Serial.print(c);
}
}
Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn't show the posting toolbar then you can just manually add the code tags: [code]`` [color=blue]// your code is here[/color] ``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.
Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you're using the Arduino Web Editor you will not have access to this useful tool. I recommend you to use the standard IDE instead.
By the way, if you change one line of your sketch from Serial.print() to Serial.println() as below, then you'll see the spurious 1 is on a line of its own, so it's a separate 1 not actually part of a 231. It's adding 1 to the 0 it's seeing as the line end sent by the monitor and treating that as a new request to add and print.
When you have the line ending set to CR and LF, the CR terminates the parseInt() function, and returns the string converted to an int with 1 added to it.
Then, the LF is processed by the next iteration of loop(), ending the parseInt() function again. The data that it then has to convert to an int is "". As an int, that value is 0, and 0 + 1 is 1.
well, I understood but will this '1' be transferred to a device like a part of data? For example: if the result c + 1 is transferred (no more to Serial Monitor but) to a bluetooth device/wireless device/desktopCOMport, does the data sent carry this '1'? This is my main care.
Look at the result returned by parseInt() before printing it or doing any calculations. Is zero a valid value for your system? If it's not then you can just ignore zeros.
If you don't know if your system is sending CR or LF or both, then find out. Then find out what parseInt() does with that. Then write your program to take account of that.
Actualy, I want to receive a INT number from serial monitor and do calculation with this number. "int c=Serial.parseInt();" was the only way I found to do that. The main problems are if the variable 'c' carry this '1' or this '1' is sent to a bluetooth/wireless/another communication device. It can cause troubles both in calculation as communication. If there's another way (easy way) to receive a INT variable from Serial Monitor it could resolve my problem.
Well, I expected a easy way HAHA. There are many commands in "Example 4 - Receiving a single number from the Serial Monitor" I don't know but your code work very well for my case. Thanks guys.