Dear All,
I want to send bytes from 0 to 99 from one arduino uno to another arduino nano serially.But when i send 1 then receiver shows 49 on serial monitor,when i send 2 then they shows 50 on reciever .how to manage the code for successfull transmition & reception.i have given both code below.please reply
Code:
//Transmitter code
int senddata = 3;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print((int)senddata);
delay(1000);
}
Code:
//Reciever code
int incomingByte=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available())
incomingByte=Serial.read();
Serial.print(incomingByte);
delay(1000);
}
"1" is a character ASCII coded, it has the decimal value of 49, this is the same as a hex value of 0x31 or a binary value of 0b00110001.
All is a byte containing 8 bits.
The problem is that you send a ASCII coded character but your terminal represents it at decimal value.
No you have to devide whether you want to send your values
* as plain (uncoded) byte with range of 0...255, or (then set your terminal to show decimal values)
* as ASCII string, (with 1 byte for values 0...9 and as 2 byte string for values 10..99, one decimal is one character), then set your terminal to show ASCII characters.
You may also transmit always 2 bytes either "00" to "09" or " 0" to " 9"
Klaus
--- Updated ---
And please read Arduino documentation of "Serial.print" command.
Mind the "human readable ASCII" statement.