Codigo mp3 para pruebas.
/***************************************************
DFPlayer - A Mini MP3 Player For Arduino
<https://www.dfrobot.com/product-1121.html>
***************************************************
This example shows the basic function of library for DFPlayer.
Created 2016-12-07
By [Angelo qiao]([email protected])
GNU Lesser General Public License.
See <http://www.gnu.org/licenses/> for details.
All above must be included in any redistribution
****************************************************/
/***********Notice and Trouble shooting***************
1.Connection and Diagram can be found here
<https://www.dfrobot.com/wiki/index.php/DFPlayer_Mini_SKU:DFR0299#Connection_Diagram>
2.This code is tested on Arduino Uno, Leonardo, Mega boards.
****************************************************/
#include "Arduino.h"
#include "DFRobotDFPlayerMini.h"
#include "SoftwareSerial.h"
SoftwareSerial mySoftwareSerial(5, 6); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
void setup() {
mySoftwareSerial.begin(9600);
Serial.begin(115200);
Serial.println();
Serial.println(F("DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
if (!myDFPlayer.begin(mySoftwareSerial, false)) { //Use softwareSerial to communicate with mp3.
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while (true) {
delay(0); // Code to compatible with ESP8266 watch dog.
}
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.volume(5); //Set volume value. From 0 to 30
mostrarMenu();
}
void loop() {
if (Serial.available()) {
char comando = Serial.read();
ejecutarComando(comando);
}
}
// Función para mostrar el menú
void mostrarMenu() {
Serial.println("\\n=== Menu de Reproducción MP3 ===");
Serial.println("1. Reproducir pista 1");
Serial.println("2. Reproducir pista 2");
Serial.println("3. Reproducir pista 3");
Serial.println("4. Pausar");
Serial.println("5. Reanudar");
Serial.println("6. Detener");
Serial.println("7. Ajustar volumen");
Serial.println("=============================");
Serial.println("Ingrese una opción: ");
}
// Función para ejecutar los comandos del menú
void ejecutarComando(char comando) {
switch (comando) {
case '1':
myDFPlayer.play(1); // Reproducir la pista 1
Serial.println("Reproduciendo pista 1...");
break;
case '2':
myDFPlayer.play(2); // Reproducir la pista 2
Serial.println("Reproduciendo pista 2...");
break;
case '3':
myDFPlayer.play(3); // Reproducir la pista 3
Serial.println("Reproduciendo pista 3...");
break;
case '4':
myDFPlayer.pause(); // Pausar la reproducción
Serial.println("Reproducción pausada");
break;
case '5':
myDFPlayer.start(); // Reanudar la reproducción
Serial.println("Reproducción reanudada");
break;
case '6':
myDFPlayer.stop(); // Detener la reproducción
Serial.println("Reproducción detenida");
break;
case '7':
ajustarVolumen(); // Permite ingresar un valor de volumen
break;
default:
Serial.println("Opción no válida. Intente nuevamente.");
break;
}
mostrarMenu(); // Muestra el menú nuevamente tras ejecutar un comando
}
// Función para ajustar el volumen ingresando un valor de 0 a 30
void ajustarVolumen() {
Serial.println("Ingrese el volumen (0-30): ");
while (Serial.available() > 0) { // Limpia el buffer serial
Serial.read();
}
delay(200); // Pausa para permitir la entrada de datos
while (!Serial.available()); // Espera a que se ingrese el valor de volumen
int vol = Serial.parseInt(); // Lee el valor ingresado
if (vol >= 0 && vol <= 30) {
myDFPlayer.volume(vol);
Serial.print("Volumen ajustado a: ");
Serial.println(vol);
} else {
Serial.println("Valor de volumen no válido. Intente nuevamente.");
}
}
Codigo mp3 activacion con los sensores infrarrojos
#include "Arduino.h"
#include "DFRobotDFPlayerMini.h"
#include "SoftwareSerial.h"
SoftwareSerial mySoftwareSerial(5, 6); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
// Pines
const int sensorPin1 = A2; // Pin del primer sensor infrarrojo
const int sensorPin2 = A1; // Pin del segundo sensor infrarrojo
// Umbrales para cada sensor
const int threshold1 = 550; // Umbral para el sensor 1
const int threshold2 = 100; // Umbral para el sensor 2
int previousValue1 = 0;
int previousValue2 = 0;
int currentValue1 = 0;
int currentValue2 = 0;
void setup() {
mySoftwareSerial.begin(9600);
Serial.begin(9600);
Serial.println();
Serial.println(F("DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
if (!myDFPlayer.begin(mySoftwareSerial, false)) { //Use softwareSerial to communicate with mp3.
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while (true) {
delay(0);
}
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.volume(20); //Set volume value. From 0 to 30
myDFPlayer.stop(); // Detener la reproducción
pinMode(sensorPin1, INPUT);
pinMode(sensorPin2, INPUT);
}
void loop() {
// Leer los valores actuales de ambos sensores
currentValue1 = analogRead(sensorPin1);
currentValue2 = analogRead(sensorPin2);
// Calcular las diferencias entre los valores anteriores y los actuales
int change1 = abs(currentValue1 - previousValue1);
int change2 = abs(currentValue2 - previousValue2);
// Verificar el cambio en el primer sensor con su umbral
if (change1 > threshold1) {
Serial.println("Sensor 1: Acción activada: 1");
myDFPlayer.play(1); // Reproducir la pista 1
Serial.println("Reproduciendo pista 1...");
delay(3600000);
// Aquí puedes activar la acción del sensor 1
} else {
Serial.println("Sensor 1: Acción desactivada: 0");
myDFPlayer.stop(); // Detener la reproducción
Serial.println("Reproducción detenida");
// Aquí puedes desactivar la acción del sensor 1
}
// Verificar el cambio en el segundo sensor con su umbral
// if (change2 > threshold2) {
// Serial.println("Sensor 2: Acción activada: 1");
// // Aquí puedes activar la acción del sensor 2
// } else {
// Serial.println("Sensor 2: Acción desactivada: 0");
// // Aquí puedes desactivar la acción del sensor 2
// }
// // Actualizar los valores anteriores para los dos sensores
// previousValue1 = currentValue1;
// previousValue2 = currentValue2;
// Pausa para evitar lecturas continuas demasiado rápidas
delay(100); // Ajustar según sea necesario
}
codigo iluminacion
const int g = 5; // Pin PWM 1
const int b = 18; // Pin PWM 2
const int r = 19; // Pin PWM 3
int pwmValue1 = 0; // Valor inicial para PWM 1
int pwmValue2 = 0; // Valor inicial para PWM 2
int pwmValue3 = 0; // Valor inicial para PWM 3
void setup() {
// Configurar los pines como salida.
pinMode(g, OUTPUT);
pinMode(b, OUTPUT);
pinMode(r, OUTPUT);
// Iniciar la comunicación serial.
Serial.begin(9600);
Serial.println("Introduce 3 valores PWM (0 a 255) separados por comas:");
}
void loop() {
// Verificar si hay datos disponibles en el puerto serial.
if (Serial.available() > 0) {
// Leer el valor introducido por el puerto serial en formato de cadena.
String input = Serial.readStringUntil('\\n');
// Dividir la cadena en valores separados por comas.
int commaIndex1 = input.indexOf(',');
int commaIndex2 = input.lastIndexOf(',');
if (commaIndex1 > 0 && commaIndex2 > commaIndex1) {
// Extraer y convertir los valores para cada pin PWM.
pwmValue1 = input.substring(0, commaIndex1).toInt();
pwmValue2 = input.substring(commaIndex1 + 1, commaIndex2).toInt();
pwmValue3 = input.substring(commaIndex2 + 1).toInt();
// Limitar los valores entre 0 y 255.
pwmValue1 = constrain(pwmValue1, 0, 255);
pwmValue2 = constrain(pwmValue2, 0, 255);
pwmValue3 = constrain(pwmValue3, 0, 255);
// Aplicar los valores PWM a cada pin.
analogWrite(g, pwmValue1);
analogWrite(b, pwmValue2);
analogWrite(r, pwmValue3);
// Mostrar los valores en el puerto serial.
Serial.print("PWM Pin 1: ");
Serial.println(pwmValue1);
Serial.print("PWM Pin 2: ");
Serial.println(pwmValue2);
Serial.print("PWM Pin 3: ");
Serial.println(pwmValue3);
} else {
Serial.println("Entrada inválida. Introduce 3 valores separados por comas.");
}
}
}