salut à toutes et tous.
mon projet de pédale backingtrack arrive bientôt au bout.
j'ai toujours un ptit problème dans mon code pour régler le volume avec le potentiomètre.
Il est branché sur la broche A0 de l'arduino Uno.
Une âme charitable pour un coup de main pour me trouver l'erreur qui me fait planter le DFplayer ou l'arduino. Les deux deviennent incontrôlables : action sur les pédales inutile et affichage Led en vrille.
Merci d'avance.
Voici le code pour gérer le potentiomètre et le DFplayer :
void loop () {
// Set volume to maximum (0 to 30).
myDFPlayer.volume(map(analogRead(A0), 0, 1023, 0, 30));
myLcd.setCursor(10, 0);
myLcd.print("VOL:");
if (myDFPlayer.readVolume() < 10)
myLcd.print(String ("0") + myDFPlayer.readVolume());
else
myLcd.print(myDFPlayer.readVolume());
le code total :
#include <LiquidCrystal_I2C.h>
// Create the LCD object
LiquidCrystal_I2C myLcd(0x27, 16, 2);
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
SoftwareSerial mySerial(10, 11); // RX, TX
# define Start_Byte 0x7E
# define Version_Byte 0xFF
# define Command_Length 0x06
# define End_Byte 0xEF
# define Acknowledge 0x00 //Returns info with command 0x41 [0x01: info, 0x00: no info]
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
int PinButtonPrevious = 3;
int PinButtonPlay = 4;
int PinButtonStop = 5;
int PinButtonNext = 6;
int PinButtonReset = 7;
boolean isPlaying = true;
# define ACTIVATED LOW
void setup()
{
myLcd.init();
myLcd.backlight();
mySerial.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(mySerial)) { //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);
}
Serial.println(F("DFPlayer Mini online."));
myLcd.print("Ready");
myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms
//----Set volume----
myDFPlayer.volume(20); //Set volume value (0~30).
//----Set different EQ----
myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
//----Set device we use SD as default----
myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
// myDFPlayer.reset(); //Reset the module
//----Read imformation----
Serial.println(myDFPlayer.readState()); //read mp3 state
Serial.println(myDFPlayer.readVolume()); //read current volume
Serial.println(myDFPlayer.readEQ()); //read EQ setting
Serial.println(myDFPlayer.readFileCounts()); //read all file counts in SD card
Serial.println(myDFPlayer.readCurrentFileNumber()); //read current play file number
Serial.println(myDFPlayer.readFileCountsInFolder(3)); //read fill counts in folder SD:/03
pinMode(PinButtonPlay, INPUT_PULLUP);
pinMode(PinButtonStop, INPUT_PULLUP);
pinMode(PinButtonNext, INPUT_PULLUP);
pinMode(PinButtonPrevious, INPUT_PULLUP);
pinMode(PinButtonReset, INPUT_PULLUP);
}
void loop(){
// Set volume to maximum (0 to 30).
myDFPlayer.volume(map(analogRead(A0), 0, 1023, 0, 30));
myLcd.setCursor(10, 0);
myLcd.print("VOL:");
if (myDFPlayer.readVolume() < 10)
myLcd.print(String ("0") + myDFPlayer.readVolume());
else
myLcd.print(myDFPlayer.readVolume());
if (digitalRead(PinButtonPlay) == ACTIVATED)
{
play();
}
if (digitalRead(PinButtonStop) == ACTIVATED)
{
stop();
}
if (digitalRead(PinButtonNext) == ACTIVATED)
{
playNext();
}
if (digitalRead(PinButtonPrevious) == ACTIVATED)
{
playPrevious();
}
if (digitalRead(PinButtonReset) == ACTIVATED)
{
reset();
}
if (myDFPlayer.available()) {
printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
}
}
void printDetail(uint8_t type, int value){
switch (type) {
case TimeOut:
Serial.println(F("Time Out!"));
break;
case WrongStack:
Serial.println(F("Stack Wrong!"));
break;
case DFPlayerCardInserted:
Serial.println(F("Card Inserted!"));
break;
case DFPlayerCardRemoved:
Serial.println(F("Card Removed!"));
break;
case DFPlayerCardOnline:
Serial.println(F("Card Online!"));
break;
case DFPlayerPlayFinished:
Serial.print(F("Number:"));
Serial.print(value);
Serial.println(F(" Play Finished!"));
break;
case DFPlayerError:
Serial.print(F("DFPlayerError:"));
switch (value) {
case Busy:
Serial.println(F("Card not found"));
break;
case Sleeping:
Serial.println(F("Sleeping"));
break;
case SerialWrongStack:
Serial.println(F("Get Wrong Stack"));
break;
case CheckSumNotMatch:
Serial.println(F("Check Sum Not Match"));
break;
case FileIndexOut:
Serial.println(F("File Index Out of Bound"));
break;
case FileMismatch:
Serial.println(F("Cannot Find File"));
break;
case Advertise:
Serial.println(F("In Advertise"));
break;
default:
break;
}
break;
default:
break;
}
}
void stop()
{
execute_CMD(0x16,0,0);
delay(500);
Serial.println ("Stoppé:");
myLcd.clear();
myLcd.setCursor(0,0);
myLcd.print("Stop de :");
myLcd.setCursor(0,1);
displayFileName();
}
void play()
{
execute_CMD(0x0D,0,0);
delay(500);
Serial.println ("Play:");
myLcd.clear();
myLcd.setCursor(0,0);
myLcd.print("Playing...");
myLcd.setCursor(0,1);
displayFileName();
}
void playNext()
{
execute_CMD(0x01,0,0);
delay(500);
Serial.println ("Next :");
myLcd.clear();
myLcd.setCursor(0,0);
myLcd.print("Playing...");
myLcd.setCursor(0,1);
displayFileName();
}
void playPrevious()
{
execute_CMD(0x02,0,0);
delay(500);
Serial.println ("Clic Previous :");
myLcd.clear();
myLcd.setCursor(0,0);
myLcd.print("Playing...");
myLcd.setCursor(0,1);
displayFileName();
}
void reset()
{
execute_CMD(0x0C,0,0);
}
void displayFileName()
{
int fileNumber = myDFPlayer.readCurrentFileNumber();
switch (fileNumber)
{
case 1:
myLcd.print("Manu All");
Serial.println ("musique1.mp3");
break;
case 2:
myLcd.print("Manu Saxs");
Serial.println ("musique2.mp3");
break;
case 3:
myLcd.print("Manu Batterie ");
Serial.println ("musique3.mp3");
break;
case 4:
myLcd.print("Renaud");
Serial.println ("musique4.mp3");
break;
case 5:
myLcd.print("Clandestino");
Serial.println ("musique5.mp3");
break;
default:
break;
}
}
void execute_CMD(byte CMD, byte Par1, byte Par2)
// Excecute the command and parameters
{
// Calculate the checksum (2 bytes)
word checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);
// Build the command line
byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge,
Par1, Par2, highByte(checksum), lowByte(checksum), End_Byte};
//Send the command line to the module
for (byte k=0; k<10; k++)
{
mySerial.write( Command_line[k]);
}
}