Compacting code lines with mutiple parameters or unequal data

For an SD Card Audio Wave Player Design with push button to play assigned files from a folder present on the memory card I have this code below

if (digitalRead(2) == LOW)
    {
      tmrpcm.play("/1/001.wav");
    }

if (digitalRead(3) == LOW)
    {
      tmrpcm.play("/1/002.wav");
    }

if (digitalRead(4) == LOW)
    {
      tmrpcm.play("/1/003.wav");
    }

if (digitalRead(5) == LOW)
    {
      tmrpcm.play("/1/004.wav");
    }

if (digitalRead(6) == LOW)
    {
      tmrpcm.play("/1/005.wav");
    }

if (digitalRead(7) == LOW)
    {
      tmrpcm.play("/1/006.wav");
    }

if (digitalRead(8) == LOW)
    {
      tmrpcm.play("/1/007.wav");
    }


Now how it is possible to make it a compact one as its a bit lengthy occupying space or memory !!

Link the pin numbers and file names in a struct. Create an array of that struct for all combinations. Loop through the struct.

Ok

Actually sir I want to clear a base here

Would you please let me know a bit lil with a short script ?

I don't know what that means.

Like how it works actually with array and loop

Which loop and how it determines

On net I am not able to find a perfect link to make it explain way better

Start by putting the pin numbers in an array with a sensible name (e.g. songSelectPins); you have examples in your other topics.

Next you can use a for-loop to iterate through the array and read the pins and if the pin reads LOW, build the filename.
If the relationship between the index in the array and the filename is linear (based on the example that you gave, it is), you can use sprintf or snprintf (C library function - sprintf(); snprintf is a little safer) to build the filename and next pass it to tmrpcm.play().

Ok for Sprintf I ll proceed later after the basic gets clear
I am still a bit confused with some areas

like

where to make arrays ?
I mean Within Setup or before that ??

Is it something like

char songList1[16];

songSelectPins[16] = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)

sorry

its like

char songSelectPins[16] = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);

Showing errors

'songSelectPins' does not name a type; did you mean 'SD_ChipSelectPin'?

Totally getting confused Sir please help to get me out of this

on basis of this

I made this one

float songSelectPins[16] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};

No errors for now

Crude, but it demonstrates the idea:

struct PinWaveStruct {
  const uint8_t pin;
  const char wav[16];
};

PinWaveStruct choices[] = {
  {2, "/1/001.wav"},
  {3, "/1/002.wav"},
  {4, "/1/003.wav"},
  {5, "/1/004.wav"},
  {6, "/1/005.wav"},
  {7, "/1/006.wav"},
  {8, "/1/007.wav"}
};

void setup() {
  for (auto &c : choices) {
    pinMode(c.pin, INPUT_PULLUP);
  }
}

void loop() {
  for (auto &c : choices) {
    if(digitalRead(c.pin ==LOW)) {
      tmrpcm.play(c.wav);
    }
  }
}

Or maybe something like this

for ( uint8_t i = 2; i <= 8; ++i )
{
	if ( digitalRead( i ) == LOW )
    {
		static char fileName[] = "/1/00X.wav";
		fileName[5] = i - 1 + '0';
		tmrpcm.play( fileName );
		break;
    }
}

Personally, I think OP is fixating excessively on "compact" and "saving space" (goals whose details he's never explained and solving a problem he likely doesn't have). Given my perception of his coding abilities (based on questions and code posted), he'd be much better off writing code he understands and can maintain.

Don't use floats for pin numbers. There is no such thing as a pin with number 2.71 or a pin with number 3.14.

Post complete code using code tags and post complete error message, also in code tags. Do not post images of code !!

2.72…

a7

No, it's 2.71... or 2.72

But no wonder that I only barely passed my maths at university :laughing:

Creating a very dangerous kind of noise for at least 2 seconds

Nothing happens other than this

Ya sure I am not applying float

That picture is an example from internet page & its not mine

Is this a joke of some sort?

No, why do you think this is a joke ?