/*
File : Pengukur Kebisingan R0
Author : leveletech.com / wa 081214584114
Date : 2024 08 2030
Description : Alat untuk mengukur kebisingan
*/
//--------------------------------- Include Libraries-----------------------------------------------
#include <LiquidCrystal_I2C.h>
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
//--------------------------------- Include Libraries-----------------------------------------------
//--------------------------------- Hardware Setting------------------------------------------------
//--------------------------------- Hardware Setting------------------------------------------------
//--------------------------------- Define and Constant---------------------------------------------
//dot Matrix
// Uncomment according to your hardware type
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
//#define HARDWARE_TYPE MD_MAX72XX::GENERIC_HW
// Defining size, and output pins
#define MAX_DEVICES 8
#define CS_PIN 5
#define CLK_PIN 18
#define DATA_PIN 23
//SETTING RUNNING TEKS
// set to 1 if we are implementing the user interface pot, switch, etc
#define USE_UI_CONTROL 0
#if USE_UI_CONTROL
#include <MD_UISwitch.h>
#endif
// Turn on debug statements to the serial output
#define DEBUG 0
#if DEBUG
#define PRINT(s, x) { Serial.print(F(s)); Serial.print(x); }
#define PRINTS(x) Serial.print(F(x))
#define PRINTX(x) Serial.println(x, HEX)
#else
#define PRINT(s, x)
#define PRINTS(x)
#define PRINTX(x)
#endif
//--------------------------------- Define and Constant---------------------------------------------
//--------------------------------- User Define Data Type-------------------------------------------
//--------------------------------- User Define Data Type-------------------------------------------
//----------------------------------Global Variable-------------------------------------------------
//lcd
LiquidCrystal_I2C lcd(0x27, 16, 2);
//dot matrix
MD_Parola ledMatrix = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
//Sound Sensor
const int SENSOR_PIN = 4; // Pin ADC untuk membaca sensor suara
float kebisingan;
float vout; //variabel dari analog to Digital
float dB = 37.5; //penguat dalam (dB)
int val = 0; //input suara awal 0 (dalam Analog)
float vin = 1.50; //nilai referensi Aref = 77dB
int desibel; //variabel hasil akhir pengukuran suara (dB)
float calvout;
//Buzzer
const int buzzer = 19;
// Scrolling parameters
#if USE_UI_CONTROL
const uint8_t SPEED_IN = A5;
const uint8_t DIRECTION_SET = 8; // change the effect
const uint8_t INVERT_SET = 9; // change the invert
const uint8_t SPEED_DEADBAND = 2;
#endif // USE_UI_CONTROL
uint8_t scrollSpeed = 25; // default frame delay value
textEffect_t scrollEffect = PA_SCROLL_LEFT;
textPosition_t scrollAlign = PA_LEFT;
uint16_t scrollPause = 1500; // in milliseconds
// Global message buffers shared by Serial and Scrolling functions
#define BUF_SIZE 75
char curMessage[BUF_SIZE] = { "" };
char newMessage[BUF_SIZE] = { "HARAP TENANG !" };
bool newMessageAvailable = true;
//----------------------------------Global Variable-------------------------------------------------
//----------------------------------User Defined Function Stage 1-----------------------------------
void Update_Sound_Sensor() {
val = analogRead (SENSOR_PIN); //baca sensor suara dlm bentuk analog ke dalam variabel val
Serial.print("Sensor Analog : ");
Serial.println(val);
vout = val * (5.0 / 1023.0); // analog to digital converter
calvout = vout + 0.039; //vout ditambah 0.039 volt
float hitung = calvout / vin; //hitung penguatan (volt)
dB = (20.0 * log(hitung)); //penguatan dB
desibel = dB + 115; //nilai Referensi dB Gitar Tuner kunci G + 76dB
delay (80);
/*===============================
CATATAN KALIBRASI SENSOR
ANALOG -> NILAI dB
0 -> 37.3dB (normal tanpa ada suara tambahan)
================================*/
Serial.print(desibel);
Serial.println(" dB");
lcd.setCursor(0, 0);
lcd.print("SOUND SENSOR: ON");
lcd.setCursor(0, 1);
lcd.print("dB Meter : ");
lcd.setCursor(13, 1);
lcd.print("dB");
lcd.setCursor(10, 1);
lcd.print(desibel);
delay(20);
runningteks();
}
void runningteks() {
// WARNING NOISE LEVEL-----------SET HERE---------
if (desibel >= 60) { // If the sound level is more than 60 decibels
#if USE_UI_CONTROL
doUI();
#endif // USE_UI_CONTROL
if (ledMatrix.displayAnimate()) {
strcpy(curMessage, newMessage);
newMessageAvailable = true;
lcd.setBacklight(255);
lcd.home();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("OVER!!!");
lcd.setCursor(0, 1);
lcd.print(desibel);
lcd.setCursor(5, 1);
lcd.print("dB");
delay(50);
ledMatrix.displayReset();
for (int i = 0; i < 2; i++) {
digitalWrite(buzzer, HIGH);
delay(1000);
digitalWrite(buzzer, LOW);
delay(200);
}
for (int i = 0; i < 5; i++) {
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
delay(200);
}
}
} else if (desibel < 60) { // If the sound level is less than 60 decibels
#if USE_UI_CONTROL
doUI();
#endif // USE_UI_CONTROL
if (ledMatrix.displayAnimate()) {
strcpy(curMessage, newMessage);
newMessageAvailable = false;
}
}
// Optional: Uncomment if needed
// delay(100);
// Serial.println(dB);
}
//----------------------------------User Defined Function Stage 1-----------------------------------
//----------------------------------User Defined Function Stage 2-----------------------------------
//----------------------------------User Defined Function Stage 2-----------------------------------
//----------------------------------User Defined Function Stage 3-----------------------------------
//----------------------------------User Defined Function Stage 3-----------------------------------
//----------------------------------Testing Function------------------------------------------------
void testing() {
//write everyting for testing
/*
*/
}
//----------------------------------Testing Function------------------------------------------------
//--------------------------------- Setup----------------------------------------------
void setup() {
//Pin Initialization
pinMode (buzzer, OUTPUT); //pin7 to variabel buzz
//Start Services
//LCD
lcd.init();
lcd.backlight(); // turn on LCD backlight
//Dot Matrix
#if USE_UI_CONTROL
uiDirection.begin();
uiInvert.begin();
pinMode(SPEED_IN, INPUT); doUI();
#endif // USE_UI_CONTROL
ledMatrix.begin();
ledMatrix.displayText(curMessage, scrollAlign, scrollSpeed, scrollPause, scrollEffect, scrollEffect);
ledMatrix.setIntensity(0); // set the brightness of the LED matrix display (from 0 to 15)
ledMatrix.displayClear(); // clear LED matrix display
//serial
Serial.begin(9600);
delay(1000);
//first run
//buzzer on
digitalWrite (buzzer, 1); // buzzer berlogika 1
delay(500); // Lama waktu buzzer aktif
digitalWrite (buzzer, 0); // buzzer berlogika 0
delay(500); // lama waktu buzzer tidak aktif
//TAMPIL LCD
lcd.setCursor(0, 0);
lcd.print(" DETEKSI BISING");
lcd.setCursor(0, 1);
lcd.print(" SKRIPSI 2024");
delay(2000);
lcd.clear();
}
//--------------------------------- Setup----------------------------------------------
//--------------------------------- Loop-----------------------------------------------
void loop() {
testing();
Update_Sound_Sensor();
}
//--------------------------------- Loop-----------------------------------------------
//--------------------------------- Note-----------------------------------------------
/*
*/
//--------------------------------- Note-----------------------------------------------