1

I was having some problems on a project, so to test my Arduino, I wrote the following program to check things. When I start the serial monitor, the value of i is 1, but after I start sending numbers, they change into ASCII. Why?

int i = 1;
void setup(){
 Serial.begin(9600);
}
void loop(){
 Serial.println(i);
 if(Serial.available()){
 i = Serial.read();
 }
}
gre_gor
1,6824 gold badges18 silver badges28 bronze badges
asked Oct 6, 2017 at 15:58

1 Answer 1

5

When you send the character 1, Serial.read() returns the integer 49.
Serial.println(i) then sends the characters 4, 9, carriage return and a newline.

If you want to get back the same characters that you sent, change the type of i to char.

answered Oct 6, 2017 at 16:55

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.