I have been trying to work on a project that involves taking inputs from the serial monitor. All I really need is to input a single character, but I have been having issues with what seems like junk being input after whatever character I enter. I was searching around for a solution, and found this post to try to help me. http://forum.arduino.cc/index.php?topic=396450.0
// Example 1 - Receiving single characters
char receivedChar;
boolean newData = false;
void setup() {
Serial.begin(9600);
Serial.println("<Arduino is ready>");
}
void loop() {
recvOneChar();
showNewData();
}
void recvOneChar() {
if (Serial.available() > 0) {
receivedChar = Serial.read();
newData = true;
}
}
void showNewData() {
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(receivedChar);
newData = false;
}
}
Since example 1 in this post is all I really need to input, I tried running the example on my arduino; however, the error persisted even with this code which is making me think it could be a problem with my arduino uno or laptop. Running that example, I input 'A' and then '4', and I copied the output from the serial monitor below:
This just in ... A
This just in ...
This just in ...
This just in ... 4
This just in ...
This just in ...
Although it looks more like this:
This just in ... A
This just in ...
This just in ...
This just in ... 4
This just in ...
This just in ...
Does anyone know why this would be happening? It seems to me like some new line characters are being read and then printed, but I'm not fully sure if that is meant to happen or some sort of issue that I can avoid. I would appreciate any help.
It's probably happening because you have line ending in Serial Monitor (selection at the bottom of the window) set to "Both NL & CR". Change it to "No line ending" if you want keys to be sent immediately.
johnwasser:
It's probably happening because you have line ending in Serial Monitor (selection at the bottom of the window) set to "Both NL & CR". Change it to "No line ending" if you want keys to be sent immediately.
My Serial Input Basics code expects the line-feed character as the line ending.
First, we need to be familiar with various sub-windows/components/fields of the Serial Monitor.
1. Look at the 'Line ending tab' field. When we pop-down, we see the following options: (1) No line ending (2) Newline (3) Carriage return (4) Both NL & CR (both Newline and Carriage return)
2. If we place A in the InputBox, choose 'Newline' option and then click on the Send button (or hit on the Enter key of the PC instead of clicking on Send Button), the following codes (called ASCII codes) are transmitted from the PC towards the UNO one after another.
01000001 (0x41 in hex format) for character A.
00001010 (0x0A in hex format) for 'non-printable character' newline. The newline character brings the cursor at the next line (newline) position.
If we choose 'Carriage return' option, ASCII code of A (0x41) and ASCII code of Carriage return (0x0D) will be transmitted. The carriage return character (non-printable) brings the cursor at the next line position.
If we choose 'Both NL & CR' option, ASCII code of A (0x41), ASCII code of Carriage return (0x0D), and ASCII code of Newline (0x0A) will be transmitted. The NL & CR characters bring the cursor at the next line position.
Someone may kindly explain the especial usefulness of 'Both NL & CR' option.
3. If we place F in the InputBox, choose 'No line ending' option and then click on the Send button, the ASCII code of character F (0x44, 0x46) will only be sent.
GolamMostafa: 3. If we place F in the InputBox, choose 'No line ending' option and then click on the Send button, the ASCII code of character F (0x44) will only be sent.
I counted 3 times on my fingers, and I always came up with 44. Let me have a good answer from my own of how it happened for 0x44 to get the place instead of 0x46 for the ASCII code of F.