Software Serial Receiving data incorrectly

Hey all, so now that I’ve gotten this darn BLE module and MIT app inventor to work I’m testing to see if it can receive data for a servo motor.

My setup is having the app send the module a byte value between 0 and 180 depending on where a slider is.
I know that the app is working fine, I did actually get it to work at one point but then I changed one thing and didn’t notice what it was and it stopped. Before it sent me numbers from 0000 to 180000 and that was a problem, trying to map it using “angle = map(BTSerial.read(), 0000, 180000, 0, 180);” Did not work, all it sent me was 0s after.

On the arduino end is this:

#include <SoftwareSerial.h>
SoftwareSerial BTSerial (10,11);
byte angle;


void setup() {
Serial.begin(9600);
BTSerial.begin(9600);
Serial.print("ready"); 
}

void loop() {
if(BTSerial.available()) {
  angle = BTSerial.read();
  Serial.println(BTSerial.read());
}
}

The problem now is that in the serial monitor instead of seeing numbers between 0 and 180 I’m getting numbers from 46 to 56. And they just change randomly throughout the slider.

Dean_Bean:
The problem now is that in the serial monitor instead of seeing numbers between 0 and 180 I'm getting numbers from 46 to 56. And they just change randomly throughout the slider.

first, are you positive you are sending a single byte (i.e.uint8_t)?

read()

returns a char

The char datatype is a signed type, meaning that it encodes numbers from -128 to 127. For an unsigned, one-byte (8 bit) data type, use the byte data type.

try casting the char to a byte:

angle = byte(BTSerial.read());

BulldogLowell:
first, are you positive you are sending a single byte (i.e.uint8_t)?

I'm not entirely sure, I will check on that in a moment

read()

returns a char

try casting the char to a byte:

angle = byte(BTSerial.read());

I tried your code and now it gives me numbers between 0 and 9

Dean_Bean:
I tried your code and now it gives me numbers between 0 and 9

then what are you actually sending?

BulldogLowell:
then what are you actually sending?

I'm having the app read the data back to me that it sends and it's just 0-180, in the Writebyte block

Dean_Bean:
I'm having the app read the data back to me that it sends and it's just 0-180, in the Writebyte block

as an ascii text string?

Your code

void loop() {
if(BTSerial.available()) {
  angle = BTSerial.read();
  Serial.println(BTSerial.read());
}

First you read a byte from your app. Next you read another one that might or might not be there and send it to the serial monitor.

This might be better

void loop() {
if(BTSerial.available()) {
  angle = BTSerial.read();
  Serial.print("angle = "); Serial.println(angle);
}

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

BulldogLowell:
as an ascii text string?

Not sure how to check, but I will read the examples Robin mentioned and see

Robin2:
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

Looking at your guide it still doesn't seem clear how to handle this data sadly. It is also possible to send it as float, int, or string. I'm just not sure which is best

Robin2:
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

Oh! I managed to get the info to come out almost correctly. I'm getting numbers 0-180 now but all individually
For example instead of 140 I get
1
4
0

Dean_Bean:
Oh! I managed to get the info to come out almost correctly. I’m getting numbers 0-180 now but all individually
For example instead of 140 I get
1
4
0

that’s a string of ascii characters… not a byte.

try:

void loop() {
if(BTSerial.available()) {
  angle = BTSerial.parseInt(); //<<<<<<<<<<<<<<
  Serial.print("angle = "); Serial.println(angle);
}

BulldogLowell:
that’s a string of ascii characters… not a byte.

try:

void loop() {

if(BTSerial.available()) {
  angle = BTSerial.parseInt(); //<<<<<<<<<<<<<<
  Serial.print("angle = "); Serial.println(angle);
}

Oh that fixed my problem perfectly!!! Thank you guys so much!!!