Advertisement
pleasedontcode

Sound Monitor rev_01

Oct 23rd, 2024
69
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 NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-10-23 11:26:31
  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. /****** DEFINITION OF LIBRARIES *****/
  27. #include <Wire.h>
  28. #include <LiquidCrystal_I2C.h>  //https://github.com/marcoschwartz/LiquidCrystal_I2C
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. /***** DEFINITION OF I2C PINS *****/
  35. const uint8_t display_LCD1602I2C_I2C_PIN_SDA_A4     = A4;
  36. const uint8_t display_LCD1602I2C_I2C_PIN_SCL_A5     = A5;
  37. const uint8_t display_LCD1602I2C_I2C_SLAVE_ADDRESS      = 39;
  38.  
  39. /****** DEFINITION OF PIN NUMBERS *****/
  40. // Define the pin numbers for the sound sensor, LED, and buzzer
  41. const uint8_t SOUND_SENSOR_PIN = A0; // Analog pin for sound sensor
  42. const uint8_t LED_PIN = 9;            // Digital pin for LED
  43. const uint8_t BUZZER_PIN = 10;        // Digital pin for Buzzer
  44.  
  45. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  46. // Initialize the LiquidCrystal_I2C object with the I2C address and dimensions of the LCD
  47. LiquidCrystal_I2C lcd(display_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2); // 16 columns and 2 rows
  48.  
  49. void setup(void)
  50. {
  51.     // Initialize the LCD
  52.     lcd.init(); // Initialize the LCD
  53.     lcd.backlight(); // Turn on the backlight
  54.  
  55.     // Initialize the LED and Buzzer pins
  56.     pinMode(LED_PIN, OUTPUT); // Set LED pin as output
  57.     pinMode(BUZZER_PIN, OUTPUT); // Set Buzzer pin as output
  58.  
  59.     // Display initial message on the LCD
  60.     lcd.setCursor(0, 0); // Set cursor to first row, first column
  61.     lcd.print("Sound Level:"); // Print message on the first row
  62. }
  63.  
  64. void loop(void)
  65. {
  66.     // Read the sound level from the sound sensor
  67.     int soundLevel = analogRead(SOUND_SENSOR_PIN); // Read the sound level
  68.  
  69.     // Display the sound level on the LCD
  70.     lcd.setCursor(0, 1); // Set cursor to second row, first column
  71.     lcd.print("        "); // Clear previous sound level
  72.     lcd.setCursor(0, 1); // Reset cursor to the beginning of the second row
  73.     lcd.print(soundLevel); // Print the sound level
  74.  
  75.     // Check if the sound level exceeds a certain limit (e.g., 300)
  76.     if (soundLevel > 300) {
  77.         digitalWrite(LED_PIN, HIGH); // Turn on the LED
  78.         digitalWrite(BUZZER_PIN, HIGH); // Turn on the Buzzer
  79.         delay(100); // Buzzer on for 100 milliseconds
  80.         digitalWrite(BUZZER_PIN, LOW); // Turn off the Buzzer
  81.     } else {
  82.         digitalWrite(LED_PIN, LOW); // Turn off the LED
  83.     }
  84.  
  85.     delay(500); // Delay for half a second before the next reading
  86. }
  87.  
  88. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement