#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address and dimensions
const int soundSensorPin = 0; // KY-037 analog interface for input
const int dangerLevelThreshold = -42; // Microphone sensitivity threshold (-42 dB)
const int soundRecorderPin = 9; // Digital pin connected to sound recorder module
const int greenLedPin = 6;
const int yellowLedPin = 7;
const int redLedPin = 5;
// Adjustable sensitivity parameters
float sensitivityFactor = 1.0; // Sensitivity adjustment factor (0.1 to 1.0)
unsigned long previousMillis = 0; // Variable to store the last time an action was performed
const long interval = 1000; // Interval in milliseconds (0.1 second)
void setup() {
Serial.begin(9600);
pinMode(soundRecorderPin, INPUT); // Set sound recorder pin as INPUT
calibrateSoundSensor(); // Calibrate the sound sensor
pinMode(greenLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
// Initialize serial communication at 9600 bits per second and for debugging
lcd.init(); // Initialize LCD
lcd.backlight(); // Turn on backlight
}
void calibrateSoundSensor() {
// Perform calibration by averaging readings over a short period
int total = 0;
for (int i = 0; i < 10; i++) { // Read 10 samples for calibration
total += analogRead(soundSensorPin);
delay(50); // Adjust delay as needed
}
int baseThreshold = total / 10; // Set baseThreshold to the average value
}
void updateSoundGraph(int decibels) {
// Calculate the number of bars based on the decibel range
int numBars = map(decibels, 0, 100, 0, 16); // Map decibels to LCD width (16 columns)
lcd.setCursor(0, 1);
for (int i = 0; i < numBars; i++) {
lcd.write(255); // Display a filled bar (custom character or ASCII value)
}
for (int i = numBars; i < 16; i++) {
lcd.print(" "); // Clear remaining space
}
}
void loop() {
unsigned long currentMillis = millis(); // Get the current time in milliseconds
if (currentMillis - previousMillis >= interval) { // Check if the interval has elapsed
previousMillis = currentMillis; // Update the previous time to the current time
int soundLevel = analogRead(soundSensorPin); // Read sound level from sensor
int decibels = map(soundLevel, 0, 1023, 0, 115); // Map analog value to decibels
// Adjust threshold based on microphone sensitivity
int threshold = dangerLevelThreshold + (int)(sensitivityFactor * decibels);
// Debugging output
Serial.print("Raw Analog Reading: ");
Serial.print(soundLevel);
Serial.print(" Mapped Decibels: ");
Serial.println(decibels);
delay(1000); // Delay for 1 second
lcd.setCursor(0, 0);
lcd.print("Decibels: ");
lcd.print(decibels);
// Update sound graph on LCD
updateSoundGraph(decibels);
// Display sound level status on LCD
if (decibels >= 0 && decibels <= 50){
lcd.setCursor(0, 1);
lcd.print("Ordinary Classroom ");
} if (decibels > 51 && decibels <= 65){
lcd.setCursor(0, 1);
lcd.print("Unruly Classroom ");
} if (decibels > 66 && decibels <= 100){
lcd.setCursor(0, 1);
lcd.print("Unacceptable Classroom Noise ");
// Trigger sound alert when every danger level is reached
triggerSoundAlert();
}
// Control LED lights based on sound levels...
if (decibels >= 0 && decibels <= 50){
digitalWrite(greenLedPin, HIGH); // Turn on the green LED
digitalWrite(yellowLedPin, LOW); // Turn off the yellow LED
digitalWrite(redLedPin, LOW); // Turn off the red LED
} if (decibels > 51 && decibels <= 65){
digitalWrite(greenLedPin, LOW); // Turn off the green LED
digitalWrite(yellowLedPin, HIGH); // Turn on the yellow LED
digitalWrite(redLedPin, LOW); // Turn off the red LED
} if(decibels > 66){
digitalWrite(greenLedPin, LOW); // Turn off the green LED
digitalWrite(yellowLedPin, LOW); // Turn off the yellow LED
digitalWrite(redLedPin, HIGH); // Turn on the red LED
}
}
}
void triggerSoundAlert() {
tone(soundRecorderPin, 20000, 20000); // Example tone (20000 Hz for 6 seconds)
delay(1000); // Wait for the tone to finish
noTone(soundRecorderPin); // Stop the tone
}