Xbee sending Maxbotix Proximity Sensor

I finally had some time at work to finish testing the Maxbotix sensor hooked to an Arduino and finally to a Xbee. This tasked turned out to be simple with one hiccup.

I started using the Maxbotix Arduino library and it works great, but when you are using it you cannot use SofwareSerial.h The library comes with a hacked version of softwareserial, so to read your sensor TTL just use the RX and TX on the Arduino, since for my case it’s a simple one remote unit to one receiving unit, this was no issue.

Here is the code on the sender unit

#include <Maxbotix.h>
 
//SoftwareSerial xbee(2, 3); // RX, TX
String inData;
int incomingByte;
void setup (){
  Serial.begin(9600);
}
 
void loop (){
  int pin1 = 2;
  int reading = readSerialMaxBotix(pin1,5);
  Serial.print(reading);
}
 
int readSerialMaxBotix(int pin1,int samples) {
	Maxbotix rangeSensorTX(pin1, Maxbotix::TX, Maxbotix::XL,  Maxbotix::MEDIAN);
	int allinches[samples];
	for (int i = 0;i&lt;samples;i++) {
		int inches = rangeSensorTX.getRange()*0.39370;
		allinches[i] = inches;
	}
	isort(allinches,samples);
	int modE = mode(allinches,samples);
	memset(allinches,0,samples);
	return modE;
}
 
void isort(int *a, int n) {
	// *a is an array pointer function
	for (int i = 1; i &lt; n; ++i) { 		int j = a[i]; 		int k; 		for (k = i - 1; (k &gt;= 0) &amp;&amp; (j &lt; a[k]); k--) {
			a[k + 1] = a[k];
		}
		a[k + 1] = j;
	}
}
 
int mode(int *x,int n){
	int i = 0;
	int count = 0;
	int maxCount = 0;
	int mode = 0;
	int bimodal;
	int prevCount = 0;
	while(i&lt;(n-1)){ 		prevCount=count; 		count=0; 		while(x[i]==x[i+1]){ 			count++; 			i++; 		} 		if(count&gt;prevCount&amp;count&gt;maxCount){
			mode=x[i];
			maxCount=count;
			bimodal=0;
		}
		if(count==0){
			i++;
		}
		if(count==maxCount){//If the dataset has 2 or more modes.
			bimodal=1;
		}
		if(mode==0||bimodal==1){//Return the median if there is no mode.
			mode=x[(n/2)];
		}
    		return mode;
  	}
}

Here is the code on the receiver

#include <SoftwareSerial.h>
SoftwareSerial xbee = SoftwareSerial(2,3);
 
int incomingByte;
String inData;
void setup() {
   Serial.begin(9600);
   xbee.begin(9600);
}
 
void loop() {
    while (xbee.available() &gt; 0) {
        char recieved = xbee.read();
        //inData += recieved; 
        // Process message when new line character is recieved
        if (recieved == '\n' || recieved == '\r' || recieved == '\r\n' || recieved == '\n\r') {
 
                Serial.println(inData);
        	inData = ""; // Clear recieved buffer
        } else {
        	inData += recieved; 
	}
    }   
}

Here is it in action:

–John
Don’t stand to close to the fire!

Leave a Reply

Your email address will not be published. Required fields are marked *