-
Notifications
You must be signed in to change notification settings - Fork 2
/
LRF50M3PS-samplecode.ino
52 lines (43 loc) · 1.7 KB
/
LRF50M3PS-samplecode.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//NOTE: before uploading code, it may be required to disconnect Tx/Rx cables,
//then reconnecting those cables after a successful upload to the Arduino board
//Additionally, ensure the Tx/Rx into the laser rangefinder module are at 3.3V,
//the Arduino Tx/Rx are typically at 5V, thus a logic converter may be needed.
#include <Arduino.h>
//Intialize the various laser rangefinder commands
//Commands not used in the sample code are commented out
//char confirmationComm[] = "$00023335&";
char laserOff[] = "$0003260029&";
char laserOn[] = "$0003260130&";
char singleMeasurement[] = "$00022123&";
//char continuousMeasurement[] = "$00022426&";
//intialize distance converter function
int distanceInterpreter(String data);
void setup(void) {
//Serial.begin defaults to 8N1 with atimeout of 1000 milliseconds
//Baud rate is 115200
Serial.begin(115200);
//#Laser OFF (Confirm LRF is in default off state)
Serial.write(laserOff);
//Laser ON
Serial.write(laserOn);
delay(1000);
}
void loop(void) {
Serial.write(singleMeasurement);
delay(1000);
//Read the Data
String data = Serial.readStringUntil('&');
if(data == "$00023335") {
//Do nothing, the act of reading the confirmation will clear it from the Serial buffer
Serial.println("\nCommand Received");
} else if(data.length()==17) {
Serial.println("Data:" + data + ", Distance:" + (String)distanceInterpreter(data) + "mm");
}
}
int distanceInterpreter(String data){
//Distance is stored in data[7:15], thus need to extract that portion
//For LRF50M3PS, distance is stored as a decimal representation
int distance = 0;
distance += data.substring(7,15).toInt();
return distance;
}