I'm having a problem reading serial bytes.
If I input A
, the code outputs: This just in ... A
.
It's correct, but the problem happens when I input more than 1 character.
Input: AAA
Output:
This just in ... A
This just in ... P
This just in ... �
Device: Arduino Nano
.
No other pins are connected. Only the cable connects to my PC.
My Baud rate is correct, 9600 -> 9600.
My code, from a simple example. the problem can't be the code.
char receivedChar;
boolean newData = false;
void setup() {
Serial.begin(9600, SERIAL_8N1);
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;
}
}
I'm sorry it's in Chinese, The value is by following: 9600, 8, None, 1, None.
And this is the advanced settings tab. the default setting.
So I tried to print the character in binary:
char receivedChar;
boolean newData = false;
void setup() {
Serial.begin(9600, SERIAL_8N2);
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.println(receivedChar, BIN);
newData = false;
}
}
Input: A
Output: 1000001
Input: AAA
Output:
1000001
1010000
11111111111111111111111111010000
The output gets weirder and weirder the longer my input length is.
I have searched like 100 times on Google and most of them makes me know more what I'm dealing with, but none of them solves my problem. I asked ChatGPT, it didn't provide any solution more than check your baud rate
. I even tried using the old IDE, So here I am, finally asking a question here.
I was randomly changing some settings and it worked. I changed the clock divider from 1 to 2, now I can read characters properly.