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)