#include <Ultrasonic.h>
#include <Talkie.h>
const int trigPins[4] = {35, 33, 26, 19}; // Array of trigger pins
const int echoPins[4] = {34, 32, 25, 18}; // Array of echo pins
const int buzzerPin = 22; // Buzzer pin
const int ledPin = 23; // LED pin
Ultrasonic sensors[4] = { // Array of ultrasonic sensor objects
Ultrasonic(trigPins[0], echoPins[0]), // Create objects with trigger and echo pins
Ultrasonic(trigPins[1], echoPins[1]),
Ultrasonic(trigPins[2], echoPins[2]),
Ultrasonic(trigPins[3], echoPins[3])
};
// Define constant for round trip time in centimeters
const float US_ROUNDTRIP_CM = 57.0;
Talkie voice; // Talkie object for speech synthesis
void setup() {
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Initialize serial communication
}
void loop() {
for (int i = 0; i < 4; i++) {
long duration = sensors[i].read(); // Get sensor reading
int distance = duration / US_ROUNDTRIP_CM; // Convert duration to distance in cm
if (distance <= 100) { // Object detected within range
sayDistance(distance);
digitalWrite(ledPin, HIGH); // Turn on LED
toneBuzzer(); // Play tone on buzzer
delay(500); // Wait for 500 milliseconds
digitalWrite(ledPin, LOW); // Turn off LED
if (distance <= 10) { // Object very close
playMelody();
}
}
}
}
void sayDistance(int distance) {
char buffer[50];
sprintf(buffer, "Object is detected at %d centimeters", distance);
const uint8_t* convertedBuffer = reinterpret_cast<const uint8_t*>(buffer);
voice.say(convertedBuffer); // Convert text to speech
Serial.println(buffer); // Print message to serial monitor
}
void playMelody() {
// Define your melody and note durations here
}
void toneBuzzer() {
// Play a tone on the buzzer pin using PWM
// Adjust the duty cycle to change the volume
for (int i = 0; i < 2000; i++) {
digitalWrite(buzzerPin, HIGH);
delayMicroseconds(500); // Adjust the delay for the desired frequency
digitalWrite(buzzerPin, LOW);
delayMicroseconds(500); // Adjust the delay for the desired frequency
}
}