Untitled

 avatar
unknown
plain_text
5 months ago
3.6 kB
3
Indexable
#include <SoftwareSerial.h>

//#define HORN_PIN 2 // Pin for activating the horn
//#define RED_LIGHT_PIN 3 // Pin for the red light
//#define GREEN_LIGHT_PIN 4 // Pin for the green light
#define TF02_RX_PIN 10 // Pin for TF02 sensor RX
#define TF02_TX_PIN 11 // Pin for TF02 sensor TX

SoftwareSerial tf02Serial(TF02_RX_PIN, TF02_TX_PIN); // SoftwareSerial instance for TF02 sensor

const int NUM_READINGS = 5; // Number of readings to average
uint16_t distanceReadings[NUM_READINGS]; // Array to store distance readings
int currentIndex = 0; // Index for storing the current reading

void setup() {
  Serial.begin(9600);
  //pinMode(HORN_PIN, OUTPUT);
  //pinMode(RED_LIGHT_PIN, OUTPUT);
  //pinMode(GREEN_LIGHT_PIN, OUTPUT);

  //digitalWrite(RED_LIGHT_PIN, LOW); // Turn off red light initially
  //digitalWrite(GREEN_LIGHT_PIN, HIGH); // Turn on green light initially

  tf02Serial.begin(115200); // Initialize serial communication for TF02 at baud rate 115200

  // Initialize distance readings array
  for (int i = 0; i < NUM_READINGS; i++) {
    distanceReadings[i] = 0;
  }
}

void loop() {
  //uint16_t distance = readDistanceFromTF02(); // Read distance measurement from TF02
  //Serial.print("Distance: ");
  //Serial.print(distance);
  //println(" cm");
  setBaudRate(9600);
  delay(20000);
  // Update distance readings array
  //distanceReadings[currentIndex] = distance;
  //currentIndex = (currentIndex + 1) % NUM_READINGS;

}

uint16_t readDistanceFromTF02() {
  // Send command to request distance data
  tf02Serial.write((uint8_t)0x5A); // Command to request distance data
  tf02Serial.write((uint8_t)0x05); // Data length
  tf02Serial.write((uint8_t)0x00); // Data byte 1
  tf02Serial.write((uint8_t)0x00); // Data byte 2
  tf02Serial.write((uint8_t)0x00); // Data byte 3
  tf02Serial.write((uint8_t)0x01); // Data byte 4

  // Read response
  uint8_t response[9];
  tf02Serial.readBytes(response, 9);

  // Check if response is valid
  if (response[0] == 0x59 && response[1] == 0x59) {
    // Extract distance data
    uint16_t distance = (response[3] << 8) | response[2]; // Combine high and low bytes
    return distance;
  } else {
    return 0; // Invalid response, return 0 distance
  }
}

void setBaudRate(int baud) {
  byte command[8] = {0x5A, 0x08, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00};

  switch (baud) {
    case 9600:
      command[4] = 0x25;
      command[5] = 0x80;
      break;
    case 19200:
      command[4] = 0x4B;
      command[5] = 0x00;
      break;
    case 38400:
      command[4] = 0x96;
      command[5] = 0x00;
      break;
    case 115200:
      command[4] = 0xC2;
      command[5] = 0x01;
      break;
    default:
      Serial.println("Nieobsługiwany baud rate!");
      return;
  }

  // Oblicz sumę kontrolną
  command[7] = (command[0] + command[1] + command[2] + command[3] +
                command[4] + command[5] + command[6]) & 0xFF;

  // Wyślij komendę do lidar
  tf02Serial.write(command, 8);

  // Read response
  uint8_t response[9];
  tf02Serial.readBytes(response, 9);
  
  Serial.write(response[0]);
  Serial.write(response[1]);
  Serial.write(response[2]);
  Serial.write(response[3]);
  Serial.write(response[4]);
  Serial.write(response[5]);
  Serial.write(response[6]);
  Serial.write(response[7]);
  Serial.write(response[8]);

}

//void activateHorn() {
 // digitalWrite(HORN_PIN, HIGH); // Activate horn
 // delay(1000); // Horn duration: 1 second
 // digitalWrite(HORN_PIN, LOW); // Deactivate horn
//}`
Editor is loading...
Leave a Comment