const int sampleWindow = 100; // This might change in the near future
unsigned int sample;
#define SENSOR_PIN A0
const int red_ledBulb = 9; // Analog connection of our LED
const int yellow_ledBulb = 10; // Analog connection of our LED
const int green_ledBulb = 11; // Analog connection of our LED
const int buzzer = 8; // Analog connection of our buzzer
const int threshold = 75; //Variable (CAN BE CHANGED)
/* DOCUMENTATION
*Changes to source code 11/2/2023
- Red LED and Buzzer continuously ACTIVE
(can be interrupted when NOISE LEVEL reached SAFE ZONE such as LESS THAN 60 to 70)
(YELLOW - GREATER THAN 61 and LESS THAN 70 ONLY, GREEN - LESS THAN 60 ONLY)
- Added Serial Output for the peakToPeak Analog value var (Peak Analog)
- Added pinMode Functions:
pinMode(red_ledBulb, OUTPUT);
pinMode(yellow_ledBulb, OUTPUT);
pinMode(green_ledBulb, OUTPUT);
pinMode(buzzer, OUTPUT);
(To add real world compatibility with LED and BUZZER)
- Added a DEBUF feature in Serial.print()
(will now print a 4 digit and 1 digit decimal regardless of the value
e.g (0342.0, 1023.0 etc))
(Changed Serial.print() output to one line for proper comparison
between Analog output and decibels.)
*/
void setup ()
{
pinMode (SENSOR_PIN, INPUT); // Set the signal pin as input
Serial.begin(9600);
pinMode(red_ledBulb, OUTPUT);
pinMode(yellow_ledBulb, OUTPUT);
pinMode(green_ledBulb, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop ()
{
unsigned long startMillis= millis(); // Start of sample window
float peakToPeak = 0; // peak-to-peak level
unsigned int signalMax = 0; //minimum value
unsigned int signalMin = 1024; //maximum value
// collect data for 100 mS (Changed based on earlier test and formula)
while (millis() - startMillis < sampleWindow)
{
sample = analogRead(SENSOR_PIN); //get reading from microphone
if (sample < 1024) // toss out spurious readings
{
if (sample > signalMax)
{
signalMax = sample; // save just the max levels
}
else if (sample < signalMin)
{
signalMin = sample; // save just the min levels
}
}
}
peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
int decibels = map(peakToPeak,20,900,49.5,90); //calibrate for deciBels
int wholeNumber = int(peakToPeak);
// Calculate the ones and tens place as integers
int ones = wholeNumber % 1000;
int tens = int((peakToPeak - wholeNumber) * 10);
if (decibels <= 60) {
digitalWrite(green_ledBulb, HIGH);
digitalWrite(buzzer, LOW);
digitalWrite(red_ledBulb, LOW);
delay(100);
digitalWrite(yellow_ledBulb, LOW);
digitalWrite(red_ledBulb, LOW);
} else if (decibels >= 61 && decibels <= 70) {
digitalWrite(green_ledBulb, HIGH);
digitalWrite(yellow_ledBulb, HIGH);
digitalWrite(buzzer, LOW);
digitalWrite(red_ledBulb, LOW);
delay(100);
digitalWrite(red_ledBulb, LOW);
} else if (decibels >= threshold ) {
digitalWrite(red_ledBulb, HIGH);
digitalWrite(green_ledBulb, HIGH);
digitalWrite(yellow_ledBulb, HIGH);
digitalWrite(buzzer, HIGH);
delay(100);
digitalWrite(green_ledBulb, LOW);
digitalWrite(yellow_ledBulb, LOW);
}
//FOR ACTUAL TESTING - remove "//"
Serial.print("Peak Analog: ");
// Print leading zeros for the ones and tens place only if the whole number part is less than 1000
if (wholeNumber < 1000) {
if (ones < 100) {
Serial.print("0");
}
if (ones < 10) {
Serial.print("0");
}
if (tens < 10) {
Serial.print("0");
}
}
Serial.print(wholeNumber);
Serial.print(".");
Serial.print(tens);
Serial.print(" || ");
Serial.print("Loudness: ");
Serial.print(decibels);
Serial.println(" dB ");
} // LINEAR COMPUTATION