05 - Lab Sound Effects Studio System

Video

# Lab 21: Sound Effects Studio System ## **Objective** Create an interactive sound effects system using the TK36 active buzzer with multiple sound patterns, adjustable volume and speed controls, and serial command interface. Learn to generate various audio feedback patterns for different applications. ## **Required Components** ![](https://cdn.shopify.com/s/files/1/0331/9994/7908/files/Pasted_image_20250723133430.png?v=1753857539) 1. **Lonely Binary UNO R3** - Main Arduino board 2. **TinkerBlock UNO R3 Shield** - Expansion shield that plugs onto the UNO R3 3. **TinkerBlock TK36** - Active Buzzer Module ## **Theory** ### **Sound Effect Generation** - **Pattern Variation**: Different timing patterns create distinct sounds - **Volume Control**: Duration affects perceived loudness - **Speed Control**: Pause duration affects rhythm and tempo - **Frequency Control**: Active buzzer uses fixed frequency (typically 2.5kHz) ### **Sound Effect Types** - **Beep**: Simple single tone for basic feedback - **Chirp**: Multiple quick tones for attention - **Warning**: Longer tones for alerts - **Success**: Ascending pattern for positive feedback - **Error**: Two-tone pattern for negative feedback - **Notification**: Double beep for general alerts ### **Interactive Control** - **Serial Commands**: Remote control via serial interface - **Volume Adjustment**: Three levels (1-3) affecting duration - **Speed Adjustment**: Three levels (1-3) affecting pause timing - **Sound Toggle**: Enable/disable all sound effects ## **Wiring Instructions** ### **TK36 Active Buzzer Pinout** - **GND** → Arduino GND - **VCC** → Arduino 5V - **NC** → No Connection - **Signal** → Arduino Digital Pin D9 ### **Connection Diagram** ![](https://cdn.shopify.com/s/files/1/0331/9994/7908/files/Pasted_image_20250723133514.png?v=1753857544) ``` UNO R3 + Shield └── Digital Pin D9 ──→ TK36 Signal ``` ## **Interactive Sound Effects Code** ```cpp // Interactive Sound Effects with Active Buzzer // Pin definitions #define BUZZER_PIN 9 // Sound effect types #define SOUND_BEEP 1 #define SOUND_CHIRP 2 #define SOUND_WARNING 3 #define SOUND_SUCCESS 4 #define SOUND_ERROR 5 #define SOUND_NOTIFY 6 // Variables int currentVolume = 1; // Volume level (1-3) int currentSpeed = 1; // Speed level (1-3) bool soundEnabled = true; // Sound on/off void setup() { Serial.begin(9600); pinMode(BUZZER_PIN, OUTPUT); digitalWrite(BUZZER_PIN, LOW); delay(1000); Serial.println("Interactive Sound Effects System"); Serial.println("================================="); Serial.println("Commands:"); Serial.println("1-6: Play sound effects"); Serial.println("v: Adjust volume"); Serial.println("s: Adjust speed"); Serial.println("t: Toggle sound on/off"); Serial.println("a: Auto demo mode"); Serial.println("r: Reset settings"); Serial.println(); displaySettings(); } void loop() { // Check for serial commands if (Serial.available()) { handleSoundCommand(Serial.read()); } delay(100); } // Function to handle sound commands void handleSoundCommand(char command) { switch (command) { case '1': playSoundEffect(SOUND_BEEP); break; case '2': playSoundEffect(SOUND_CHIRP); break; case '3': playSoundEffect(SOUND_WARNING); break; case '4': playSoundEffect(SOUND_SUCCESS); break; case '5': playSoundEffect(SOUND_ERROR); break; case '6': playSoundEffect(SOUND_NOTIFY); break; case 'v': adjustVolume(); break; case 's': adjustSpeed(); break; case 't': toggleSound(); break; case 'a': autoDemo(); break; case 'r': resetSettings(); break; } } // Function to play sound effects void playSoundEffect(int effectType) { if (!soundEnabled) { Serial.println("Sound disabled"); return; } Serial.print("Playing sound effect: "); switch (effectType) { case SOUND_BEEP: Serial.println("Beep"); playBeep(); break; case SOUND_CHIRP: Serial.println("Chirp"); playChirp(); break; case SOUND_WARNING: Serial.println("Warning"); playWarning(); break; case SOUND_SUCCESS: Serial.println("Success"); playSuccess(); break; case SOUND_ERROR: Serial.println("Error"); playError(); break; case SOUND_NOTIFY: Serial.println("Notification"); playNotify(); break; } } // Sound effect functions void playBeep() { int duration = 100 * currentVolume; int pause = 50 / currentSpeed; digitalWrite(BUZZER_PIN, HIGH); delay(duration); digitalWrite(BUZZER_PIN, LOW); delay(pause); } void playChirp() { int duration = 50 * currentVolume; int pause = 30 / currentSpeed; for (int i = 0; i < 3; i++) { digitalWrite(BUZZER_PIN, HIGH); delay(duration); digitalWrite(BUZZER_PIN, LOW); delay(pause); } } void playWarning() { int duration = 200 * currentVolume; int pause = 100 / currentSpeed; for (int i = 0; i < 3; i++) { digitalWrite(BUZZER_PIN, HIGH); delay(duration); digitalWrite(BUZZER_PIN, LOW); delay(pause); } } void playSuccess() { int duration = 80 * currentVolume; int pause = 40 / currentSpeed; // Ascending pattern for (int i = 0; i < 4; i++) { digitalWrite(BUZZER_PIN, HIGH); delay(duration); digitalWrite(BUZZER_PIN, LOW); delay(pause); } } void playError() { int duration = 300 * currentVolume; int pause = 150 / currentSpeed; // Low tone digitalWrite(BUZZER_PIN, HIGH); delay(duration); digitalWrite(BUZZER_PIN, LOW); delay(pause); // High tone digitalWrite(BUZZER_PIN, HIGH); delay(duration); digitalWrite(BUZZER_PIN, LOW); delay(pause); } void playNotify() { int duration = 60 * currentVolume; int pause = 30 / currentSpeed; // Quick double beep digitalWrite(BUZZER_PIN, HIGH); delay(duration); digitalWrite(BUZZER_PIN, LOW); delay(pause); digitalWrite(BUZZER_PIN, HIGH); delay(duration); digitalWrite(BUZZER_PIN, LOW); delay(pause); } // Function to adjust volume void adjustVolume() { currentVolume = (currentVolume % 3) + 1; Serial.print("Volume set to: "); Serial.println(currentVolume); displaySettings(); } // Function to adjust speed void adjustSpeed() { currentSpeed = (currentSpeed % 3) + 1; Serial.print("Speed set to: "); Serial.println(currentSpeed); displaySettings(); } // Function to toggle sound void toggleSound() { soundEnabled = !soundEnabled; Serial.print("Sound: "); Serial.println(soundEnabled ? "ON" : "OFF"); displaySettings(); } // Function to run auto demo void autoDemo() { Serial.println("Auto Demo Mode - Playing all sound effects..."); for (int i = 1; i <= 6; i++) { playSoundEffect(i); delay(1000); } Serial.println("Demo complete!"); } // Function to reset settings void resetSettings() { currentVolume = 1; currentSpeed = 1; soundEnabled = true; Serial.println("Settings reset to defaults"); displaySettings(); } // Function to display current settings void displaySettings() { Serial.println("Current Settings:"); Serial.print(" Volume: "); Serial.println(currentVolume); Serial.print(" Speed: "); Serial.println(currentSpeed); Serial.print(" Sound: "); Serial.println(soundEnabled ? "ON" : "OFF"); Serial.println(); } ``` ## **Advanced Sound Effects System** ```cpp // Advanced Sound Effects with Custom Patterns // Pin definitions #define BUZZER_PIN 9 // Sound effect types #define SOUND_BEEP 1 #define SOUND_CHIRP 2 #define SOUND_WARNING 3 #define SOUND_SUCCESS 4 #define SOUND_ERROR 5 #define SOUND_NOTIFY 6 #define SOUND_ALARM 7 #define SOUND_MELODY 8 // Variables int currentVolume = 1; int currentSpeed = 1; bool soundEnabled = true; int customPattern[10]; // Custom pattern storage int patternLength = 0; void setup() { Serial.begin(9600); pinMode(BUZZER_PIN, OUTPUT); digitalWrite(BUZZER_PIN, LOW); delay(1000); Serial.println("Advanced Sound Effects System"); Serial.println("============================="); Serial.println("Commands:"); Serial.println("1-8: Play sound effects"); Serial.println("v: Adjust volume"); Serial.println("s: Adjust speed"); Serial.println("t: Toggle sound on/off"); Serial.println("a: Auto demo mode"); Serial.println("c: Create custom pattern"); Serial.println("p: Play custom pattern"); Serial.println("r: Reset settings"); Serial.println(); displaySettings(); } void loop() { if (Serial.available()) { handleAdvancedCommand(Serial.read()); } delay(100); } void handleAdvancedCommand(char command) { switch (command) { case '1': case '2': case '3': case '4': case '5': case '6': playSoundEffect(command - '0'); break; case '7': playAlarm(); break; case '8': playMelody(); break; case 'v': adjustVolume(); break; case 's': adjustSpeed(); break; case 't': toggleSound(); break; case 'a': autoDemo(); break; case 'c': createCustomPattern(); break; case 'p': playCustomPattern(); break; case 'r': resetSettings(); break; } } void playAlarm() { if (!soundEnabled) return; Serial.println("Playing: Alarm"); int duration = 150 * currentVolume; int pause = 75 / currentSpeed; for (int i = 0; i < 5; i++) { digitalWrite(BUZZER_PIN, HIGH); delay(duration); digitalWrite(BUZZER_PIN, LOW); delay(pause); } delay(200); for (int i = 0; i < 5; i++) { digitalWrite(BUZZER_PIN, HIGH); delay(duration); digitalWrite(BUZZER_PIN, LOW); delay(pause); } } void playMelody() { if (!soundEnabled) return; Serial.println("Playing: Melody"); int duration = 100 * currentVolume; int pause = 50 / currentSpeed; // Simple melody pattern for (int i = 0; i < 3; i++) { digitalWrite(BUZZER_PIN, HIGH); delay(duration); digitalWrite(BUZZER_PIN, LOW); delay(pause); } delay(200); for (int i = 0; i < 2; i++) { digitalWrite(BUZZER_PIN, HIGH); delay(duration * 2); digitalWrite(BUZZER_PIN, LOW); delay(pause); } } void createCustomPattern() { Serial.println("Create custom pattern (max 10 beeps)"); Serial.println("Enter durations in milliseconds (0 to finish):"); patternLength = 0; while (patternLength < 10) { while (!Serial.available()) { delay(100); } int duration = Serial.parseInt(); if (duration == 0) break; customPattern[patternLength] = duration; patternLength++; Serial.print("Added: "); Serial.print(duration); Serial.println("ms"); } Serial.print("Pattern created with "); Serial.print(patternLength); Serial.println(" beeps"); } void playCustomPattern() { if (!soundEnabled || patternLength == 0) { Serial.println("No custom pattern available"); return; } Serial.println("Playing custom pattern"); int pause = 50 / currentSpeed; for (int i = 0; i < patternLength; i++) { digitalWrite(BUZZER_PIN, HIGH); delay(customPattern[i] * currentVolume); digitalWrite(BUZZER_PIN, LOW); delay(pause); } } // Include all previous sound effect functions here... void playSoundEffect(int effectType) { if (!soundEnabled) { Serial.println("Sound disabled"); return; } Serial.print("Playing sound effect: "); switch (effectType) { case SOUND_BEEP: Serial.println("Beep"); playBeep(); break; case SOUND_CHIRP: Serial.println("Chirp"); playChirp(); break; case SOUND_WARNING: Serial.println("Warning"); playWarning(); break; case SOUND_SUCCESS: Serial.println("Success"); playSuccess(); break; case SOUND_ERROR: Serial.println("Error"); playError(); break; case SOUND_NOTIFY: Serial.println("Notification"); playNotify(); break; } } // Include all previous sound effect functions... void playBeep() { int duration = 100 * currentVolume; int pause = 50 / currentSpeed; digitalWrite(BUZZER_PIN, HIGH); delay(duration); digitalWrite(BUZZER_PIN, LOW); delay(pause); } void playChirp() { int duration = 50 * currentVolume; int pause = 30 / currentSpeed; for (int i = 0; i < 3; i++) { digitalWrite(BUZZER_PIN, HIGH); delay(duration); digitalWrite(BUZZER_PIN, LOW); delay(pause); } } void playWarning() { int duration = 200 * currentVolume; int pause = 100 / currentSpeed; for (int i = 0; i < 3; i++) { digitalWrite(BUZZER_PIN, HIGH); delay(duration); digitalWrite(BUZZER_PIN, LOW); delay(pause); } } void playSuccess() { int duration = 80 * currentVolume; int pause = 40 / currentSpeed; for (int i = 0; i < 4; i++) { digitalWrite(BUZZER_PIN, HIGH); delay(duration); digitalWrite(BUZZER_PIN, LOW); delay(pause); } } void playError() { int duration = 300 * currentVolume; int pause = 150 / currentSpeed; digitalWrite(BUZZER_PIN, HIGH); delay(duration); digitalWrite(BUZZER_PIN, LOW); delay(pause); digitalWrite(BUZZER_PIN, HIGH); delay(duration); digitalWrite(BUZZER_PIN, LOW); delay(pause); } void playNotify() { int duration = 60 * currentVolume; int pause = 30 / currentSpeed; digitalWrite(BUZZER_PIN, HIGH); delay(duration); digitalWrite(BUZZER_PIN, LOW); delay(pause); digitalWrite(BUZZER_PIN, HIGH); delay(duration); digitalWrite(BUZZER_PIN, LOW); delay(pause); } void adjustVolume() { currentVolume = (currentVolume % 3) + 1; Serial.print("Volume set to: "); Serial.println(currentVolume); displaySettings(); } void adjustSpeed() { currentSpeed = (currentSpeed % 3) + 1; Serial.print("Speed set to: "); Serial.println(currentSpeed); displaySettings(); } void toggleSound() { soundEnabled = !soundEnabled; Serial.print("Sound: "); Serial.println(soundEnabled ? "ON" : "OFF"); displaySettings(); } void autoDemo() { Serial.println("Auto Demo Mode - Playing all sound effects..."); for (int i = 1; i <= 8; i++) { if (i == 7) playAlarm(); else if (i == 8) playMelody(); else playSoundEffect(i); delay(1000); } Serial.println("Demo complete!"); } void resetSettings() { currentVolume = 1; currentSpeed = 1; soundEnabled = true; patternLength = 0; Serial.println("Settings reset to defaults"); displaySettings(); } void displaySettings() { Serial.println("Current Settings:"); Serial.print(" Volume: "); Serial.println(currentVolume); Serial.print(" Speed: "); Serial.println(currentSpeed); Serial.print(" Sound: "); Serial.println(soundEnabled ? "ON" : "OFF"); Serial.print(" Custom Pattern: "); Serial.print(patternLength); Serial.println(" beeps"); Serial.println(); } ``` ## **Code Explanation** ### **Basic System Features** - **Serial Command Interface**: Remote control via serial monitor - **Six Sound Effects**: Beep, chirp, warning, success, error, notification - **Volume Control**: Three levels affecting sound duration - **Speed Control**: Three levels affecting pause timing - **Sound Toggle**: Enable/disable all sound effects ### **Advanced System Features** - **Custom Pattern Creation**: User-defined sound patterns - **Additional Effects**: Alarm and melody patterns - **Pattern Storage**: Save and replay custom sequences - **Enhanced Demo**: All sound effects demonstration ### **Sound Effect Patterns** #### **Beep Pattern** - **Single tone**: One continuous sound - **Volume scaling**: Duration multiplied by volume level - **Speed scaling**: Pause divided by speed level #### **Chirp Pattern** - **Triple beep**: Three quick consecutive sounds - **Short duration**: 50ms base duration - **Quick rhythm**: 30ms base pause #### **Warning Pattern** - **Triple long beep**: Three longer sounds - **Alert duration**: 200ms base duration - **Attention rhythm**: 100ms base pause #### **Success Pattern** - **Ascending sequence**: Four consecutive beeps - **Positive feedback**: 80ms base duration - **Celebration rhythm**: 40ms base pause #### **Error Pattern** - **Two-tone sequence**: Low-high tone pattern - **Error duration**: 300ms base duration - **Warning rhythm**: 150ms base pause #### **Notification Pattern** - **Double beep**: Two quick consecutive sounds - **Alert duration**: 60ms base duration - **Quick rhythm**: 30ms base pause ### **Control Functions** #### **Volume Adjustment** ```cpp void adjustVolume() { currentVolume = (currentVolume % 3) + 1; } ``` - **Cycles through**: 1 → 2 → 3 → 1 - **Affects duration**: Longer sounds at higher volumes - **Perceived loudness**: Duration creates volume effect #### **Speed Adjustment** ```cpp void adjustSpeed() { currentSpeed = (currentSpeed % 3) + 1; } ``` - **Cycles through**: 1 → 2 → 3 → 1 - **Affects pauses**: Shorter pauses at higher speeds - **Rhythm control**: Creates tempo variation #### **Sound Toggle** ```cpp void toggleSound() { soundEnabled = !soundEnabled; } ``` - **Boolean flip**: ON ↔ OFF - **Global control**: Affects all sound effects - **Silent mode**: Useful for testing ### **Advanced Features** #### **Custom Pattern Creation** - **User input**: Serial-based pattern definition - **Duration storage**: Array of millisecond values - **Flexible length**: Up to 10 beeps maximum - **Volume scaling**: Custom durations affected by volume #### **Alarm Pattern** - **Double sequence**: Two groups of 5 beeps - **Alert rhythm**: Longer duration for attention - **Emergency style**: Distinctive warning sound #### **Melody Pattern** - **Musical sequence**: Varying note lengths - **Rhythm variation**: Different timing patterns - **Melodic structure**: Simple musical phrase ## **Expected Output** ### **Basic System:** ``` Interactive Sound Effects System ================================= Commands: 1-6: Play sound effects v: Adjust volume s: Adjust speed t: Toggle sound on/off a: Auto demo mode r: Reset settings Current Settings: Volume: 1 Speed: 1 Sound: ON 1 Playing sound effect: Beep v Volume set to: 2 Current Settings: Volume: 2 Speed: 1 Sound: ON ``` ### **Advanced System:** ``` Advanced Sound Effects System ============================= Commands: 1-8: Play sound effects v: Adjust volume s: Adjust speed t: Toggle sound on/off a: Auto demo mode c: Create custom pattern p: Play custom pattern r: Reset settings c Create custom pattern (max 10 beeps) Enter durations in milliseconds (0 to finish): 100 Added: 100ms 200 Added: 200ms 0 Pattern created with 2 beeps p Playing custom pattern ``` ## **Troubleshooting** ### **Buzzer not working:** - Check TK36 wiring (D9 pin) - Verify power supply (5V required) - Test buzzer with simple digitalWrite - Check for proper grounding ### **Serial commands not responding:** - Verify serial monitor is open - Check baud rate (9600) - Ensure commands are sent without extra characters - Test with simple serial communication ### **Sound effects not playing:** - Check soundEnabled variable - Verify volume and speed settings - Test individual sound functions - Check for timing conflicts ### **Custom patterns not working:** - Verify patternLength > 0 - Check customPattern array values - Ensure proper serial input format - Test pattern creation process ## **Applications** ### **User Interface Feedback** - **Button Press**: Confirmation beeps - **Menu Navigation**: Different sounds for different actions - **Form Validation**: Success/error feedback - **System Status**: Notification sounds ### **Game Development** - **Sound Effects**: Different actions, events - **Scoring Feedback**: Success/error sounds - **Level Completion**: Celebration sounds - **Game Over**: Warning/error sounds ### **Alarm Systems** - **Security Alerts**: Warning patterns - **Timer Notifications**: Notification sounds - **Error Conditions**: Error patterns - **System Status**: Status announcements ### **Educational Projects** - **Learning Feedback**: Success/error sounds - **Interactive Lessons**: Sound-based responses - **Quiz Systems**: Correct/incorrect feedback - **Progress Tracking**: Achievement sounds ## **Customization Ideas** ### **Add More Sound Effects:** - **Musical Notes**: Different frequencies - **Animal Sounds**: Bird chirps, cat meows - **Machine Sounds**: Engine, motor, fan - **Nature Sounds**: Rain, wind, thunder ### **Add Sensors:** - **Light Sensor**: Automatic volume based on ambient light - **Sound Sensor**: Sound-reactive patterns - **Motion Sensor**: Motion-activated sounds - **Temperature Sensor**: Temperature-based patterns ### **Advanced Features:** - **Frequency Control**: Variable tone generation - **Pattern Looping**: Continuous pattern playback - **Random Generation**: Random sound patterns - **Synchronization**: Multiple buzzer coordination ## **Next Steps** - Experiment with different timing patterns - Create custom sound effect libraries - Integrate with sensors for automatic triggers - Build complete audio feedback systems ## **Resources** - **Active Buzzer Technology**: Piezoelectric sound generation - **Audio Feedback Design**: User interface sound principles - **Timing Control**: Precise delay and timing techniques - **Serial Communication**: Command interface design