/*
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---------------------------------------------
//Blynk
#define BLYNK_TEMPLATE_ID "TMPL6Qp9ffkc4"
#define BLYNK_TEMPLATE_NAME "Yourman"
#define BLYNK_AUTH_TOKEN "tn3wyXwYpj0icCVBrWMht_45xixJH1CC"
#define BLYNK_PRINT Serial
//ESP32
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
//ESP8266
//#include <ESP8266WiFi.h>
//#include <BlynkSimpleEsp8266.h>
char auth[] = BLYNK_AUTH_TOKEN; //Auth Token
char ssid[] = "Wokwi-GUEST"; //nama hotspot yang digunakan
char pass[] = ""; //password hotspot yang digunakan
//dot Matrix
// Uncomment according to your hardware type
//#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
//#define HARDWARE_TYPE MD_MAX72XX::GENERIC_HW
// Defining size, and output pins
#define MAX_DEVICES 8
//ESP32
#define CS_PIN 5
#define CLK_PIN 18
#define DATA_PIN 23
//ESP8266
//#define CS_PIN 15 //D8
//#define CLK_PIN 14 //D5
//#define DATA_PIN 13 //D7
//--------------------------------- Define and Constant---------------------------------------------
//--------------------------------- User Define Data Type-------------------------------------------
//--------------------------------- User Define Data Type-------------------------------------------
//----------------------------------Global Variable-------------------------------------------------
//Blynk
int value0;
BlynkTimer timer;
//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 = 34; // Pin ADC untuk membaca sensor suara //ESP32
//const int SENSOR_PIN = A0; // Pin ADC untuk membaca sensor suara //ESP8266
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;
int desibel_limit = 60;
//Buzzer
//ESP32
const int buzzer = 19;
//ESP8266
//const int buzzer = 12; //D6
// Scrolling parameters
uint8_t scrollSpeed = 5; // default frame delay value
textEffect_t scrollEffect = PA_SCROLL_LEFT;
textPosition_t scrollAlign = PA_LEFT;
uint16_t scrollPause = 0; // in milliseconds
// Global message buffers shared by Serial and Scrolling functions
#define BUF_SIZE 100
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");
Serial.print(desibel_limit);
Serial.println(" dB Limit");
lcd.setCursor(0, 0);
lcd.print("SOUND SENSOR: ON");
lcd.setCursor(0, 1);
lcd.print("dB Meter : ");
lcd.setCursor(12, 1);
lcd.print("dB");
lcd.setCursor(10, 1);
lcd.print(desibel);
delay(20);
runningteks();
}
void runningteks() {
// WARNING NOISE LEVEL-----------SET HERE---------
if (desibel >= desibel_limit) { // If the sound level is more than 60 decibels
Blynk.logEvent("keadaan_bising", "keadaan bising");
Blynk.virtualWrite(V2, 1); //
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);
Serial.println("Alarm Bunyi");
delay(1000);
digitalWrite(buzzer, LOW);
digitalWrite(buzzer, HIGH);
Serial.println("Alarm Mati");
delay(1000);
}
for (int i = 0; i < 5; i++) {
digitalWrite(buzzer, HIGH);
Serial.println("Alarm Bunyi");
delay(200);
digitalWrite(buzzer, LOW);
Serial.println("Alarm Mati");
delay(200);
}
}
} else if (desibel < desibel_limit) { // If the sound level is less than 60 decibels
if (ledMatrix.displayAnimate()) {
strcpy(curMessage, newMessage);
newMessageAvailable = false;
}
Blynk.virtualWrite(V2, 0); //
}
}
BLYNK_WRITE(V1) {
value0 = param.asInt();
desibel_limit = value0;
Serial.println(desibel_limit);
}
void sendSensor() {
Blynk.virtualWrite(V0, desibel); //
//Serial.println(data);
if (desibel >= desibel_limit) {
Blynk.logEvent("smoke_detected", "smoke detected");
}
}
//----------------------------------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
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(2000); // Lama waktu buzzer aktif
digitalWrite (buzzer, 0); // buzzer berlogika 0
delay(2000); // 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();
//Blynk
//Blynk.begin(auth, ssid, pass); //memulai Blynk
timer.setInterval(2500L, sendSensor);
delay(2000);
Blynk.virtualWrite(V1, desibel_limit); //
}
//--------------------------------- Setup----------------------------------------------
//--------------------------------- Loop-----------------------------------------------
void loop() {
//testing();
Update_Sound_Sensor();
//Blynk.run();
//timer.run();
}
//--------------------------------- Loop-----------------------------------------------
//--------------------------------- Note-----------------------------------------------
/*
*/
//--------------------------------- Note-----------------------------------------------