#include <SoftwareSerial.h>
#define NUM_SENSORS 6
#define THRESHOLD_DISTANCE 30
// Ultrasonic sensor pin assignments
const int trigPins[NUM_SENSORS] = {9, 11, 13, A0, A2, A4};
const int echoPins[NUM_SENSORS] = {10, 12, 4, A1, A3, A5};
// Buzzer and LED pins
#define BUZZER_PIN 5
#define LED_PIN 6
// Setup SoftwareSerial for the DWIN display (RX=2, TX=3)
SoftwareSerial HMI(2, 3);
// Battery percentage variables
uint8_t batteryPercentage = 100;
unsigned long batteryLastUpdateTime = 0;
const unsigned long batteryInterval = 420000; // 7 minutes in milliseconds
// UART Transmission function
void uart_tx(uint16_t address, uint16_t data) {
uint8_t uart_frame[8] = {0x5A, 0xA5, 0x05, 0x82, 0, 0, 0, 0};
uart_frame[4] = (address >> 8) & 0xFF;
uart_frame[5] = address & 0xFF;
uart_frame[6] = (data >> 8) & 0xFF;
uart_frame[7] = data & 0xFF;
HMI.write(uart_frame, 8);
// Toggle LED to indicate data transmission
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
// Debugging
Serial.print("Sent: Addr = 0x");
Serial.print(address, HEX);
Serial.print(", Data = 0x");
Serial.println(data, HEX);
}
// Function to measure distance from ultrasonic sensors
long getDistance(int sensorIndex) {
digitalWrite(trigPins[sensorIndex], LOW);
delayMicroseconds(2);
digitalWrite(trigPins[sensorIndex], HIGH);
delayMicroseconds(10);
digitalWrite(trigPins[sensorIndex], LOW);
long duration = pulseIn(echoPins[sensorIndex], HIGH, 30000);
if (duration == 0) return -1; // No valid signal
long distance = duration * 0.034 / 2;
return distance;
}
void setup() {
Serial.begin(9600);
HMI.begin(115200); // Lower baud rate for stability
for (int i = 0; i < NUM_SENSORS; i++) {
pinMode(trigPins[i], OUTPUT);
pinMode(echoPins[i], INPUT);
}
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(2, INPUT); // RX for SoftwareSerial
pinMode(3, OUTPUT); // TX for SoftwareSerial
Serial.println("System Initialized");
}
void loop() {
bool anyTriggered = false;
// Sensor addresses for transmission
uint16_t sensorAddresses[NUM_SENSORS] = {0x2000, 0x3000, 0x4000, 0x5000, 0x6000, 0x7000};
// Process each sensor
for (int i = 0; i < NUM_SENSORS; i++) {
long distance = getDistance(i);
Serial.print("Sensor ");
Serial.print(i + 1);
Serial.print(": ");
if (distance == -1) {
Serial.println("No Reading");
continue;
} else {
Serial.print(distance);
Serial.println(" cm");
}
uint16_t sensorData = (distance > 0 && distance < THRESHOLD_DISTANCE) ? 1 : 0;
if (sensorData == 1) anyTriggered = true;
uart_tx(sensorAddresses[i], sensorData);
delay(100);
}
// Activate buzzer if any sensor is triggered
digitalWrite(BUZZER_PIN, anyTriggered ? HIGH : LOW);
// Update battery percentage every 7 minutes
unsigned long currentMillis = millis();
if (currentMillis - batteryLastUpdateTime >= batteryInterval) {
batteryLastUpdateTime = currentMillis;
if (batteryPercentage > 0) {
batteryPercentage--;
} else {
batteryPercentage = 100; // Reset after reaching 0
}
}
// Transmit battery data
uart_tx(0x1000, batteryPercentage);
Serial.print("Battery: ");
Serial.print(batteryPercentage);
Serial.println("%");
delay(500);
}
serial monitor output: Sent: Addr = 0x1000, Data = 0x64
Battery: 100%
Sensor 1: 256 cm
Sent: Addr = 0x2000, Data = 0x0
Sensor 2: No Reading
Sensor 3: No Reading
Sensor 4: No Reading
Sensor 5: No Reading
Sensor 6: No Reading