I want to send a number between 0 and 120 as a byte over Serial port. I'm working on a project that involves sending ultrasonic sensor readings over Bluetooth to an Android. I would like to send the entire reading as a byte and I think this can be done because an signed byte ranges from -128 to 127. However, the byte received in my Android app doesn't show the correct sensor reading. Here's the relevant code snippet:
while (true){
//ultrasonic sensor sends out signal
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
cm = (duration / 2) / 29.1; // Divide by 29.1 or multiply by 0.0343
inches = (duration / 2) / 74; // Divide by 74 or multiply by 0.0135
// max range is 156 inches, but our app will only display up to 100 inches
inches = min(127, inches);
Serial.print(inches, BIN);
delay(50);
In my Android app written in Java I read the value like this:
b = (byte) inputStream.read();
For example, if inches
is 100 and I send that over Serial port, how can I expect to get the same reading in my Android app?