Difficulty understanding data type conversion

First the code:

#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

int sample1 = 0;//keep track of sample number selected


const char *mySamples[] = {"\"EERIE.WAV\"", "\"LA.WAV\"", "\"RCALOCK3.WAV\"",
                     "\"TIMESPAN.WAV\"", "\"BEADS.WAV\"", "\"RCA2.WAV\"", "\"RCALOCK2.WAV\"", "\"RCALOOP3.WAV\""};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(11520);
  SPI.setMOSI(7);
  SPI.setSCK(14);
  if (!(SD.begin(10))) {
    while (1) {
      Serial.println("Unable to access the SD card");
      delay(500);
    }
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  playSdWav1.play(mySamples[0]);
  delay(10); //to allow parsing of SD
}

I am looking to create a music sampler. One thing I wish to do at some point is have a user push one of 8 buttons, and when that button is pressed it will write a value (0-7 depending on which is pressed) to the variable "sample1=0". I hope to put sample1 inside the function as such:

playSdWav1.play(mySamples[sample1]);

My goal here is to use the value of sample1 as a reference for the a certain filename in the mySamples array. That way if sample1 = 0, the function will read the following:

playSdWav1.play("EERIE.WAV");

And then it will play that sample. The issue is, it is not playing the sample if I do the referencing. I have done some troubleshooting and found several things. One, if I write:

playSdWav1.play("EERIE.WAV");

It will play the sample perfectly. I have tried increasing the delay time to 1000 ms, thinking I'm not giving enough time to do the referencing and still no luck.

Now, if I write the following:

Serial.println(mySamples[sample1]);

The Serial prints: "EERIE.WAV". So I know it might not be an issue with the quotation marks. If I do:

Serial.println(sizeof[mySamples[0]);

The serial returns a value of 4. This is weird, aren't characters 1 or 2 bytes. Is it possibly converting the name "EERIE.WAV" to an int? One more thing I've found. The following was taken from the cpp file for the playSdWav function. It requires a const char *filename:

AudioPlaySdWav::play(const char *filename)

So I am confused at this point why when I called the array mySamples, I'm not able to receive a const char*

Thank you

Try changing this:-

const char *mySamples[] = {"\"EERIE.WAV\"", "\"LA.WAV\"", "\"RCALOCK3.WAV\"",
                     "\"TIMESPAN.WAV\"", "\"BEADS.WAV\"", "\"RCA2.WAV\"", "\"RCALOCK2.WAV\"", "\"RCALOOP3.WAV\""};

to this:-

const char *mySamples[] =
{
    "EERIE.WAV", "LA.WAV", "RCALOCK3.WAV","TIMESPAN.WAV",
    "BEADS.WAV", "RCA2.WAV", "RCALOCK2.WAV", "RCALOOP3.WAV"
};

cosmictealeaf:

#include <Audio.h>

const char *mySamples[] = {""EERIE.WAV"", ""LA.WAV"", ""RCALOCK3.WAV"",
                    ""TIMESPAN.WAV"", ""BEADS.WAV"", ""RCA2.WAV"", ""RCALOCK2.WAV"", ""RCALOOP3.WAV""};

Serial.println(sizeof[mySamples[0]);



The serial returns a value of 4. This is weird, aren't characters 1 or 2 bytes. Is it possibly converting the name "EERIE.WAV" to an int?

mySampes is an array of pointer to char. Pointers are four bytes long (evidently).

What happens here is that the compiler creates 8 arrays of char, each with just enough size to hold your string. Then it builds an array of 8 pointers to char and initialises it so that each pointer points to the first char in the string. This is standard, it's how strings are done in C.

sizeof mySamples == 32;  // 8 pointers of 4 bytes each
sizeof mySamples[0] == 4;  // size of the first pointer in the list
sizeof mySamples / sizeof mySamples[0] == 8; // Number of pointers in the list

Two ==s?

CrossRoads:
Two ==s?

Yes. It indicates equality rather than an assignment. :slight_smile:

OldSteve's solution worked very well. It is interesting that I did not need to find a way to jam the quotation marks into the function in order to get it working. Thanks OldSteve!

Also thank you PaulMurray and johnwasser.I have a better grasp of how pointers work now and how the program is directed to point to the first character as a reference. Thanks all! Your answers are appreciated!