/*
* Buzzer Sound Generation - IMPROVED VERSION
* Better button response and clear mode indicators
* Press button ANYTIME to change modes!
*/
// Pin Definitions
const int buzzerPin = 8;
const int buttonPin = 2;
const int ledPin = 13;
// Variables
volatile int soundMode = 0; // Use volatile for interrupt
int lastMode = -1; // Track mode changes
bool modeChanged = true; // Flag for mode change
// Musical note frequencies
#define NOTE_C4 262
#define NOTE_D4 294
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_G4 392
#define NOTE_A4 440
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_D5 587
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_G5 784
#define NOTE_A5 880
#define NOTE_B5 988
#define REST 0
void setup() {
pinMode(buzzerPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
// Attach interrupt for instant button response
attachInterrupt(digitalPinToInterrupt(buttonPin), buttonPressed, FALLING);
Serial.begin(9600);
Serial.println("\n========================================");
Serial.println(" BUZZER SOUND SYSTEM - IMPROVED");
Serial.println("========================================");
Serial.println("Press button ANYTIME to change modes!");
Serial.println("Watch the LED blink pattern!");
Serial.println("========================================");
Serial.println("Mode 0: Simple Beeps (1 blink)");
Serial.println("Mode 1: Alarm (2 blinks)");
Serial.println("Mode 2: Police Siren (3 blinks)");
Serial.println("Mode 3: Ambulance (4 blinks)");
Serial.println("Mode 4: Door Bell (5 blinks)");
Serial.println("Mode 5: Happy Birthday (6 blinks)");
Serial.println("Mode 6: Twinkle Star (7 blinks)");
Serial.println("Mode 7: Mario (8 blinks)");
Serial.println("Mode 8: Star Wars (9 blinks)");
Serial.println("Mode 9: Nokia (10 blinks)");
Serial.println("========================================\n");
playStartupSound();
}
void loop() {
// Check if mode changed
if (modeChanged) {
noTone(buzzerPin); // Stop current sound
showMode(); // Show mode with LED blinks
modeChanged = false;
lastMode = soundMode;
}
// Play current mode
switch(soundMode) {
case 0: simpleBeeps(); break;
case 1: alarmSound(); break;
case 2: policeSiren(); break;
case 3: ambulanceSiren(); break;
case 4: doorBell(); break;
case 5: playHappyBirthday(); break;
case 6: playTwinkleTwinkle(); break;
case 7: playMarioTheme(); break;
case 8: playStarWars(); break;
case 9: playNokiaRingtone(); break;
}
delay(100); // Small delay
}
// Interrupt service routine
void buttonPressed() {
static unsigned long lastInterruptTime = 0;
unsigned long interruptTime = millis();
// Debounce - ignore if pressed within 200ms
if (interruptTime - lastInterruptTime > 200) {
soundMode++;
if (soundMode > 9) soundMode = 0;
modeChanged = true;
}
lastInterruptTime = interruptTime;
}
// Show current mode with LED blinks
void showMode() {
Serial.print("\n>>> MODE ");
Serial.print(soundMode);
Serial.print(": ");
switch(soundMode) {
case 0: Serial.println("Simple Beeps"); break;
case 1: Serial.println("Alarm Sound"); break;
case 2: Serial.println("Police Siren"); break;
case 3: Serial.println("Ambulance Siren"); break;
case 4: Serial.println("Door Bell"); break;
case 5: Serial.println("Happy Birthday"); break;
case 6: Serial.println("Twinkle Twinkle"); break;
case 7: Serial.println("Mario Theme"); break;
case 8: Serial.println("Star Wars"); break;
case 9: Serial.println("Nokia Ringtone"); break;
}
// Blink LED (mode + 1) times
delay(500);
for(int i = 0; i <= soundMode; i++) {
digitalWrite(ledPin, HIGH);
tone(buzzerPin, 1000, 50); // Short beep
delay(150);
digitalWrite(ledPin, LOW);
delay(150);
}
delay(500);
}
// ============================================
// MODE 0: Simple Beeps
// ============================================
void simpleBeeps() {
digitalWrite(ledPin, HIGH);
tone(buzzerPin, 1000, 200);
delay(200);
digitalWrite(ledPin, LOW);
delay(800);
}
// ============================================
// MODE 1: Alarm Sound
// ============================================
void alarmSound() {
for(int i = 0; i < 2; i++) {
digitalWrite(ledPin, HIGH);
tone(buzzerPin, 1000, 300);
delay(300);
digitalWrite(ledPin, LOW);
tone(buzzerPin, 500, 300);
delay(300);
}
delay(400);
}
// ============================================
// MODE 2: Police Siren
// ============================================
void policeSiren() {
// Faster siren
for(int freq = 500; freq < 1500; freq += 100) {
tone(buzzerPin, freq, 20);
digitalWrite(ledPin, HIGH);
delay(20);
}
for(int freq = 1500; freq > 500; freq -= 100) {
tone(buzzerPin, freq, 20);
digitalWrite(ledPin, LOW);
delay(20);
}
delay(200);
}
// ============================================
// MODE 3: Ambulance Siren
// ============================================
void ambulanceSiren() {
for(int i = 0; i < 3; i++) {
digitalWrite(ledPin, HIGH);
tone(buzzerPin, 600, 150);
delay(150);
digitalWrite(ledPin, LOW);
tone(buzzerPin, 800, 150);
delay(150);
}
delay(300);
}
// ============================================
// MODE 4: Door Bell
// ============================================
void doorBell() {
digitalWrite(ledPin, HIGH);
tone(buzzerPin, 1000, 200);
delay(200);
tone(buzzerPin, 800, 400);
delay(400);
digitalWrite(ledPin, LOW);
delay(1000);
}
// ============================================
// MODE 5: Happy Birthday (SHORT VERSION)
// ============================================
void playHappyBirthday() {
int melody[] = {
NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_F4, NOTE_E4,
NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_G4, NOTE_F4
};
int durations[] = {8, 8, 4, 4, 4, 2, 8, 8, 4, 4, 4, 2};
for (int i = 0; i < 12; i++) {
int duration = 1000 / durations[i];
digitalWrite(ledPin, HIGH);
tone(buzzerPin, melody[i], duration);
delay(duration * 1.2);
digitalWrite(ledPin, LOW);
noTone(buzzerPin);
delay(50);
}
delay(1000);
}
// ============================================
// MODE 6: Twinkle Twinkle (SHORT VERSION)
// ============================================
void playTwinkleTwinkle() {
int melody[] = {
NOTE_C4, NOTE_C4, NOTE_G4, NOTE_G4, NOTE_A4, NOTE_A4, NOTE_G4,
NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4, NOTE_C4
};
int durations[] = {4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 2};
for (int i = 0; i < 14; i++) {
int duration = 1000 / durations[i];
digitalWrite(ledPin, HIGH);
tone(buzzerPin, melody[i], duration);
delay(duration * 1.2);
digitalWrite(ledPin, LOW);
noTone(buzzerPin);
delay(50);
}
delay(1000);
}
// ============================================
// MODE 7: Mario Theme
// ============================================
void playMarioTheme() {
int melody[] = {
NOTE_E5, NOTE_E5, REST, NOTE_E5, REST, NOTE_C5, NOTE_E5, NOTE_G5
};
int durations[] = {8, 8, 8, 8, 8, 8, 8, 4};
for (int i = 0; i < 8; i++) {
int duration = 1000 / durations[i];
if (melody[i] == REST) {
delay(duration);
} else {
digitalWrite(ledPin, HIGH);
tone(buzzerPin, melody[i], duration);
delay(duration * 1.2);
digitalWrite(ledPin, LOW);
noTone(buzzerPin);
}
delay(50);
}
delay(1000);
}
// ============================================
// MODE 8: Star Wars
// ============================================
void playStarWars() {
int melody[] = {
NOTE_A4, NOTE_A4, NOTE_A4, NOTE_F4, NOTE_C5,
NOTE_A4, NOTE_F4, NOTE_C5, NOTE_A4
};
int durations[] = {8, 8, 8, 6, 16, 8, 6, 16, 4};
for (int i = 0; i < 9; i++) {
int duration = 1000 / durations[i];
digitalWrite(ledPin, HIGH);
tone(buzzerPin, melody[i], duration);
delay(duration * 1.2);
digitalWrite(ledPin, LOW);
noTone(buzzerPin);
delay(50);
}
delay(1000);
}
// ============================================
// MODE 9: Nokia Ringtone
// ============================================
void playNokiaRingtone() {
int melody[] = {
NOTE_E5, NOTE_D5, NOTE_F4, NOTE_G4,
NOTE_C5, NOTE_B4, NOTE_D4, NOTE_E4
};
int durations[] = {8, 8, 4, 4, 8, 8, 4, 4};
for (int i = 0; i < 8; i++) {
int duration = 1000 / durations[i];
digitalWrite(ledPin, HIGH);
tone(buzzerPin, melody[i], duration);
delay(duration * 1.2);
digitalWrite(ledPin, LOW);
noTone(buzzerPin);
delay(50);
}
delay(1000);
}
// ============================================
// Startup Sound
// ============================================
void playStartupSound() {
Serial.println("Starting up...");
for(int i = 200; i < 1000; i += 100) {
tone(buzzerPin, i, 50);
digitalWrite(ledPin, HIGH);
delay(50);
digitalWrite(ledPin, LOW);
delay(50);
}
Serial.println("Ready!\n");
}
Comments
Post a Comment