I am going through an Arduino Learning Book(978-1-59327-448-1) and I've run into the same issue twice now.
When I enter data into the serial monitor, the number that it reads and displays(when asked to print) is way different than what I'm entering.
In diagnosing the problem, I went to https://www.arduino.cc/en/Serial/Read
Copied the code into a new sketch, and tried to run it.
When I Enter 0, I get 48, and a second line saying 10. Enter 1, I get 49 and 10, etc. Here are the results when I enter 0, then 1, then 2, then 3
I received: 48 I received: 10
I received: 49 I received: 10
I received: 50 I received: 10
I received: 51 I received: 10
here is the code I'm using
int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}
I have the serial monitor set to 9600. All sketches have worked fine so far, including those that use Serial.print functions, but when I use Serial.read, things seem to get screwed up.
I'm sure I'm missing something stupid, but I've googled around and all the answers are much higher level than just trying to run the code in the Arduino.cc example
Thanks in advance for helping a first timer!
PS: I Tried to get all of the code into the grey box with various line spacing and indenting strategies, but it's not perfect. Does anyone have any tips on that?
2 Answers 2
Serial.println(incomingByte, DEC);
You told it to print the incoming byte in decimal, so that is what it did. 48 is the decimal equivalent of ASCII '0'.
Change that line to:
Serial.println((char) incomingByte);
That casts the thing you are printing to a character, and then the appropriate version of Serial.println prints it as a character, not a decimal number.
10 is the decimal equivalent of a new line (newline) so that is why you are also seeing 10 printed.
I need to convert the incoming stuff into integers so that it can see if im guessing right or not
Assuming you mean an integer other than 0 to 9 (like, 42 for example) then you need a bit more work. I have a page about that sort of thing:
http://www.gammon.com.au/serial
Example code:
/*
Example of processing incoming serial data without blocking.
Author: Nick Gammon
Date: 13 November 2011.
Modified: 31 August 2013.
Released for public use.
*/
// how much serial data we expect before a newline
const unsigned int MAX_INPUT = 50;
void setup ()
{
Serial.begin (115200);
} // end of setup
// here to process incoming serial data after a terminator received
void process_data (const char * data)
{
// for now just display it
// (but you could compare it to some value, convert to an integer, etc.)
Serial.println (data);
} // end of process_data
void processIncomingByte (const byte inByte)
{
static char input_line [MAX_INPUT];
static unsigned int input_pos = 0;
switch (inByte)
{
case '\n': // end of text
input_line [input_pos] = 0; // terminating null byte
// terminator reached! process input_line here ...
process_data (input_line);
// reset buffer for next time
input_pos = 0;
break;
case '\r': // discard carriage return
break;
default:
// keep adding if not full ... allow for terminating null byte
if (input_pos < (MAX_INPUT - 1))
input_line [input_pos++] = inByte;
break;
} // end of switch
} // end of processIncomingByte
void loop()
{
// if serial data available, process it
while (Serial.available () > 0)
processIncomingByte (Serial.read ());
// do other stuff here like testing digital input (button presses) ...
} // end of loop
In this example, the function process_data
is called when there is a complete number to be handled.
-
Ok i changed it as you suggested, and got "I received: 1 I received: " so thats working for printing the number i enter, still confused as to why its printing that extra line. Also, when i go back to the guessing game in the book, i need to convert the incoming stuff into integers so that it can see if im guessing right or not.Mechanical-ish– Mechanical-ish10/16/2018 21:11:37Commented Oct 16, 2018 at 21:11
-
1The extra line is the linefeed you are echoing back. You will have configured the terminal monitor to send a linefeed after pressing Send.10/16/2018 22:03:59Commented Oct 16, 2018 at 22:03
You can use Serial.parseInt()
to read a number sent from Serial Monitor. parseInt waits for all numeric characters of the sent text, so it blocks the execution of your sketch for a very short time until all characters are received. At higher baud rate it will be faster. If your sketch doesn't need to loop as fast as possible, then you can use it. parseInt
stops at first character that is not a digit (for example a newline character) and returns the received number as long.
void setup() {
Serial.begin(115200);
}
void loop() {
if (Serial.available() > 0) {
long number = Serial.parseInt();
Serial.println(number);
}
}
Serial.print((char)49);
to coerce it back into1