Parallax laser range finder need help

so i have a parallax laser range finder but the code i want is not for it to be hooked up to a LCD but to the serial monitor and no matter how much i change it, it wont work

// include the SoftwareSerial library so we can use it to talk to the LRF
#include <SoftwareSerial.h>

#define rxPin 2  // connect to the LRF's SOUT pin
#define txPin 4  // connect to the LRF's SIN pin thru a 4.7k resister

// set up a new serial port
SoftwareSerial lrfSerial =  SoftwareSerial(rxPin, txPin);

int incomingByte;

void setup()  
{
 Serial.begin(9600);
  incomingByte = lrfSerial.read();
  // define pin modes for tx, rx, led pins:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  // set the data rate for the SoftwareSerial port
  lrfSerial.begin(9600);

  // send a U to the LRF so that it will autodetect the speed to communicate with us at (9600)
  lrfSerial.print('U');
  // the LRF will send back a : and a newline, we just read and ignore those here
  lrfSerial.read();
  lrfSerial.read();
  Serial.print(incomingByte);

  delay(3000);
  // send a E to the LRF to adjust camera to current lighting conditions
//  lrfSerial.print('E');
//  delay(1000);
  delay(1500);
}

void loop() 
{
  // send an R to the LRF telling it to take a measurement
  lrfSerial.print('R');

  // read what the LRF sends back 
  char lrfData[32];
  int offset = 0;
  lrfData[0] = 0;
  while(1)
  {
    lrfData[offset]=lrfSerial.read();
    if (lrfData[offset]==':')
    {
      lrfData[offset]=0;
      
      break;
    }
    offset++;
  }
  // print out the data from the LRF (it sends back a string in the form "D = xxxx mm" where xxxx is the distance in milimeters 
  Serial.print(lrfData);

  // delay a tiny bit before the next read
  delay(100);

}
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);

These belong to the SoftwareSerial instance. Quit diddling with them!

  Serial.print(incomingByte);

You haven't valued incomingByte. Why are you printing it?

  while(1)
  {
    lrfData[offset]=lrfSerial.read();

You don't know that there is anything to read. But, you read anyway. Why?

no matter how much i change it, it wont work

The code does something. You expect it to do something. If those two somethings were the same thing, presumably you would not be posting here. But, what either of them is is a mystery.

Well this is a code in transition I guess I forgot to put it back the way I had it and just copied in my hacked code. And please, I ask you for help without a condescending attitude. I'm new to this and I want to be pointed in the right direction.

Source of code is the hack-a-week laser range finder code

Source of code is the hack-a-week laser range finder code

Link?

I'm new to this and I want to be pointed in the right direction.

If you did not write the code you posted, you need to recognize crappy code when you see it.

We can hardly point you in the right direction when we don't know where you are (what that code is not doing that you want it to or what it is doing that you don't want it to do) or where you are going.

ok here is the link

http://hackaweek.com/hacks/?page_id=479

What i want is to get rid of the LCD code and move it to serial monitor then move onto adding it to the MAX7456 OSD using its continuous measurement function and like in my other post all i have for the OSD is the dfraser code period(no mods to the dfraser code). So I'm hoping its enough.

kiaxa:
Well this is a code in transition I guess I forgot to put it back the way I had it and just copied in my hacked code. And please, I ask you for help without a condescending attitude.

The condescending attitude is telling you that you're expected to put in some effort to solve the problem yourself, and by the time you're desperate enough to ask for help you should be in a position to provide a clear minimal sketch that compiles, runs and demonstrates the problem you want solving. Asking people to debug code that has been shredded and hacked about just wastes everyone's time. You're asking strangers to give their time and skills to help you. The least you could do is make life as easy as possible for those people trying to help you.

Ok well sorry for being for being new at something. I'm no professional, I know more on how to make the 328 than I know how to program it. :disappointed_relieved:

Sorry for not actually posting a reply offering help, but would you mind telling me which LRF you have? I wouldn't mind getting one myself and having a play-around.

im using the parallax LRF but be careful it does not seem to play nice with the arduino (at least from what i found)

Kiaxa-
Did you ever figure this out? I have the Parallax LRF and have it setup with basically this same code. I can see it taked the R command and it starts flashing the laser. If I disconnect the SOUT pi from my Aurdrino it stopss the cycle and stops flashing. This tells me it is receiving commands and the Aurdrino is able to stay in the loop as long as it receives data so the LRF must be good. It seems like it just can’t pull data to print tomy serial monitor? If you have any examples or advice t would be greatly appriciated;) Here is what I currently have setup-

/*
Parallax LRF to serial print
 */


#include <SoftwareSerial.h>
// The SoftwareSerial library enables the use of Serial communications
// on pins other than 0 and 1

#define rxPin 4  // connect to the LRF's SOUT pin
#define txPin 3  // connect to the LRF's SIN pin thru a 4.7k resister

// set up a new serial port
SoftwareSerial lrfSerial =  SoftwareSerial(rxPin, txPin);

// Create a SoftwareSerial instance for the SerLCD:
// since the LCD does not send data back to the Arduino, we should only define 
// the txPin

void setup() {
  

  
  // define pin modes for tx, rx, led pins:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  // set the data rate for the SoftwareSerial port
  lrfSerial.begin(9600);
   Serial.begin(9600);
  delay(100);

  // send a U to the LRF so that it will autodetect the speed to communicate with us at (9600)
  lrfSerial.print('U');
  // the LRF will send back a : and a newline, we just read and ignore those here
  lrfSerial.read();
  lrfSerial.read();

  //print splash screen
  Serial.print("ProSag");
 

  delay(3000);
  // send a E to the LRF to adjust camera to current lighting conditions
//  lrfSerial.print('E');
//  delay(1000);
  //tell the user that the LRF is ready  
  Serial.print("LRF ready.");
  delay(1500);

  // Print a message to the LCD.
  Serial.print("Race Sag:");


}

void loop() 
{
  // send an R to the LRF telling it to take a measurement
  lrfSerial.print('R');

  // read what the LRF sends back 
  char lrfData[32];
  int offset = 0;
  lrfData[0] = 0;
  while(1)
  {
    lrfData[offset]=lrfSerial.read();
    if (lrfData[offset]==':')
    {
      lrfData[offset]=0;
      break;
    }

  }
  // print out the data from the LRF (it sends back a string in the form "D = xxxx mm" where xxxx is the distance in milimeters 
  
  Serial.print(lrfData);

  // delay a tiny bit before the next read
  delay(100);
}

LCD's are cheap... Why not? make it work on an LCD device first them transfer it to the serial monitor... which is MOST easy to do and there are samples of working code all about you both in your IDE and here in the forum... Like the guy said, You Need to do some work first... None of us here participating are Magicians... Nor are we paid assistants. Had you but looked first you wouldn't have the problem you now have.

Bob

Has anything come about from this?

I recently purchased one of these units to prototype a blind spot detection device i’m developing.

Without butchering the original code i have got it working, however, it seems to be only displaying a read-out once i extend one of the delays (highlighted below), i’m scratching my head at reasons why anything <4s wont display on the LCD or send a signal to the LRF?

void loop()
{
// send an R to the LRF telling it to take a measurement

lcd.setCursor(0, 0);
lcd.print(“Distance in mm”);
delay(4000);
lcd.clear();
lrfSerial.print(‘R’);

Any help would be greatly appreciated.

/*
 Simple LRF Demo
 by Roy Eltham and Dino Segovis
 */

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

// include the SoftwareSerial library so we can use it to talk to the LRF
#include <SoftwareSerial.h>

#define rxPin 2  // connect to the LRF's SOUT pin
#define txPin 3  // connect to the LRF's SIN pin thru a 4.7k resister

// set up a new serial port
SoftwareSerial lrfSerial =  SoftwareSerial(rxPin, txPin);

void setup()  
{
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);

  // define pin modes for tx, rx, led pins:a
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  // set the data rate for the SoftwareSerial port
  lrfSerial.begin(9600);

  // send a U to the LRF so that it will autodetect the speed to communicate with us at (9600)
  lrfSerial.print('U');
  // the LRF will send back a : and a newline, we just read and ignore those here
  lrfSerial.read();
  lrfSerial.read();

  //print splash screen
  lcd.print("BlindSpot");
  lcd.setCursor(0, 1);
  lcd.print("Detection System");

  delay(1000);
  lcd.clear();
  // send a E to the LRF to adjust camera to current lighting conditions
  lrfSerial.print('E');
  delay(1000);
  //tell the user that the LRF is ready  
  lcd.print("BSDS ready.");
  delay(1000);
  lcd.clear();

   //Print a message to the LCD.
  lcd.print("Distance in mm");
}

void loop() 
{
  // send an R to the LRF telling it to take a measurement
  
  lcd.setCursor(0, 0);
  lcd.print("Distance in mm");
  delay(4000);
  lcd.clear();
  lrfSerial.print('R');
  // read what the LRF sends back 
  char lrfData[32];
  int offset = 0;
  lrfData[0] = 0;
  while(1)
  {
    lrfData[offset] = lrfSerial.read();
    // if we get a : character that indicates the end of the read data (and that the LRF is ready for the next command)
    if (lrfData[offset] == ':')
    {
      lrfData[offset] = 0; // null terminate the string of bytes and break out of the loop
      break;
    }
    offset++;
  }

  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);

  // print out the data from the LRF (it sends back a string in the form "D = xxxx mm" where xxxx is the distance in milimeters 
  lcd.print(lrfData);

  // delay a tiny bit before the next read
  delay(100);

//  //integer to convert lrfData string to an integer value
//  int distance = atoi(&lrfData[4]);
//  //sound the alert if the distance is less than one meter
//  if (distance < 1000)
//  {
//    tone(5, 440, 200);
//    delay(200);
//  }
}

LRF_WORKING.ino (2.6 KB)

  while(1)
  {
    lrfData[offset] = lrfSerial.read();
    // if we get a : character that indicates the end of the read data (and that the LRF is ready for the next command)
    if (lrfData[offset] == ':')
    {
      lrfData[offset] = 0; // null terminate the string of bytes and break out of the loop
      break;
    }
    offset++;
  }

As fast as you can, read data that might not be there, overflowing the buffer like mad. Break out of the loop when the colon finally trickles in, after storing several million bytes in an array than can cold 32. How's that working for you? Never mind, I think I know.