Creating Emergency Vehicle sounds using Arduino || Arduino UNO || Electronics Mini Projects
ฝัง
- เผยแพร่เมื่อ 16 มิ.ย. 2024
- In this video, I'll show you how to create realistic emergency vehicle siren sounds using PWM (Pulse Width Modulation) and an Arduino, without relying on any pre-recorded audio files! Follow along as I demonstrate how to produce the distinct sounds of police cars, ambulances, and fire trucks with just a few electronic components and some programming.
This tutorial is ideal for beginners interested in electronics and Arduino programming. It's a fun and educational project to learn sound generation and modulation through coding and simple circuit setup. Whether you’re a hobbyist or a student, this guide will help you add unique sound effects to your projects.
The Arduino code for this circuit is in the description :)
Chapters of this video :
0:00 - Placing Components
0:45 - Arduino Connections
1:00 - Working
1:26 - Outro - วิทยาศาสตร์และเทคโนโลยี
Here's the Code for Arduino -->
const int speakerPin = 9;
const int ambulanceButtonPin = 2;
const int policeButtonPin = 3;
const int fireButtonPin = 4;
const int ambulanceLowFrequency = 440;
const int ambulanceHighFrequency = 800;
const int policeLowFrequency = 500;
const int policeHighFrequency = 2000;
const int fireLowFrequency = 200;
const int fireHighFrequency = 3000;
const int frequencyChange = 20;
void setup() {
pinMode(speakerPin, OUTPUT);
pinMode(ambulanceButtonPin, INPUT_PULLUP);
pinMode(policeButtonPin, INPUT_PULLUP);
pinMode(fireButtonPin, INPUT_PULLUP);
}
void loop() {
if (digitalRead(ambulanceButtonPin) == LOW) {
playAmbulanceSiren();
}
if (digitalRead(policeButtonPin) == LOW) {
playPoliceSiren();
}
if (digitalRead(fireButtonPin) == LOW) {
playFireSiren();
}
}
void playAmbulanceSiren() {
for (int i = 0; i < 5; i++) {
tone(9, 440, 500);
delay(500);
tone(9, 800, 500);
delay(500);
}
noTone(speakerPin);
}
void playPoliceSiren() {
for (int i = 0; i < 10; i++) {
for (int frequency = policeLowFrequency; frequency = policeLowFrequency; frequency -= frequencyChange) {
tone(speakerPin, frequency);
delay(2);
}
}
noTone(speakerPin);
}
void playFireSiren() {
for (int i = 0; i < 5; i++) {
for (int frequency = fireLowFrequency; frequency = fireLowFrequency; frequency -= frequencyChange) {
tone(speakerPin, frequency);
delay(8);
}
}
noTone(speakerPin);
}