Oke i have try the both options but likes that they do the same.
#include "LiquidCrystal_I2C.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
SoftwareSerial mySoftwareSerial(2, 3); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
#define butUp 12
#define butDown 11
#define butP 10
#define butM 9
#define btns A0
void changeMenu();
void dispMenu();
void mp3Bank1();
void mp3Bank2();
void mp3Bank3();
char menu = 0x01;
char set1 = 0x00, set2 = 0x00 , set3 = 0x00;
boolean t_butUp, t_butDown, t_butP, t_butM;
void setup() {
mySoftwareSerial.begin(9600);
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
while(true);
}
lcd.begin(20, 4);
lcd.setCursor(0, 0);
lcd.print(F("** Sound Sampler **"));
myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms
//----Set volume----
myDFPlayer.volume(30); //Set volume value (0~30).
//----Set different EQ----
myDFPlayer.EQ(DFPLAYER_EQ_ROCK);
//----Set device we use SD as default----
myDFPlayer.outputDevice(DFPLAYER_DEVICE_FLASH);
for (char i=9; i<13; i++){
}
t_butUp = 0x00;
t_butDown = 0x00;
t_butP = 0x00;
t_butM = 0x00;
}
void loop() {
changeMenu();
dispMenu();
}
//Functies
void changeMenu(){
if(digitalRead(butUp) == 0x00){ // UP
t_butUp = 0x01;
}
if(digitalRead(butDown) == 0x00){ //DOWN
t_butDown = 0x01;
}
if(digitalRead(butUp) && t_butUp){
t_butUp = 0x00;
lcd.clear();
menu++;
if(menu > 0x04){
menu = 0x01;
}
}
if(digitalRead(butDown) && t_butDown){
t_butDown = 0x00;
lcd.clear();
menu--;
if(menu < 0x01){
menu = 0x04;
}
}
}
void dispMenu(){
switch(menu){
case 0x01:
mp3Bank( 1 ); //Funzione controllo led1
break;
case 0x02:
mp3Bank( 2 ); //Funzione controllo led1
break;
}
}
/*begin mp3 bank*/
void mp3Bank( int bank )
{
int val = analogRead( btns );
int button = valToButton( val );
lcd.print( F("MP3 bank ") ); // F macro around double-quoted string constants saves RAM
lcd.print( bank );
if (button > 0) {
myDFPlayer.playFolder( bank, button );
lcd.setCursor(0, 4);
lcd.print( F("Playing: Sound ") );
lcd.print( button );
} else {
lcd.print( F("Invalid button") );
}
}
const int minButtonVal[] = { 400, 445, 480, 520, 570, 630, 700, 800, 900 };
const int arraySize = sizeof(minButtonVal) / sizeof( minButtonVal[0] ); // always works, even if you change the array above
const int NO_BUTTON = 0;
int valToButton( uint16_t val )
{
for (int i=0; i<arraySize; i++) {
if (val <= minButtonVal[ i ]) {
return i;
}
}
// Too big!
return NO_BUTTON;
}