Make sketch shorter, simply

I've made a soundbox with a dfplayer and works fine, just the skecth is big, maybe someone can help me make it smaller and more transparent?

What we see here are buttons available when sample bank 1 is selected, 8 sounds are available from folder 01, this repeats itself as sample bank 2, it retrieves the sounds from folder 02. This is repeated 10 times so the sketch gets huge big.

Maybe someone has a solution to this? to make it easier to make it clear? Thank you

Here the code, when select mp3 bank1

void mp3Bank1(){

  lcd.print("MP3 bank 1");

  val = analogRead(btns);

  if (val >= 400 && val <= 445)
  {
   myDFPlayer.playFolder(01, 1);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 1");
  }
  else if (val >= 445 && val <= 480)
  {
   myDFPlayer.playFolder(01, 2);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 2");
  }
  else if (val >= 480 && val <= 520)
  {
   myDFPlayer.playFolder(01, 3);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 3");
  }
  else if (val >= 520 && val <= 570)
  {
   myDFPlayer.playFolder(01, 4);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 4");
  }
  else if (val >= 570 && val <= 630)
  {
   myDFPlayer.playFolder(01, 5);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 5");
  }
  else if (val >= 630 && val <= 700)
  {
   myDFPlayer.playFolder(01, 6);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 6");
  }
  else if (val >= 700 && val <= 800)
  {
   myDFPlayer.playFolder(01, 7);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 7");
  }
  else if (val >= 800 && val <= 900)
  {
   myDFPlayer.playFolder(01, 8);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 8");
  }

So this is working fine, when select mp3 bank 2, its the same code, only folder and display print changed:

void mp3Bank2(){

  lcd.print("MP3 bank 2");

  val = analogRead(btns);

  if (val >= 400 && val <= 445)
  {
   myDFPlayer.playFolder(02, 1);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 1");
  }
  else if (val >= 445 && val <= 480)
  {
   myDFPlayer.playFolder(02, 2);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 2");
  }
  else if (val >= 480 && val <= 520)
  {
   myDFPlayer.playFolder(02, 3);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 3");
  }
  else if (val >= 520 && val <= 570)
  {
   myDFPlayer.playFolder(02, 4);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 4");
  }
  else if (val >= 570 && val <= 630)
  {
   myDFPlayer.playFolder(02, 5);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 5");
  }
  else if (val >= 630 && val <= 700)
  {
   myDFPlayer.playFolder(02, 6);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 6");
  }
  else if (val >= 700 && val <= 800)
  {
   myDFPlayer.playFolder(02, 7);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 7");
  }
  else if (val >= 800 && val <= 900)
  {
   myDFPlayer.playFolder(02, 8);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 8");
  }

So and there is a mp3 bank 3, and mp3 bank 4 and 5 and 6 and 7 till 10

So the code is very long, can some help me to make it easier?

Thanks

this can be much shorter...

int lookup[] = {
  400, // 0
  445,
  480, // 2
  520,
  570,
  630,
  700,
  800,
  900, // 8
};

int  btns = 0;

int getButton(int value)
{
  for (int i = 0; i < sizeof(lookup) / sizeof(lookup[0]); i++)
  {
    if (value < lookup[i])
    {
      return i;
    }
  }
  return 0;
}

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  if (int val = getButton(analogRead(btns)))
  {
    //myDFPlayer.playFolder(02, val);
    //lcd.setCursor(0, 4);
    //lcd.print("Playing: Sound ");
    //lcd.print(val);
    Serial.print("Playing: Sound ");
    Serial.println(val);
    delay(5000);  // you will need some sort of mechanism to prevent the constant readings...
  }
}

Arguments to functions are a wonderful thing!

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

int valToButton( uint16_t val )
{
  for (int i=0; i<arraySize; i++) {
    if (val <= minButtonValue[ i ]) {
      return i;
    }
  }

  // Too big!
  return 0;
}

Then call this:

    mp3Bank( 2 );

... instead of this:

    mp3Bank2();

Etc.

Also notice how an array is "searched" for the analog value range for each button. It returns 0 when the value is too small (less than 400) or too big (greater than 900). You can add other values to the array, and the arraySize variable will "calculate" how many elements you are declaring.

(tee hee, BulldogLowell)

Why is the bank number not a parameter of the mp3Bank(...) function?

lcd.setCursor, lcd.print, and myDFPlayer.play should probably be in a function with a parameter as well.

  if (val >= 400 && val <= 445)  {  }
  else if (val >= 445 && val <= 480)  {  }
  else if (val >= 480 && val <= 520)  {  }
  else if (val >= 520 && val <= 570)  {  }
  else if (val >= 570 && val <= 630)  {  }
  else if (val >= 630 && val <= 700)  {  }
  else if (val >= 700 && val <= 800)  {  }
  else if (val >= 800 && val <= 900)  {  }

Can be:

  if(val < 400) ; // do nothing
  else if (val <= 445)  {  }
  else if (val <= 480)  {  }
  else if (val <= 520)  {  }
  else if (val <= 570)  {  }
  else if (val <= 630)  {  }
  else if (val <= 700)  {  }
  else if (val <= 800)  {  }
  else if (val <= 900)  {  }

-dev:
Arguments to functions are a wonderful thing!

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

int valToButton( uint16_t val )
{
  for (int i=0; i<arraySize; i++) {
    if (val <= minButtonValue[ i ]) {
      return i;
    }
  }

// Too big!
  return 0;
}



Then call this:



mp3Bank( 2 );




... instead of this:



mp3Bank2();




Etc.

Also notice how an array is "searched" for the analog value range for each button. It returns 0 when the value is too small (less than 400) or too big (greater than 900). You can add other values to the array, and the `arraySize` variable will "calculate" how many elements you are declaring.

(tee hee, BulldogLowell)

Okay, i try this but get a error:
'minButtonVal' was not declared in this scope

Show us your code:

(1) Use autoformat CTRL-T in the Arduino IDE to format your code.

(2) Copy/Paste your code into a forum post.

(3) Use code tags around your code.

You should do this with your error message as well.

Oke

Wel first this is the working code i already have, in this example there are 3 mp3 bank, but normaly its up to 10 or 11 banks, i think its to much to paste here, anyway the old code:

#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);
  }


  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:
        mp3Bank1();              //Funzione controllo  led1
      break;
    case 0x02:
        mp3Bank2();             //Funzione controllo  led2
       break;
    case 0x03:
        mp3Bank3();             //Funzione controllo  led2
      break;

  }
}





/*begin mp3 bank1*/
void mp3Bank1(){

  lcd.setCursor(0,1);
  lcd.print("mp3 bank 1");

  val = analogRead(btns);

  if (val >= 400 && val <= 445)
  {
   myDFPlayer.playFolder(01, 1);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 1");
  }
  else if (val >= 445 && val <= 480)
  {
   myDFPlayer.playFolder(01, 2);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 2");
  }
  else if (val >= 480 && val <= 520)
  {
   myDFPlayer.playFolder(01, 3);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 3");
  }
  else if (val >= 520 && val <= 570)
  {
   myDFPlayer.playFolder(01, 4);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 4");
  }
  else if (val >= 570 && val <= 630)
  {
   myDFPlayer.playFolder(01, 5);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 5");
  }
  else if (val >= 630 && val <= 700)
  {
   myDFPlayer.playFolder(01, 6);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 6");
  }
  else if (val >= 700 && val <= 800)
  {
   myDFPlayer.playFolder(01, 7);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 7");
  }
  else if (val >= 800 && val <= 900)
  {
   myDFPlayer.playFolder(01, 8);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 8");
  

 if(digitalRead(butP) == 0x00){ 
    t_butP = 0x01;
  }
  if(digitalRead(butM) == 0x00){  
    t_butM = 0x01;
  }
  if(digitalRead(butP) && t_butP){ 
    t_butP = 0x00;
    set3++;
    if(set3 > 2){      
      set3 = 0x01;
    }
  }
  }
  }





/*begin mp3 bank2*/
void mp3Bank2(){
 

  lcd.setCursor(0,1);
  lcd.print("mp3 bank 2");

   val = analogRead(btns);

  if (val >= 400 && val <= 445)
  {
   myDFPlayer.playFolder(02, 1);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 1");
  }
  else if (val >= 445 && val <= 480)
  {
   myDFPlayer.playFolder(02, 2);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 2");
  }
  else if (val >= 480 && val <= 520)
  {
   myDFPlayer.playFolder(02, 3);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 3");
  }
  else if (val >= 520 && val <= 570)
  {
   myDFPlayer.playFolder(02, 4);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 4");
  }
  else if (val >= 570 && val <= 630)
  {
   myDFPlayer.playFolder(02, 5);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 5");
  }
  else if (val >= 630 && val <= 700)
  {
   myDFPlayer.playFolder(02, 6);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 6");
  }
  else if (val >= 700 && val <= 800)
  {
   myDFPlayer.playFolder(02, 7);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 7");
  }
  else if (val >= 800 && val <= 900)
  {
   myDFPlayer.playFolder(02, 8);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 8");

 if(digitalRead(butP) == 0x00){ 
    t_butP = 0x01;
  }
  if(digitalRead(butM) == 0x00){  
    t_butM = 0x01;
  }
  if(digitalRead(butP) && t_butP){ 
    t_butP = 0x00;
    set3++;
    if(set3 > 2){      
      set3 = 0x01;
    }
        

  }
}
}



/*Begin mp3 bank3*/
void mp3Bank3(){

    lcd.setCursor(0,1);
  lcd.print("mp3 bank 3");
  
  val = analogRead(btns);

  if (val >= 400 && val <= 445)
  {
   myDFPlayer.playFolder(03, 1);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 1");
  }
  else if (val >= 445 && val <= 480)
  {
   myDFPlayer.playFolder(03, 2);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 2");
  }
  else if (val >= 480 && val <= 520)
  {
   myDFPlayer.playFolder(03, 3);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 3");
  }
  else if (val >= 520 && val <= 570)
  {
   myDFPlayer.playFolder(03, 4);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 4");
  }
  else if (val >= 570 && val <= 630)
  {
   myDFPlayer.playFolder(03, 5);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 5");
  }
  else if (val >= 630 && val <= 700)
  {
   myDFPlayer.playFolder(03, 6);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 6");
  }
  else if (val >= 700 && val <= 800)
  {
   myDFPlayer.playFolder(03, 7);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 7");
  }
  else if (val >= 800 && val <= 900)
  {
   myDFPlayer.playFolder(03, 8);
   lcd.setCursor(0, 4);
   lcd.print("Playing: Sound 8");
  
  if(digitalRead(butP) == 0x00){
   
    t_butP = 0x01;
  }
  
  if(digitalRead(butM) == 0x00){
   
    t_butM = 0x01;
    
  }
  
  if(digitalRead(butP) && t_butP){
    
    t_butP = 0x00;
    set3++;
    
    if(set3 > 2){
      
      set3 = 0x01;
    }
    

  }

  }
}

And must be lik this?

#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);
  }


  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:
        mp3Bank1();              //Funzione controllo  led1
      break;
    case 0x02:
        mp3Bank2();             //Funzione controllo  led2
       break;
    case 0x03:
        mp3Bank3();             //Funzione controllo  led2
      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

int valToButton( uint16_t val )
{
  for (int i=0; i<arraySize; i++) {
    if (val <= minButtonValue[ i ]) {
      return i;
    }
  }

  // Too big!
  return 0;
}

  

 if(digitalRead(butP) == 0x00){ 
    t_butP = 0x01;
  }
  if(digitalRead(butM) == 0x00){  
    t_butM = 0x01;
  }
  if(digitalRead(butP) && t_butP){ 
    t_butP = 0x00;
    set3++;
    if(set3 > 2){      
      set3 = 0x01;
    }
  }
  }
  }

'minButtonValue' was not declared in this scope

But minButtonVal was

Nataschabakker:
Okay, i try this but get a error:
'minButtonVal' was not declared in this scope

Are you sure the error wasn't: minButtonValue' was not declared in this scope?

It's just a typo, like evanmars suggested:

   if (val <= minButtonValue[ i ]) {
                          ^

That should be "minButtonVal". Since we don't have your hardware, we can't always test. And if the program is fairly simple, I don't (or can't) always compile it. Simple misspellings is a common mistake, and after you make a few, you will spot them fairly quickly.

yeaaah correct its working now, it was the problem, val.
Only the display seems to be looping now?

I have made a video so u can see what happening, sound is working only the display seems to be in a loop or something.

http://tst.webcentre.nl/Video.MOV

We would love to see the current sketch. :-/

#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( 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

int valToButton( uint16_t val )
{
  for (int i=0; i<arraySize; i++) {
    if (val <= minButtonVal[ i ]) {
      return i;
    }
  }

  // Too big!
  return 0;
}
  for (char i=9; i<13; i++){       
  }

Well, that's useful. NOT.

int valToButton( uint16_t val )
{
  for (int i=0; i<arraySize; i++) {
    if (val <= minButtonVal[ i ]) {
      return i;
    }
  }

  // Too big!
  return 0;
}

This function will return 0 when val is less than minButtonVal[0] or when val is greater than minButtonVal[8]. Having 0 mean two different things is a BAD idea.

he display seems to be in a loop

It's always redrawing the LCD because you told it to do that. loop always calls dispMenu. Maybe you should only call dispMenu when the menu number changes?

You should also debounce these buttons, unless you are using special hardware to do that (resistors, capacitors or logic gates). HackADay has a nice two-part series on the topic, and the Bounce2 library is popular.

Having 0 mean two different things is a BAD idea.

Hmm, I think it means only one thing. This may make it more obvious:

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;
}

You could return 0 or arraySize instead, perhaps requiring two tests to know the button is invalid:

int valToButton( uint16_t val )
{
  int i=0;

  while ((i < arraySize) && (val >= minButtonVal[ i ])) {
    i++;
  }

  return i;
}

This may make it more obvious

If val is 380, what will that function return? According to my fat-fingered calculations, 380 is less than minButtonVal[0], so the function should return 0, indicating that the 0th switch was pressed. But, 0 means no switch was pressed.

Why doesn't -1 mean that no switch was pressed?

PaulS:
If val is 380, what will that function return? According to my fat-fingered calculations, 380 is less than minButtonVal[0], so the function should return 0, indicating that the 0th switch was pressed. But, 0 means no switch was pressed.

Why doesn't -1 mean that no switch was pressed?

In the OP, there is no 0 button. They start at 1, so 0 can be used to mean "no button". BulldogLowell's function does the same. For a val of 380, both of our functions return 0. The test inside my mp3Bank makes sure there is a valid button number:

      if (button > 0) {

-1 could be used, but I was just matching the OP.