Devices & Components
Arduino Nano
Breadboard (generic)
3 Watt Speaker
Jumper Wires
10k Resistor
Df Player Mini
LDR
1k Resistor
SD Card
Project description
Code
Code
arduino
1#include "SoftwareSerial.h" 2#include "DFRobotDFPlayerMini.h" 3 4// Use pins 2 and 3 to communicate with DFPlayer Mini 5static const uint8_t PIN_MP3_TX = 2; // Connects to module's RX 6static const uint8_t PIN_MP3_RX = 3; // Connects to module's TX 7SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX); 8 9const int LDR = A0; 10int input_val = 0; 11int x = 0; 12int y = 0; 13 14// Create the Player object 15DFRobotDFPlayerMini player; 16 17void setup() { 18 19 20 // Init USB serial port for debugging 21 Serial.begin(9600); 22 // Init serial port for DFPlayer Mini 23 softwareSerial.begin(9600); 24 25 // Start communication with DFPlayer Mini 26 if (player.begin(softwareSerial)) { 27 Serial.println("OK"); 28 29 // Set volume to maximum (0 to 30). 30 player.volume(30); 31 32 } else { 33 Serial.println("Connecting to DFPlayer Mini failed!"); 34 } 35} 36 37void loop() { 38 input_val = analogRead(LDR); 39 40 delay(1000); 41 42 if ((input_val < 100) && (x == 0)) 43 { 44 player.play(2); 45 x = 1; 46 y = 0; 47 Serial.print("Good night "); 48 } 49 50 else if ((input_val > 100) && (y == 0)) 51 { 52 player.play(1); 53 y = 1; 54 x = 0; 55 Serial.print("Good morning "); 56 } 57 58 59} 60
Comments
Only logged in users can leave comments