I am beginer in programming, and I need some help to read 2 bytes (msb/lsb) that comes after a request (0x01 to msb and 0x02 to lsb) via serial, and then, make an mathematical operation and display on an 2x16 display. I have the functions of my project that use only 1 byte working good. One example:
I don't know the answer you ask, but if you use "Tools - Auto format" on your code, some smarter than I, may be able to understand and help you easier.
Serial.write(0x06); //request
if (Serial.available() > 0)
The chances that the data you requested are available to read in such a short time are very slim indeed.
You probably need a loop to wait for them to come back.
There is no need to change that as the variable MAP will be a two byte variable.
Unless you are not telling us something. What does that formular do anyway?
The chances that the data you requested are available to read in such a short time are very slim indeed.
You probably need a loop to wait for them to come back.
Alternatively, you can move the delay(600) ;
or simply exchange the two
My friends, I think I didn´t explain very well my problem. The code (function) that I have beed posted, is working, what doesn t works are other similar functions that use 2 bytes instead 1 byte and needs 2 requests, 1 for msb and 1 for lsb (0x01 and 0x02 for example), I am trying to use your suggestions but the function doens t work anyway :. The porpouse is read values from a car ecu. The mathematical operation is the transfer function.
Just think what happens if after you send the request you have not got an answer.
The if statement does not trigger and you read nothing.
You have to wait for the data to arrive. Use a while loop that only terminates when there is a byte to read, that is until serial avaliable gives you a non zero value.