Devices & Components
10 jumper wires 150mm male
Arduino Uno Rev3
TF-Luna LiDAR Sensor
XBee S2C Module
Software & Tools
Arduino IDE
Digi XCTU
Project description
Code
distance_xbee_01
cpp
1#include "SoftwareSerial.h" 2#include <Wire.h> 3 4byte deviceAddress = 0x10; // The address of the TF-Luna device is 0x10 5SoftwareSerial XBee(2,3); 6void setup() { 7 Wire.begin(); // The I2C bus communication starts 8 Serial.begin(9600); 9 XBee.begin(9600);// Example Set the baud rate of the serial port to 115200 10} 11 12void loop() { 13 Wire.beginTransmission(deviceAddress); // The I2C data transmission starts 14 Wire.write(0x00); // Send command 15 Wire.endTransmission(); // The I2C data transfer is complete 16 17 Wire.requestFrom((uint8_t)deviceAddress, (uint8_t)7); // Read 7 bytes of data 18 19 if (Wire.available() == 7) { // 7 bytes of data are available 20 byte data[7]; 21 for (int i = 0; i < 7; i++) { 22 data[i] = Wire.read(); // Read data into an array 23 } 24 25 unsigned int distance = (data[1] << 8) | data[0]; // DistanceValue 26 unsigned int signalStrength = (data[3] << 8) | data[2]; // signal strength 27 28 Serial.print("Distance: "); 29 Serial.print(distance); 30 Serial.print(" cm \n"); 31 int A = distance; 32 char distanceStr[10]; 33 snprintf(distanceStr, sizeof(distanceStr), " D %d\n", A); 34 35 Serial.println(distanceStr); 36 37 XBee.write(distanceStr); 38 delay(1000); 39 } 40 41 42}
Comments
Only logged in users can leave comments