Hi,
I'm using a TF40 UART sensor with an arduino UNO. As the name suggest it uses UART as communication protocol. But i can't figure out how to use it. I would like to know how to send data and recieve data in a more general way.
The program should work as followed:
- Define commando's
- Set baudrate
- Give a test reading using Trig_single during the void setup.
- Use Trig_successive to write 5 measurements each second to the serial monitor.
This is what i have right now:
#include <SoftwareSerial.h>
// Define TF40 UART pins
#define TF40_RX_PIN 0 // Connect TF40 UART RX pin to Arduino TX pin
#define TF40_TX_PIN 1 // Connect TF40 UART TX pin to Arduino RX pin
// Create a SoftwareSerial object for TF40 UART communication
SoftwareSerial tf40Serial(TF40_RX_PIN, TF40_TX_PIN);
// Command frame format:
// 1byte: Adress code; 1byte: Function code; 2Bytes: Starting address;
// 2Bytes: Number of registers (N); 2Bytes: CRC
// Data frame format:
// 1byte: Adress code; 1byte: Function code; 1Byte: Byte count;
// 2Bytes: Register value; 2Bytes: CRC
byte Baud_rate[13] = {0x01, 0x10, 0x00, 0x00, 0x00, 0x02, 0x04, 0x00, 0x01, 0xC2, 0x00, 0xF3, 0x0F};// Controle baud rate
byte Open_laser[11] = {0x01, 0x10, 0x00, 0x03, 0x00, 0x01, 0x02, 0x00, 0x01, 0x67, 0xA3}; // Open Beam
byte Close_laser[11] = {0x01, 0x10, 0x00, 0x03, 0x00, 0x01, 0x02, 0x00, 0x00, 0xA6, 0x63}; // Close Laser
byte Trig_single[8] = {0x01, 0x03, 0x00, 0x0F, 0x00, 0x02, 0xF4, 0x08}; // Measure once
byte Trig_successive[8] = {0x01, 0x03, 0x00, 0x01, 0x00, 0x02, 0x95, 0xCB}; // Successive measurment (5Hz)
byte read_measure_data[8] = {0x01, 0x03, 0x00, 0x0F, 0x00, 0x02, 0xF4, 0x08}; //Read measuring distance (don't know what to do with this)
void setup() {
// Baudrate setup
// Serial -> Communicating to PC Serial Monitor
Serial.begin(115200); // Tells Arduino the baudrate
tf40Serial.write(Baud_rate,13); // Sends baudrate to TF40 UART
delay(1000);
Serial.println("==========");
Serial.println("Setup: Baudrate send.");
// Controle measurment
Serial.println("Setup: Controle measurement...");
tf40Serial.write(Close_laser,11);
delay(200);
tf40Serial.write(Trig_single,8);
readMeasurement();
Serial.println("Setup: Controle measurement completed");
delay(2000);
Serial.println("Setup: Successive measurement started...");
}
void loop(){
//Serial.write(Open_laser,11);
//Serial.println("Laser opened");
for (int i = 0; i < 5; i++) {
tf40Serial.write(Trig_successive,8);
delay(200); // Wait for measurement interval (5Hz) (Is this needed? since the device knows it has to do 5 reading in 1 second)
readMeasurement();
}
}
// This part i got from ChatGPT ¯\_(ツ)_/¯
void readMeasurement() {
byte buffer[4];
tf40Serial.readBytes(buffer, sizeof(buffer));
// Convert received data to millimeters
uint32_t distance = (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
// Print measured distance
Serial.print("Measured distance: ");
Serial.print(distance);
Serial.println(" mm");
}
At the moment this code gives an error that i use to many bytes and leave to few bytes for local variables, how do i reduce this? There are more functions given by the manufactor if i add these will it not decrease the amount of bytes for local variables even more? I have no idea if the decoding part is done right since i got that from chatGPT.
I hope this all made sense, i'm new to arduino so this sketch is probably full of inefficiency and errors. All help is appreciated, thank you in advance.