#include <Wire.h>
#include <LedControl.h>
// Define pins for buzzer, ultrasonic sensor, LEDs, and emergency button
const int trigPin = 9; // Arduino pin for ultrasonic sensor trigger
const int echoPin = 10; // Arduino pin for ultrasonic sensor echo
const int buzzerPin = 2; // Arduino pin for buzzer
const int redLedPin = 4; // Arduino pin for red LED
const int yellowLedPin = 5; // Arduino pin for yellow LED
const int greenLedPin = 6; // Arduino pin for green LED
const int emergencyButtonPin = 2; // Arduino pin for emergency button
// MAX7219 matrix configuration
const int dataIn = 11; // DIN pin of MAX7219 connected to Arduino digital pin 11
const int load = 12; // CS pin of MAX7219 connected to Arduino digital pin 12
const int clock = 13; // CLK pin of MAX7219 connected to Arduino digital pin 13
const int devicesInUse = 1; // Number of MAX7219 devices in the chain
LedControl lc = LedControl(dataIn, clock, load, devicesInUse); // Initialize the MAX7219 library
void setup() {
Serial.begin(9600); // Initialize serial communication for distance display
pinMode(trigPin, OUTPUT); // Trigger pin of ultrasonic sensor as output
pinMode(echoPin, INPUT); // Echo pin of ultrasonic sensor as input
pinMode(buzzerPin, OUTPUT); // Buzzer pin as output
pinMode(redLedPin, OUTPUT); // Red LED pin as output
pinMode(yellowLedPin, OUTPUT); // Yellow LED pin as output
pinMode(greenLedPin, OUTPUT); // Green LED pin as output
pinMode(emergencyButtonPin, INPUT_PULLUP); // Emergency button pin as input
// Initialize the MAX7219 matrix display
lc.shutdown(0, false); // Wake up the MAX7219 (turn off shutdown)
lc.setIntensity(0, 8); // Set brightness level (0 is min, 15 is max)
lc.clearDisplay(0); // Clear the display register
// Display initial message or setup
lc.setDigit(0, 7, 'H', false); // Example: Display 'H' at position 7 (character 0, digit 7)
lc.setDigit(0, 6, 'E', false); // Example: Display 'E' at position 6 (character 0, digit 6)
lc.setDigit(0, 5, 'L', false); // Example: Display 'L' at position 5 (character 0, digit 5)
lc.setDigit(0, 4, 'P', false); // Example: Display 'P' at position 4 (character 0, digit 4)
delay(1000); // Delay to display the message for a moment
lc.clearDisplay(0); // Clear the display after initial message
}
void loop() {
// Check if the emergency button is pressed
if (digitalRead(emergencyButtonPin) == LOW) {
triggerEmergencyAlert();
delay(1000); // Debounce delay
}
// Generate random distance between 10 and 1000 cm
int distance = random(10, 1000);
// Print distance to Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Display distance on MAX7219 matrix
displayDistanceOnMatrix(distance);
// Determine feedback based on distance
if (distance < 200) {
// Close obstacle: Red LED and high frequency buzzer sound
digitalWrite(redLedPin, HIGH);
digitalWrite(yellowLedPin, LOW);
digitalWrite(greenLedPin, LOW);
tone(buzzerPin, 384); // High frequency
} else if (distance < 500) {
// Medium distance: Yellow LED and medium frequency buzzer sound
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, HIGH);
digitalWrite(greenLedPin, LOW);
tone(buzzerPin, 341); // Medium frequency
} else {
// Clear path: Green LED and low frequency buzzer sound
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
digitalWrite(greenLedPin, HIGH);
tone(buzzerPin, 256); // Low frequency
}
// Delay to control the frequency of readings
delay(1000); // Adjust delay according to your preference
// Stop the buzzer tone after the delay
noTone(buzzerPin);
// Turn off all LEDs at the end of each loop iteration
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
digitalWrite(greenLedPin, LOW);
}
void triggerEmergencyAlert() {
// Clear MAX7219 display
lc.clearDisplay(0);
// Display emergency message on MAX7219 matrix
lc.setRow(0, 0, B11111111); // Example: Display a row of LEDs to represent an emergency alert
}
void displayDistanceOnMatrix(int distance) {
// Example function to display distance on MAX7219 matrix
// Convert distance to a string
String distanceStr = String(distance);
// Display distance string on MAX7219 matrix (up to 4 digits)
int len = distanceStr.length();
for (int i = 0; i < len; i++) {
lc.setDigit(0, i, distanceStr.charAt(i), false); // Display each character of distance
}
// Clear remaining digits if distance is less than 4 digits
for (int i = len; i < 4; i++) {
lc.setDigit(0, i, ' ', false); // Clear the digit
}
}