Shop OBEX P1 Docs P2 Docs Learn Events
28044 Laser Range FInder — Parallax Forums

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

  • What processor are you using? Binary to decimal is a language specific thing.
    Jim
  • Thanks - I should have mentioned using an Arduino mega, but the real question is why did they limit the output to either binary or a sentence string.

    But I would be eternally grateful for a simple routine.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-08-19 17:17
    If you're using an Arduino, I think you will want to use the

    sydeem wrote: »
    Thanks - I should have mentioned using an Arduino mega, but the real question is why did they limit the output to either binary or a sentence string.

    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
    }
    

  • Thanks for your efforts. I wasn't exactly sure how to use your code so I added your two int's to our program and replaced the demo LRF code starting with while(1) and got this output:

    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.
  • Sorry about that. That's what I get for posting untested code.

    I just dug out my Uno and LRF and I also received zeros.

    I'll see if I can figure this out.
  • I was clearing the range before displaying it.

    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 off
    
  • Yep - that works great - Thanks - I am back in business.
  • sydeem wrote: »
    Yep - that works great - Thanks - I am back in business.

    Good to hear. You're welcome.

Sign In or Register to comment.