I am currently working with my professor on putting together ultrasonic and laser rangefinders. We have got the ultrasonic rangefinder working great, but we do not have the laser rangefinder working correctly. We are using a Lightware AL-01 laser rangefinder on an Arduino Uno board and that is connected to our main board (Mega 2560). For some reason the output from the laser is unreadable in our data and shows up as B-99999E and stuff like that. Below is the code for our laser rangefinder. Can someone please tell me if there is a mistake in our code. Thanks
/*
*---------------------------------------------------------
* AL_01_Arduino_Laser_Range_Finder
* Rev0
* 25/06/2012
*
* This program includes the following modules:
*
* 1. AL_01_Arduino_Laser_Range_Finder_Rev0.ino - Top level
* 2. laser.ino - communications with the DS00VQ100 chip and control functions
* 3. serial.ino - USB communications functions
* 4. settings.ino - settings and EEPROM functions
* 5. utilities.ino - other useful functions
* 6. constants.h - constant values available to all functions
* 7. globals.ino - definitions of global variables
* 8. globals.h - makes global variables available to all functions
*
*---------------------------------------------------------
*/
#include "constants.h"
#include "globals.h"
#include <EEPROM.h>
#include <SPI.h>
#include <stdio.h>
#include <avr/pgmspace.h>
#include <Wire.h>
//#include <I2C_Anything.h>
//#include "DHT.h"
//#include <SoftwareSerial.h>
const boolean debug = false;
// I2C Config
const int laserControlI2CAddress = 2;
char LaserRange[6];
String LaserRangeTX = "";
// Hardware pin assignments
// These four transmit each digit of the range distance via PWM values.
const int mmPin = 3; // thousandth-of-meter (millimeter) digit
const int cmPin = 5; // hundredth-of-meter (centimeter) digit
const int dmPin = 6; // tenth-of-meter (decimeter) digit
const int mPin = 9; // meters (meters) digit
const int DkPin = 10; // Tens of meters (dekameters) digit
//const int HeartbeatLEDPin = 11;
// Status readout config
//int HeartbeatBrightness = 0;
int lastLaserRangeInMM = 0;
int MaxLaserRangeInMM = 25000;
String LaserRangeString = "";
/*
// Temperature + humidity sensor
// Hardware sensor config
#define DHTPIN 11 // what pin
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
// Sensor software config
int refreshInterval = 1000; // every 1 seconds
int lastTempHumidReadingTimestamp = 3000; // start checking after 3 seconds
float h = 0.0;
float t = 0.0;
int humInt = 0;
int tempInt = 0;
char temp[5];
char humid[5];
String tempHumidTX = "";
// End Temperature + humidity sensor
*/
void setup()
{
pinMode( mmPin, OUTPUT );
pinMode( cmPin, OUTPUT );
pinMode( dmPin, OUTPUT );
pinMode( mPin, OUTPUT );
pinMode( DkPin, OUTPUT );
delay(3000); // wait for power to stabilize
//initialiseHardware(); // utilities.ino
Serial.begin(9600);
// SPI
pinMode(SPI_CHIPSELECT_PIN, OUTPUT);
digitalWrite(SPI_CHIPSELECT_PIN, LOW);
pinMode(DATA_READYN_PIN, INPUT);
SPI.begin();
intSettings[FOOTER] = ASYNC_FOOTER;
digitalWrite(SPI_CHIPSELECT_PIN, HIGH);
delay(1);
for (int j = 0; j < 14; ++j)
spiRead[j] = SPI.transfer((byte)intSettings[j]);
delay(1);
digitalWrite(SPI_CHIPSELECT_PIN, LOW);
initialiseSettings(); // settings.ino
Wire.begin(laserControlI2CAddress);
Wire.onRequest(requestEvent);
//dht.begin();
// Handle configuration via serial to laptop
if (intSettings[DISPLAY_MODE] == 4)
displaySettingsMenu();
//Serial.println ( "LASER UNIT AWAITING COMMANDS." );
}
void loop()
{
/*
if ( millis() > lastTempHumidReadingTimestamp + refreshInterval ) {
lastTempHumidReadingTimestamp = millis();
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)
h = dht.readHumidity();
t = dht.readTemperature();
humInt = h * 1;
tempInt = t * 1;
if ( debug ) {
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h)) {
Serial.println("Failed to read from DHT");
} else {
dtostrf(t, 9, 1, temp);
dtostrf(h, 5, 1, humid);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
} // End debug output
} // End debug check
} // End refreshInterval check
*/
//lcd.setCursor(0, 1);
//lcd.print(lastLaserRangeInMM);
handleKeypress(); // serial.ino
handleSPIResult(); // laser.ino
lastLaserRangeInMM = (int) (distance * 1000.0); // This step removes the decimal in the distance float.
if ( lastLaserRangeInMM <= 0 ) {
lastLaserRangeInMM = -9999;
} else if ( lastLaserRangeInMM >= MaxLaserRangeInMM ) {
lastLaserRangeInMM = MaxLaserRangeInMM;
}
LaserRangeString = (String)lastLaserRangeInMM;
//Serial.println(LaserRangeString);
int FirstDigit = CharConvertedToDigit(LaserRangeString.charAt(0));
int SecondDigit = CharConvertedToDigit(LaserRangeString.charAt(1));
int ThirdDigit = CharConvertedToDigit(LaserRangeString.charAt(2));
int FourthDigit = CharConvertedToDigit(LaserRangeString.charAt(3));
int FifthDigit = CharConvertedToDigit(LaserRangeString.charAt(4));
int DekameterDigit = 0;
int MeterDigit = 0;
int DecimeterDigit = 0;
int CentimeterDigit = 0;
int MillimeterDigit = 0;
if ( lastLaserRangeInMM < 10 && lastLaserRangeInMM >= 0) {
LaserRangeString = "0000" + LaserRangeString;
//MillimeterDigit = FirstDigit;
} else if ( lastLaserRangeInMM < 100 && lastLaserRangeInMM >= 0 ) {
LaserRangeString = "000" + LaserRangeString;
//CentimeterDigit = FirstDigit;
//MillimeterDigit = SecondDigit;
//Serial.println (LaserRangeString);
} else if ( lastLaserRangeInMM < 1000 && lastLaserRangeInMM >= 0 ) {
LaserRangeString = "00" + LaserRangeString;
//DecimeterDigit = FirstDigit;
//CentimeterDigit = SecondDigit;
//MillimeterDigit = ThirdDigit;
//Serial.println (LaserRangeString);
} else if ( lastLaserRangeInMM < 10000 && lastLaserRangeInMM >= 0 ) {
LaserRangeString = "0" + LaserRangeString;
//DekameterDigit = 0;
//MeterDigit = SecondDigit;
//DecimeterDigit = ThirdDigit;
//CentimeterDigit = FourthDigit;
//MillimeterDigit = FifthDigit;
//Serial.println (LaserRangeString);
} else if ( lastLaserRangeInMM <= MaxLaserRangeInMM ) {
//Serial.println (LaserRangeString);
//DekameterDigit = FirstDigit;
//MeterDigit = SecondDigit;
//DecimeterDigit = ThirdDigit;
//CentimeterDigit = FourthDigit;
//MillimeterDigit = FifthDigit;
} else if (lastLaserRangeInMM < 0 ) {
LaserRangeString = "-7777";
//MillimeterDigit = 128; //"Negative Range: -9999"
//Serial.println (LaserRangeString);
} else {
LaserRangeString = "-8888" ;
}
LaserRangeString = "B" + LaserRangeString + "E";
Serial.println (LaserRangeString);
/*
analogWrite( DkPin, DekameterDigit * 25); //multiply by 25 to use the full possible range of values for analogWrite (0 - 225, anyway...)
analogWrite( mPin, MeterDigit * 25);
analogWrite( dmPin, DecimeterDigit * 25);
analogWrite( cmPin, CentimeterDigit * 25);
analogWrite( mmPin, MillimeterDigit * 25);
*/
}
void requestEvent() //12C data transfer
{
//Serial.print( "Range: " );
//Serial.println( LaserRangeString );
//Serial.println("Incoming I2C request");
String bar = "R";
bar += LaserRangeString;
//bar += lastLaserRangeInMM;
bar += "X";//'\0';
char foo[bar.length()];
bar.toCharArray(foo, bar.length());
Wire.write(foo);
//Wire.write(temp);
//Serial.print("I2C requested, sent: ");
//Serial.println(foo);
}
int CharConvertedToDigit (char input) {
int tempInt;
if( input == 45 ) {
tempInt = 128;
return tempInt;
} else if( input == 48 ) {
tempInt = 0;
return tempInt;
} else if( input == 49 ) {
tempInt = 1;
return tempInt;
} else if( input == 50 ) {
tempInt = 2;
return tempInt;
} else if( input == 51 ) {
tempInt = 3;
return tempInt;
} else if( input == 52 ) {
tempInt = 4;
return tempInt;
} else if( input == 53 ) {
tempInt = 5;
return tempInt;
} else if( input == 54 ) {
tempInt = 6;
return tempInt;
} else if( input == 55 ) {
tempInt = 7;
return tempInt;
} else if( input == 56 ) {
tempInt = 8;
return tempInt;
} else if( input == 57 ) {
tempInt = 9;
return tempInt;
}
}
//void PulseStatusLED () {
// HeartbeatBrightness = (int) (((float)lastLaserRangeInMM / (float)MaxLaserRangeInMM) * 255.0);
// analogWrite(HeartbeatLEDPin, HeartbeatBrightness);
//}