Shop OBEX P1 Docs P2 Docs Learn Events
SoftwareSerial conflict with Parallax Laser Rangefinder? — Parallax Forums

SoftwareSerial conflict with Parallax Laser Rangefinder?

tedkurtztedkurtz Posts: 1
edited 2015-02-21 13:15 in Accessories
I am am having a problem trying to use two SoftwareSerial ports on my project, one port to communicate with a Parallax Laser Rangefinder and the other port to communicate with a servo controller. I get my modification of the Parallax Arduino demo code to work OK as is, but if I modify it simply by adding the line "SoftwareSerial ServoSerial(12, 13);" the rangefinder does not work. Here is my code:

Code:
/*

Modified Arduino Laser-Ranger-Finder example from Parallax.com
Laser Range Finder Module: Basic Demonstration

Author: Joe Grand [www.grandideastudio.com]
Contact: support@parallax.com

Modified by Ted Kurtz to use functions for initializing and getting data for use in
his Laser_Guided_Hexapod program
Converted to subroutines by Ted Kurtz 19Jan20154
This works OK only if ServoSerial not defined! 19Feb2015

*/

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

#define Servo_OUT 12 //Output pin for (SSC32 RX) on BotBoard (Yellow)
#define Servo_IN 13 //Input pin for (SSC32 TX) on BotBoard (Blue)
#define Servo_BAUD 38400 //SSC32 BAUD rate
//SoftwareSerial ServoSerial(Servo_IN, Servo_OUT);

#define rxPin 10 // Serial input (connects to the LRF's SOUT pin)
#define txPin 11 // Serial output (connects to the LRF's SIN pin)
#define laserBaud 9600

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

void initializeLaser(){
// define pin modes
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
// setup Arduino Serial Monitor
Serial.begin(57600);
while (!Serial); // Wait until ready
Serial.println("\n\nParallax Laser Range Finder");
// set the baud rate for the SoftwareSerial port
laserSerial.begin(laserBaud);
Serial.print("Waiting for the LRF...");
delay(2000); // Delay to let LRF module start up
laserSerial.print('U'); // Send character
while (laserSerial.read() != ':'); // When the LRF has initialized and is ready, it will send a single ':' character, so wait here until we receive it
delay(10); // Short delay
laserSerial.flush(); // Flush the receive buffer
Serial.println("Ready!");
Serial.flush(); // Wait for all bytes to be transmitted to the Serial Monitor
}

int getLaserRange(){ // Measure the range and convert it to a number for calculations
char lrfData[16]; // Buffer for incoming data
char offset = 0; // Offset into buffer
int ctr = 0;
char *lrfDataPointer = lrfData; // Pointer to lrfData[0]
char rangeString[5]; // 4 ascii characters plus '\0' terminator
char term;
lrfData[0] = 0; // Clear the buffer
laserSerial.listen(); // Have to enable this port!!!! Won't work otherwise
laserSerial.print('R'); // Send command to laser range finder
while(1){
if (laserSerial.available() > 0){ // If there are any bytes available to read, then the LRF must have responded
lrfData[offset] = laserSerial.read(); // Get the byte and store it in our buffer
if (lrfData[offset] == ':'){ // If a ":" character is received, all data has been sent and the LRF is ready to accept the next command
lrfData[offset] = 0; // Null terminate the string of bytes we just received
break; // Break out of the loop
}

offset++; // Increment offset into array
if (offset >= 16) offset = 0; // If the incoming data string is longer than our buffer, wrap around to avoid going out-of-bounds
}
else{
ctr++;
}
}
Serial.println(lrfData); // The lrfData string should now contain the data returned by the LRF, so display it on the Serial Monitor
Serial.flush(); // Wait for all bytes to be transmitted to the Serial Monitor
delay(500);
ctr = 0; // Look for 4 decimal digits in the lrfData string
unsigned int range = 0; // Initialize range
for(int i = 0; i < 16; i++){ // Pull decimal digits out of string)
term = *(lrfDataPointer +i); // Get ascii code for a character
if(term >47 && term < 58){ // Look for ascii codes for decimal digits
rangeString[ctr] = term; // Save it if it is a number
ctr++;
}
}
rangeString[ctr] = '\0'; // Terminate the string
range = atoi(rangeString); // Convert to integer
// Serial.print("Range: "); Serial.println(range); Serial.println();
return range;
}

void setup(){ // Set up code called once on start-up
initializeLaser();
}

void loop(){ // Main code, to run repeatedly
int laserRange = getLaserRange();
Serial.print("laserRange: "); Serial.println(laserRange); // Prove that it is a number
Serial.println();
}

End of code:

If anyone finds any of this code useful, feel free to use it.

Does anyone know how I can use two SoftwareSerial ports?

Thank you,

Ted
Sign In or Register to comment.