28044 Laser Range FInder
Interesting the only output I can find is using either "R" or "B" as a command. One sends out a string message with the distance buried in the output string, the other returns a binary number and I don't know how to change the number to a decimal so I can use it in code. I suppose real programmers and engineers would have no trouble but isn't a good portion of the intended market for hobbyist? How can I get a decimal number from this?

Comments
Jim
But I would be eternally grateful for a simple routine.
I'm not sure there's another option. How would you want the data output to be formatted?
I think the output called "binary" are the number values you want.
The LRF outputs data in a pretty standard way for industrial and hobbyist devices. It's always a bit of a trick converting data transmitted from a sensor into a useful value on the receiver side. I don't see any particular issues with the way the LRF sends data.
I do think it would have been nice if the example included a way of capturing the data in a variable. It doesn't do the user much good having the data in a character string.
I believe there are several ways of solving this. One is the "toInt" function. I think the demo code will require a bit of a rewrite to use "toInt" since (or so I think) the character data needs to be in a string. Here's a link to information about this function.
https://www.arduino.cc/en/Tutorial/StringToIntExample
Another option is to check the incoming data to see if the characters are numbers. If the character is a number then use it to build your variable. I haven't tested this, but I think something like this should work for the main loop section of the program.
void loop() // Main code, to run repeatedly { /* When a single range (R) command is sent, the LRF returns the distance to the target object in ASCII in millimeters. For example: D = 0123 mm */ lrfSerial.print('R'); // Send command digitalWrite(ledPin, HIGH); // Turn LED on while LRF is taking a measurement // Get response back from LRF // See Arduino readBytesUntil() as an alternative solution to read data from the LRF //char lrfData[BUFSIZE]; // Buffer for incoming data //char offset = 0; // Offset into buffer int newInputValue; int range = 0; while(1) { if (lrfSerial.available() > 0) // If there are any bytes available to read, then the LRF must have responded { //lrfData[offset] = lrfSerial.read(); // Get the byte and store it in our buffer newInputValue = lrfSerial.read(); // if (newInputValue == ':') // If a ":" character is received, all data has been sent and the LRF is ready to accept the next command { //previousRange = range; // use previousRange in other parts of the program, previousRange needs to be defined elsewhere range = 0; // reset local range for next read break; // Break out of the loop } else if ((newInputValue >= '0') && (newInputValue <= '9')) { range *= 10; // get ready to add next digit range += newInputValue - '0'; // convert the ASCII character to a number and add it to the value of range. } } } //Serial.println(lrfData); // The lrfData string should now contain the data returned by the LRF, so display it on the Serial Monitor Serial.print("range = "); Serial.print(range); // print the numeric value rather than the characters received Serial.println(); Serial.flush(); // Wait for all bytes to be transmitted to the Serial Monitor digitalWrite(ledPin, LOW); // Turn LED off }Left Angle is 10
range = 0
@
Left Angle is 20
range = 0
@
Left Angle is 30
range = 0
@
Left Angle is 40
range = 0
@
Left Angle is 50
I would send you our complete code but I was afraid to choke up this thread.
I just dug out my Uno and LRF and I also received zeros.
I'll see if I can figure this out.
I just tested this version and it appears to work as expected.
void loop() // Main code, to run repeatedly { /* When a single range (R) command is sent, the LRF returns the distance to the target object in ASCII in millimeters. For example: D = 0123 mm */ lrfSerial.print('R'); // Send command digitalWrite(ledPin, HIGH); // Turn LED on while LRF is taking a measurement // Get response back from LRF // See Arduino readBytesUntil() as an alternative solution to read data from the LRF //char lrfData[BUFSIZE]; // Buffer for incoming data //char offset = 0; // Offset into buffer int newInputValue; int inputNumber = 0; int range; while(1) { if (lrfSerial.available() > 0) // If there are any bytes available to read, then the LRF must have responded { //lrfData[offset] = lrfSerial.read(); // Get the byte and store it in our buffer newInputValue = lrfSerial.read(); // if (newInputValue == ':') // If a ":" character is received, all data has been sent and the LRF is ready to accept the next command { range = inputNumber; // use previousRange in other parts of the program, previousRange needs to be defined elsewhere inputNumber = 0; // reset local range for next read break; // Break out of the loop } else if ((newInputValue >= '0') && (newInputValue <= '9')) { inputNumber *= 10; // get ready to add next digit inputNumber += newInputValue - '0'; // convert the ASCII character to a number and add it to the value of range. } } } //Serial.println(lrfData); // The lrfData string should now contain the data returned by the LRF, so display it on the Serial Monitor Serial.print("range = "); Serial.print(range); // print the numeric value rather than the characters received Serial.println(); Serial.flush(); // Wait for all bytes to be transmitted to the Serial Monitor digitalWrite(ledPin, LOW); // Turn LED offGood to hear. You're welcome.