Advertisement
pleasedontcode

Sound Monitor rev_02

Oct 23rd, 2024
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Sound Monitor
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-10-23 11:43:28
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* I want the lcd to display the sound level when the */
  21.     /* sound sensor detects a sound and the led should */
  22.     /* glow if it crosses a certain limit and the buzzer */
  23.     /* should beep */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Wire.h>
  29. #include <LiquidCrystal_I2C.h>  //https://github.com/marcoschwartz/LiquidCrystal_I2C
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34.  
  35. /***** DEFINITION OF I2C PINS *****/
  36. const uint8_t display_LCD1602I2C_I2C_PIN_SDA_A4     = A4;
  37. const uint8_t display_LCD1602I2C_I2C_PIN_SCL_A5     = A5;
  38. const uint8_t display_LCD1602I2C_I2C_SLAVE_ADDRESS      = 39;
  39.  
  40. /****** DEFINITION OF PIN NUMBERS *****/
  41. // Define the pin numbers for the sound sensor, LED, and buzzer
  42. const uint8_t SOUND_SENSOR_PIN = A0; // Analog pin for sound sensor
  43. const uint8_t LED_PIN = 9;            // Digital pin for LED
  44. const uint8_t BUZZER_PIN = 10;        // Digital pin for Buzzer
  45.  
  46. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  47. // Initialize the LiquidCrystal_I2C object with the I2C address and dimensions of the LCD
  48. LiquidCrystal_I2C lcd(display_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2); // 16 columns and 2 rows
  49.  
  50. void setup(void)
  51. {
  52.     // Initialize the LCD
  53.     lcd.init(); // Initialize the LCD
  54.     lcd.backlight(); // Turn on the backlight
  55.  
  56.     // Initialize the LED and Buzzer pins
  57.     pinMode(LED_PIN, OUTPUT); // Set LED pin as output
  58.     pinMode(BUZZER_PIN, OUTPUT); // Set Buzzer pin as output
  59.  
  60.     // Display initial message on the LCD
  61.     lcd.setCursor(0, 0); // Set cursor to first row, first column
  62.     lcd.print("Sound Level:"); // Print message on the first row
  63. }
  64.  
  65. void loop(void)
  66. {
  67.     // Read the sound level from the sound sensor
  68.     int soundLevel = analogRead(SOUND_SENSOR_PIN); // Read the sound level
  69.  
  70.     // Display the sound level on the LCD
  71.     lcd.setCursor(0, 1); // Set cursor to second row, first column
  72.     lcd.print("        "); // Clear previous sound level
  73.     lcd.setCursor(0, 1); // Reset cursor to the beginning of the second row
  74.     lcd.print(soundLevel); // Print the sound level
  75.  
  76.     // Check if the sound level exceeds a certain limit (e.g., 300)
  77.     if (soundLevel > 300) {
  78.         digitalWrite(LED_PIN, HIGH); // Turn on the LED
  79.         digitalWrite(BUZZER_PIN, HIGH); // Turn on the Buzzer
  80.         delay(100); // Buzzer on for 100 milliseconds
  81.         digitalWrite(BUZZER_PIN, LOW); // Turn off the Buzzer
  82.     } else {
  83.         digitalWrite(LED_PIN, LOW); // Turn off the LED
  84.     }
  85.  
  86.     delay(500); // Delay for half a second before the next reading
  87. }
  88.  
  89. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement