• Post category:Boîte à musique
  • Commentaires de la publication :0 commentaire
  • Dernière modification de la publication :mars 12, 2022
  • Temps de lecture :14 min de lecture

Boîte à musique DIY (Arduino NANO + DFplayer + TP4056 + batterie 18650) *

Description :

Petite boîte à music sur batterie rechargeable.

Elle permet la lecture des chansons présentes sur la carte SD du D. fplayer.

Elle dispose d’un mode veille, activer par un appui prolonger sur la touche ou une inactivité sur les boutons au bout d’un certain temps.

 

 

    Prérequis :

Matériel :

  • 1 x Carte Arduino Nano
  • 3 x Boutons
  • 1 x Ds1302
  • 1 x TP4056
  • 1 x Led RGB
  • 3 x Résistances 220Ω
  • 1 x Résistance 470Ω
  • 1 x Résistance 2KΩ
  • 1 x Résistance 1KΩ
  • 1 x Potentiomètres 10 KΩ
  • 1 x Batterie 18650
  • 1 x Led RGB
  • 1 x Transistor NPN
  • 1 x Mini haut-parleur en corne Ultra-mince, 2 W, 8 Ohm
  • 1 x Dfplayer
  • Fils de connexion
  • 1 x Breadboard

Version IDE :

Bibliothèque :

  • DFPlayerMini_Fast.h

 

 

Vidéo de démonstration :

 

Schéma de câblage :

 

Code :

#include <avr/sleep.h>
#include <DFPlayerMini_Fast.h>
#if !defined(UBRR1H)
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
#endif
DFPlayerMini_Fast myMP3;

int AlimDfplayer = 7; //Broche pour alimenter Transistor NPN 2N2222
int boutonstartstop = 2; //Broche du bouton play/stop
int boutonprev = 3; //Broche du bouton precedent
int boutonnext = 4; //Broche du bouton suivant
int potvolume = A4; //Broche du potentiomètre volume
int pontdiviseur = A3; //Broche de mesure du pont diviseur
float r1 = 1998; // Valeur de la résistance R1
float r2 = 471; // Valeur de la réstance R2
int Maxvolume = 20; // Valeur du volume souhaité MAXI ("Valeur Max possible 30")
float batteriefaible = 3.80; // Valeur de tension pour signaler la batterie faible

boolean Etatboutonstartstop = 0;
boolean Etatboutonprev = 0;
boolean Etatboutonnext = 0;
boolean LectureStartStop = 0;

unsigned long timer;
unsigned long Timerboutonstartstop;
unsigned long Timersleep;
int Valvolume;
const int LED[3] = {5 , 6, 9};
unsigned long TimerAnimation[2] = { 0, 0};
int LEDlum[2] = { 0, 0};
int menuanimation = 1;
float ARef = 1.1;
int signalbatteriefaible;

void setup() {
analogReference(INTERNAL);
Serial.begin(115200);
#if !defined(UBRR1H)
mySerial.begin(9600);
myMP3.begin(mySerial, true);
#else
Serial1.begin(9600);
myMP3.begin(Serial1, true);
#endif
pinMode(AlimDfplayer, OUTPUT);
digitalWrite(AlimDfplayer,HIGH);
delay(800);
for (int i=0; i <= 2; i++){
pinMode(LED[i], OUTPUT);
digitalWrite(LED[i],LOW);
}
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN,LOW);
pinMode(boutonstartstop,INPUT_PULLUP);
pinMode(boutonprev,INPUT_PULLUP);
pinMode(boutonnext,INPUT_PULLUP);
myMP3.pause();
}

void loop() {
int raw = analogRead(pontdiviseur);
float vout = raw * 1.1 / 1023 / (r2 / (r1+r2));
Serial.println(raw);
timer = millis();
/////Gestion Bouton volume /////////////////////////////////////////////
int valvolume = analogRead(potvolume);
valvolume = map(valvolume, 0, 1023, 0, Maxvolume);
if(Valvolume != valvolume){
myMP3.volume(valvolume);
Valvolume = valvolume;
}
////////////////////////////////////////////////////////////////////////
/////Gestion Bouton boutonstartstop ////////////////////////////////////
if(digitalRead(boutonstartstop) == LOW && Etatboutonstartstop == 0){
Timerboutonstartstop = timer;
Etatboutonstartstop = 1;
}
if(digitalRead(boutonstartstop) == HIGH && timer < Timerboutonstartstop + 1500 && Etatboutonstartstop == 1){
Etatboutonstartstop = 0;
LectureStartStop = !LectureStartStop;
if(LectureStartStop == 1){
myMP3.resume();
//myMP3.randomAll();
}
else{
myMP3.pause();
Resetanimations();
}
}
else if (timer > Timerboutonstartstop + 1500 && Etatboutonstartstop == 1 ){
myMP3.stop();
while (digitalRead(boutonstartstop) == LOW) {
}
Going_To_Sleep();
LectureStartStop = 0;
Etatboutonstartstop = 0;
}
////////////////////////////////////////////////////////////////////////
/////Gestion Bouton boutonprev /////////////////////////////////////////
if (digitalRead(boutonprev) == LOW && Etatboutonprev == 0){
Etatboutonprev = 1;
myMP3.playPrevious();
menuanimation++;
Resetanimations();
}
else if (digitalRead(boutonprev) == HIGH && Etatboutonprev == 1){
Etatboutonprev = 0;
}
////////////////////////////////////////////////////////////////////////
/////Gestion Bouton boutonnext /////////////////////////////////////////
if (digitalRead(boutonnext) == LOW && Etatboutonnext == 0){
Etatboutonnext = 1;
myMP3.playNext();
menuanimation++;
Resetanimations();
}
else if (digitalRead(boutonnext) == HIGH && Etatboutonnext == 1){
Etatboutonnext = 0;
}
////////////////////////////////////////////////////////////////////////
///// Gestion alerte batterie faible //////////////////////////////////
if(vout < batteriefaible){
menuanimation = 0;
signalbatteriefaible = 1;
}
else if (vout > batteriefaible + 0.10){
signalbatteriefaible = 0;
}
if (signalbatteriefaible == 0 && menuanimation == 0){
menuanimation++;
}
////////////////////////////////////////////////////////////////////////
///// Mise en Sleep si aucune activiter pendant 2 minute DFPLAYER /////
if(myMP3.isPlaying() == 0){
if(timer > Timersleep + 120000){
Going_To_Sleep();
}
}
else{
Timersleep = timer;
}
////////////////////////////////////////////////////////////////////////
///// Synchro DFplayer programme ///////////////////////////////////////
if(myMP3.isPlaying() == 0){
LectureStartStop = 0;
}
else{
LectureStartStop = 1;
}
////////////////////////////////////////////////////////////////////////
///// Gestion animations ///////////////////////////////////////////////
if(LectureStartStop == 1 || vout < batteriefaible){
switch (menuanimation) {
case 0:
Serial.println(LED[0]);
digitalWrite(LED[1], LOW);
digitalWrite(LED[2], LOW);
animationbatteriefaible();
break;
case 1:
animation(0);
break;
case 2:
animation(1);
break;
case 3:
animation(2);
break;
case 4:
menuanimation = 1;
break;
case 5:
break;
default:
break;
}
}
else{
}
}

void animationbatteriefaible(){
if(TimerAnimation[1] == 0){
TimerAnimation[1] = timer;
}
else if(timer > TimerAnimation[1]+500 && timer < TimerAnimation[1]+1000){
analogWrite(LED[0],50);
}
else if(timer > TimerAnimation[1]+1000 && timer < TimerAnimation[1]+3000){
analogWrite(LED[0],0);
}
else if(timer > TimerAnimation[1]+4000){
TimerAnimation[1] = timer;
}
}

int animation(int x){
if(TimerAnimation[0] == 0){
TimerAnimation[0] = timer;
}
if(timer > TimerAnimation[0]+50){
if(LEDlum[0] < 100 && LEDlum[1] == 0){
LEDlum[0]++;
TimerAnimation[0] = timer;
}
if(LEDlum[0] == 100){
LEDlum[1] = 1;
}
if (LEDlum[0] > 0 && LEDlum[1] == 1){
LEDlum[0]--;
TimerAnimation[0] = timer;
}
if(LEDlum[0] == 0){
LEDlum[1] = 0;
}
analogWrite(LED[x],LEDlum[0]);
}
}


void Resetanimations(){
for (int i=0; i <= 2; i++){
digitalWrite(LED[i], LOW);
}
for (int i=0; i <= 1; i++){
LEDlum[i] = 0;
TimerAnimation[i] = 0;
}

}
////////////////////////////////////////////////////////////////////////
//// Gestion Mode sleep ////////////////////////////////////////////////
void Going_To_Sleep() {
sleep_enable();
attachInterrupt(0, wakeUp, LOW);
attachInterrupt(1, wakeUp, LOW);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
digitalWrite(AlimDfplayer,LOW);
Resetanimations();
delay(500);
sleep_cpu();
digitalWrite(AlimDfplayer,HIGH);
delay(800);
Timersleep = timer;
#if !defined(UBRR1H)
mySerial.begin(9600);
myMP3.begin(mySerial, true);
#else
Serial1.begin(9600);
myMP3.begin(Serial1, true);
#endif
myMP3.volume(Valvolume);
myMP3.resume();
}

void wakeUp(){
Serial.println("interrupt");
sleep_disable();
detachInterrupt(0);
detachInterrupt(1);
}
////////////////////////////////////////////////////////////////////////

 


 

Laisser un commentaire