I have int variable assigned a value 1.
I send a value from bluetooth controller app to Arduino UNO, and if the value is 1, then it will perform an operation.
I have 2 questions:
But what if i send a value of 01 instead of 1? Will it work? Why?
Also, what if i initialize the variable in my Arduino sketch to 01 instead of 1, will it fix the problem?
Both 01 and 1 represent the same value in C and C++. In C and C++ 01 is actually an octal integer literal. But the value of octal 1 is the same as the value of decimal 1, so it makes no difference at all.
What "problem" you are talking about and how you expect this change to "fix" it is not clear to me. No, it won't "fix" anything, since, as I said above, 1 and 01 are the same value.
Basically, i'm asking which values of ASCII or HEX should i send using Bluetooth app if i need to receive the following according to my Arduino sketch:
signed char received_data = -1; //declared as a global variable
void loop(){
if(Serial.available())
received_data = Serial.read();
if(received_data == 1)
serial.println("Data value received is: 1");
}
And also, how does Arduino recognize this conversion automatically? Is it done within the Bluetooth app? Or is it done by the Arduino UNO? Or is it just automatic according to serial communication protocol?
Serial.read() returns a non-negative byte value or -1. If you want to receive a byte with value 1, you have to send a byte with value 1. That's all there's to it. It has nothing to do with "ASCII or HEX".
And the proper type to store the result of Serial.read() is int, not signed char. Don't attempt to use signed char for that purpose.
Only after you verified that the received value is non-negative, you can convert it to unsigned char (or byte), which is the byte you received.
OK, i've done some testing. In the Bluetooth app, i sent a HEX value of 01 and it worked on the Arduino since apparently, the received value was an integer 1.
I am confused about how these values are being automatically converted.
So, i tested further by sending a value of 09 and the received value was an integer 9. But then, i sent 10 and the Arduino didn't respond as i expected it to receive an integer value of 10. So, i dug into hexadecimal to decimal conversion, and i found that the equivalent of 10 in hex is 16 in decimal, so i entered 16 in my Arduino sketch and sent 10 from the Bluetooth app and it worked! But i don't understand how the HEX value is being converted into decimal from the bluetooth app to the Arduino?? Is it being done automatically by the data type 'int' or 'signed char' for that matter since i know that all values sent from the bluetooth app will be within -128 to 127.
cattledog:
What is this app, and why are you even considering sending something other than the the character '1'.
Er... Why wouldn't one consider it? Serial write/read interface is designed for sending and receiving bytes. Bytes. Plain and simple binary data. No connection to any "characters". If you want to send 1, you can send 1. If you want to send '1', you can send '1'. Any byte value, from 0 to 255.
cattledog:
What is this app, and why are you even considering sending something other than the the character '1'.
Check for it with
if(received_data == '1')
The single quotes indicate that the value is an ascii character.
Very interesting. I sent a value of 1 as ASCII from the bluetooth app and edited the sketch to:
if(received_data == '1')
The Arduino received it! It makes sense now. I was lost trying to figure out which part of the whole communication process was the conversion being done. I guess there is no conversion at all, meaning that if the data is sent as ASCII, then it is received as ASCII but it just needs to be represented in the proper format with the correct data type.
I also tried with HEX to experiment so in the app it was displaying without the 0x prefix so that confused me even more, so i sent a hex value of 02 from the app and in the sketch, i set it to:
if(received_data == 0x02)
And it worked!
I also sent a hex value of 10 from the app and in the sketch, i set it to:
if(received_data == 0x10)
And success! Previously, i had to convert it to decimal which has a value of 16.
I am using this app: https://play.google.com/store/apps/details?id=project.bluetoothterminal
There are several Bluetooth terminal apps on the Google Play store but this one works for me (it has a few hiccups sometimes, maybe a problem with buffer?).