89 lines
2.6 KiB
C++
89 lines
2.6 KiB
C++
#include "DFRobotDFPlayerMini.h"
|
|
|
|
// Define the pins for communication with DFPlayer Mini
|
|
static const uint8_t PIN_MP3_TX = 26; // Connects to module's RX
|
|
static const uint8_t PIN_MP3_RX = 27; // Connects to module's TX
|
|
|
|
// Use Serial2 for communication with DFPlayer Mini
|
|
HardwareSerial playerSerial(2);
|
|
HardwareSerial SerialAT(1); // Define SerialAT on hardware serial port 1
|
|
|
|
// Create the Player object
|
|
DFRobotDFPlayerMini player;
|
|
|
|
// Definisikan pin untuk sensor PIR dan IR
|
|
const int pirPin = 34;
|
|
const int irPin = 35;
|
|
|
|
// Variabel untuk menyimpan status sensor
|
|
int pirStatus = 0;
|
|
int irStatus = 0;
|
|
|
|
// Variabel untuk mencatat waktu terakhir kali sensor diperiksa
|
|
unsigned long previousMillis = 0;
|
|
const long interval = 1000; // interval untuk pembacaan sensor dalam milidetik
|
|
|
|
void setup() {
|
|
// Inisialisasi komunikasi serial untuk debugging
|
|
Serial.begin(115200);
|
|
|
|
// Init serial port for DFPlayer Mini
|
|
playerSerial.begin(9600, SERIAL_8N1, PIN_MP3_RX, PIN_MP3_TX);
|
|
|
|
// Init serial port for LoRa communication
|
|
SerialAT.begin(9600, SERIAL_8N1, 16, 17);
|
|
|
|
// Start communication with DFPlayer Mini
|
|
if (player.begin(playerSerial)) {
|
|
Serial.println("DFPlayer Mini ready.");
|
|
// Set volume to maximum (0 to 30)
|
|
player.volume(30);
|
|
} else {
|
|
Serial.println("Connecting to DFPlayer Mini failed!");
|
|
}
|
|
|
|
// Set pinMode untuk sensor
|
|
pinMode(pirPin, INPUT);
|
|
pinMode(irPin, INPUT);
|
|
|
|
// Tulis pesan awal ke serial monitor
|
|
Serial.println("Memulai program sensor PIR dan IR");
|
|
}
|
|
|
|
void loop() {
|
|
// Dapatkan waktu sekarang
|
|
unsigned long currentMillis = millis();
|
|
|
|
// Kirim pesan "Hello, LoRa!" setiap 500 ms
|
|
static unsigned long previousLoRaMillis = 0;
|
|
if (currentMillis - previousLoRaMillis >= 500) {
|
|
previousLoRaMillis = currentMillis;
|
|
SerialAT.println("Hello, LoRa!");
|
|
}
|
|
|
|
// Periksa apakah interval waktu untuk pembacaan sensor sudah berlalu
|
|
if (currentMillis - previousMillis >= interval) {
|
|
// Simpan waktu sekarang
|
|
previousMillis = currentMillis;
|
|
|
|
// Baca status sensor PIR
|
|
pirStatus = digitalRead(pirPin);
|
|
// Baca status sensor IR
|
|
irStatus = digitalRead(irPin);
|
|
|
|
// Periksa status PIR dan tampilkan hanya jika ada gerakan
|
|
if (pirStatus == HIGH) {
|
|
Serial.println("Gerakan terdeteksi oleh sensor PIR!");
|
|
SerialAT.println("PIR:Gerakan terdeteksi");
|
|
// Putar MP3 saat gerakan terdeteksi
|
|
player.play(1); // Ganti nomor dengan nomor track yang ingin Anda putar
|
|
}
|
|
|
|
// Periksa status IR (logika terbalik) dan tampilkan hanya jika ada objek
|
|
if (irStatus == LOW) {
|
|
Serial.println("Objek terdeteksi oleh sensor IR!");
|
|
SerialAT.println("IR:Objek terdeteksi");
|
|
}
|
|
}
|
|
}
|