playSdWav in for loop

Status
Not open for further replies.
Hi all,

Is it possible to use playSdWav in a for loop?

Something like this:

playSdWav.play(".wav"); // this doesn't work of course.

How can I use this function in an iterative for loop?

Thanks in advance!
 
First to directly answer your question: yes.

But honestly, I really don't understand what you want to accomplish. Maybe you could explain with a bit more context. We're pretty good at helping on this forum, but like all humans, we need to actually understand to do anything more than not-so-useful answers.
 
First to directly answer your question: yes.

But honestly, I really don't understand what you want to accomplish. Maybe you could explain with a bit more context. We're pretty good at helping on this forum, but like all humans, we need to actually understand to do anything more than not-so-useful answers.

Hi Paul thanks for qucik response!

I have 6 LDR's on my Analog inputs, triggering 6 different samples I have already uploaded to my sd card.

Here is my entire code:

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

// GUItool: begin automatically generated code
AudioPlaySdWav playSdWav3; //xy=83,262
AudioPlaySdWav playSdWav4; //xy=104,335
AudioPlaySdWav playSdWav2; //xy=132,192
AudioPlaySdWav playSdWav5; //xy=159,414
AudioPlaySdWav playSdWav1; //xy=176,107
AudioPlaySdWav playSdWav6; //xy=227,497
AudioMixer4 mixer1; //xy=519,105
AudioMixer4 mixer3; //xy=522,177
AudioMixer4 mixer2; //xy=524,342
AudioMixer4 mixer4; //xy=527,416
AudioMixer4 mixer5; //xy=753,194
AudioMixer4 mixer6; //xy=756,349
AudioOutputI2S i2s1; //xy=941,263
AudioConnection patchCord1(playSdWav3, 0, mixer1, 2);
AudioConnection patchCord2(playSdWav3, 1, mixer2, 2);
AudioConnection patchCord3(playSdWav4, 0, mixer3, 0);
AudioConnection patchCord4(playSdWav4, 1, mixer4, 0);
AudioConnection patchCord5(playSdWav2, 0, mixer1, 1);
AudioConnection patchCord6(playSdWav2, 1, mixer2, 1);
AudioConnection patchCord7(playSdWav5, 0, mixer3, 1);
AudioConnection patchCord8(playSdWav5, 1, mixer4, 1);
AudioConnection patchCord9(playSdWav1, 0, mixer1, 0);
AudioConnection patchCord10(playSdWav1, 1, mixer2, 0);
AudioConnection patchCord11(playSdWav6, 0, mixer3, 2);
AudioConnection patchCord12(playSdWav6, 1, mixer4, 2);
AudioConnection patchCord13(mixer1, 0, mixer5, 0);
AudioConnection patchCord14(mixer3, 0, mixer5, 1);
AudioConnection patchCord15(mixer2, 0, mixer6, 0);
AudioConnection patchCord16(mixer4, 0, mixer6, 1);
AudioConnection patchCord17(mixer5, 0, i2s1, 0);
AudioConnection patchCord18(mixer6, 0, i2s1, 1);
AudioControlSGTL5000 sgtl5000_1; //xy=604,760
// GUItool: end automatically generated code


#define SDCARD_CS_PIN 10
#define SDCARD_MOSI_PIN 7
#define SDCARD_SCK_PIN 14

const int ldrpins[] = {17, 18, 19, 20, 21, 22};
int ldr_pre[] = {0, 0, 0, 0, 0, 0};
int ldr_now[] = {0, 0, 0, 0, 0, 0};
bool ldr_state[] = {0, 0, 0, 0, 0, 0};
int treshold = 80;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
AudioMemory(100);
sgtl5000_1.enable();
sgtl5000_1.volume(0.5);
SPI.setMOSI(SDCARD_MOSI_PIN);
SPI.setSCK(SDCARD_SCK_PIN);
if (!(SD.begin(SDCARD_CS_PIN))) {
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
//Left Channels
mixer1.gain(0, 0.15);
mixer1.gain(1, 0.15);
mixer3.gain(0, 0.15);
mixer3.gain(1, 0.15);
mixer5.gain(0, 0.15);
mixer5.gain(1, 0.15);
//Right Channels
mixer2.gain(0, 0.15);
mixer2.gain(1, 0.15);
mixer4.gain(0, 0.15);
mixer4.gain(1, 0.15);
mixer6.gain(0, 0.15);
mixer6.gain(1, 0.15);
delay(1000);
}

void loop() {
// Read and check if LDRs is covered or not
for (int i = 0; i < 6; i++) {
ldr_now = analogRead(ldrpins);

if (ldr_now[0]-ldr_pre[0] > treshold) {
ldr_pre[0] = ldr_now[0];
Serial.print("LDR1 STATE: ");
Serial.print(ldr_now[0]);
Serial.println("Start playing");
playSdWav.play(".wav");
delay(10); // wait for library to parse WAV info
};

if (ldr_pre[0]-ldr_now[0] > treshold) {
ldr_pre[0] = ldr_now[0];
Serial.print("LDR1 STATE: ");
Serial.print(ldr_now[0]);
Serial.println("Stop playing");
playSdWav[i+1].stop();
};
}

}
 
When the is inside a quoted string it has no effect as an array.
If the wav files are numbered 0.wav, ..., 5.wav you could do something like this:
Code:
char *wavname[6] = {"0.wav","1.wav", ... ,"5.wav"};

.
.
   playSdWav[i].play(wavname[i]);
which would allow you to have arbitrary names, provided of course that they are 8.3 formatted.

or you could generate the names like this:
Code:
// Make this at least long enough to hold the longest string name plus one for the NULL
char str_temp[10];
.
.
  sprintf(str_temp,"%d.wav",i);
  playSdWav[i].play(str_temp);

Pete
 
@el_supremo thats a good point thanks for the suggestion! but this only resolves the sample selection part, how about the player selection part? playSdWav doesn't do it since the players are declared as their names.
 
I think this is what you want but haven't tested it:
Code:
AudioPlaySdWav arr_SdWav[6] = {
	&playSdWav1, &playSdWav2, &playSdWav3,
	&playSdWav4, &playSdWav5, &playSdWav6
};
.
.
	*arr_SdWav[i].play(wavname[i]);

Pete
 
Thanks for help Pete!
I will try to implement your solution into my sketch.
I'm a newbie slow programmer. Trying to figure out some other issues too. It takes some time:)
I will keep posted if I achieve it.
 
Or try it like this (adding one * character), if that doesn't work.

Code:
AudioPlaySdWav *arr_SdWav[6] = {
	&playSdWav1, &playSdWav2, &playSdWav3,
	&playSdWav4, &playSdWav5, &playSdWav6
};
 
Hi again, I'm do it as you described but I couldn't manage to make it work..

You can see my sketch and the error below:

Code:
SoundLAB_Sensor_Test_4:114: error: request for member 'play' in 'arr_SdWav[i]', which is of pointer type 'AudioPlaySdWav*' (maybe you meant to use '->' ?)
 *arr_SdWav[i].play(wavname[i]);
               ^
SoundLAB_Sensor_Test_4:123: error: request for member 'stop' in 'arr_SdWav[i]', which is of pointer type 'AudioPlaySdWav*' (maybe you meant to use '->' ?)
 *arr_SdWav[i].stop;
               ^
Multiple libraries were found for "SD.h"
 Used: /Applications/Arduino.app/Contents/Java/hardware/teensy/avr/libraries/SD
 Not used: /Applications/Arduino.app/Contents/Java/libraries/SD
request for member 'play' in 'arr_SdWav[i]', which is of pointer type 'AudioPlaySdWav*' (maybe you meant to use '->' ?)

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

// GUItool: begin automatically generated code

AudioPlaySdWav           playSdWav3;     //xy=83,262
AudioPlaySdWav           playSdWav4;     //xy=104,335
AudioPlaySdWav           playSdWav2;     //xy=132,192
AudioPlaySdWav           playSdWav5;     //xy=159,414
AudioPlaySdWav           playSdWav1;     //xy=176,107
AudioPlaySdWav           playSdWav6;     //xy=227,497
AudioMixer4              mixer1;         //xy=519,105
AudioMixer4              mixer3;         //xy=522,177
AudioMixer4              mixer2;         //xy=524,342
AudioMixer4              mixer4;         //xy=527,416
AudioMixer4              mixer5;         //xy=753,194
AudioMixer4              mixer6;         //xy=756,349
AudioOutputI2S           i2s1;           //xy=941,263
AudioConnection          patchCord1(playSdWav3, 0, mixer1, 2);
AudioConnection          patchCord2(playSdWav3, 1, mixer2, 2);
AudioConnection          patchCord3(playSdWav4, 0, mixer3, 0);
AudioConnection          patchCord4(playSdWav4, 1, mixer4, 0);
AudioConnection          patchCord5(playSdWav2, 0, mixer1, 1);
AudioConnection          patchCord6(playSdWav2, 1, mixer2, 1);
AudioConnection          patchCord7(playSdWav5, 0, mixer3, 1);
AudioConnection          patchCord8(playSdWav5, 1, mixer4, 1);
AudioConnection          patchCord9(playSdWav1, 0, mixer1, 0);
AudioConnection          patchCord10(playSdWav1, 1, mixer2, 0);
AudioConnection          patchCord11(playSdWav6, 0, mixer3, 2);
AudioConnection          patchCord12(playSdWav6, 1, mixer4, 2);
AudioConnection          patchCord13(mixer1, 0, mixer5, 0);
AudioConnection          patchCord14(mixer3, 0, mixer5, 1);
AudioConnection          patchCord15(mixer2, 0, mixer6, 0);
AudioConnection          patchCord16(mixer4, 0, mixer6, 1);
AudioConnection          patchCord17(mixer5, 0, i2s1, 0);
AudioConnection          patchCord18(mixer6, 0, i2s1, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=604,760
// GUItool: end automatically generated code


#define SDCARD_CS_PIN    10
#define SDCARD_MOSI_PIN  7
#define SDCARD_SCK_PIN   14
          AudioPlaySdWav *arr_SdWav[6] = {
  &playSdWav1, &playSdWav2, &playSdWav3,
  &playSdWav4, &playSdWav5, &playSdWav6
};

char *wavname[6] = {"0.wav","1.wav", "2.wav", "3.wav", "4.wav" ,"5.wav"};
int numSensors = 6;
const int ldrpins[] = {15, 16, 17, 18, 19, 22};
int ldr_pre[] = {0, 0, 0, 0, 0, 0}; 
int ldr_now[] = {0, 0, 0, 0, 0, 0};
//bool ldr_state[] = {0, 0, 0, 0, 0, 0};
int treshold = 80;
int ledPins[] = {
  0,
  1,
  2,
  3,
  4,
  5,
};

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
initLEDs();
  AudioMemory(100);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.5);
  SPI.setMOSI(SDCARD_MOSI_PIN);
  SPI.setSCK(SDCARD_SCK_PIN);
  if (!(SD.begin(SDCARD_CS_PIN))) {
    while (1) {
      Serial.println("Unable to access the SD card");
      delay(500);
    }
  }
  //Left Channels
  mixer1.gain(0, 0.15);
  mixer1.gain(1, 0.15);
  mixer3.gain(0, 0.15);
  mixer3.gain(1, 0.15);
  mixer5.gain(0, 0.15);
  mixer5.gain(1, 0.15);
  //Right Channels
  mixer2.gain(0, 0.15);
  mixer2.gain(1, 0.15);
  mixer4.gain(0, 0.15);
  mixer4.gain(1, 0.15); 
  mixer6.gain(0, 0.15);
  mixer6.gain(1, 0.15);
  delay(1000);

}

void loop() {
  // Read and check if LDRs is covered or not
  showLED();
  //printsection();
for (int i = 0; i < 6; i++) {    
     ldr_now[i] = analogRead(ldrpins[i]);

    if (ldr_now[0]-ldr_pre[0] > treshold) {
        ldr_pre[0] = ldr_now[0]; 
        Serial.print("LDR1 STATE:  ");  
        Serial.print(ldr_now[0]);
        Serial.println("Start playing");

*arr_SdWav[i].play(wavname[i]);
        delay(10); // wait for library to parse WAV info   
    };
  
    if (ldr_pre[0]-ldr_now[0] > treshold) {
         ldr_pre[0] = ldr_now[0];
        Serial.print("LDR1 STATE:  ");  
        Serial.print(ldr_now[0]);
         Serial.println("Stop playing");
*arr_SdWav[i].stop;
         
    };
}

} 

void initLEDs() {
  //set all LED pins to output
  for (int i = 0; i < numSensors; i ++) {
    pinMode(int(ledPins[i]), OUTPUT);
  }
}
void showLED() {

  //set all LED pins to output
  for (int i = 0; i < numSensors; i ++) {
    bool LEDstate = false;
    if (ldr_now[i] < 80) {
      LEDstate = true;
    }
    digitalWrite(int(ledPins[i]), LEDstate);
  }


}
 
Here's a fix for the syntax errors. Had to comment out the calls to missing functions.

In Arduino, type CTRL-T (or click Tools > Auto Format) to make your code easier to read.


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

// GUItool: begin automatically generated code

AudioPlaySdWav           playSdWav3;     //xy=83,262
AudioPlaySdWav           playSdWav4;     //xy=104,335
AudioPlaySdWav           playSdWav2;     //xy=132,192
AudioPlaySdWav           playSdWav5;     //xy=159,414
AudioPlaySdWav           playSdWav1;     //xy=176,107
AudioPlaySdWav           playSdWav6;     //xy=227,497
AudioMixer4              mixer1;         //xy=519,105
AudioMixer4              mixer3;         //xy=522,177
AudioMixer4              mixer2;         //xy=524,342
AudioMixer4              mixer4;         //xy=527,416
AudioMixer4              mixer5;         //xy=753,194
AudioMixer4              mixer6;         //xy=756,349
AudioOutputI2S           i2s1;           //xy=941,263
AudioConnection          patchCord1(playSdWav3, 0, mixer1, 2);
AudioConnection          patchCord2(playSdWav3, 1, mixer2, 2);
AudioConnection          patchCord3(playSdWav4, 0, mixer3, 0);
AudioConnection          patchCord4(playSdWav4, 1, mixer4, 0);
AudioConnection          patchCord5(playSdWav2, 0, mixer1, 1);
AudioConnection          patchCord6(playSdWav2, 1, mixer2, 1);
AudioConnection          patchCord7(playSdWav5, 0, mixer3, 1);
AudioConnection          patchCord8(playSdWav5, 1, mixer4, 1);
AudioConnection          patchCord9(playSdWav1, 0, mixer1, 0);
AudioConnection          patchCord10(playSdWav1, 1, mixer2, 0);
AudioConnection          patchCord11(playSdWav6, 0, mixer3, 2);
AudioConnection          patchCord12(playSdWav6, 1, mixer4, 2);
AudioConnection          patchCord13(mixer1, 0, mixer5, 0);
AudioConnection          patchCord14(mixer3, 0, mixer5, 1);
AudioConnection          patchCord15(mixer2, 0, mixer6, 0);
AudioConnection          patchCord16(mixer4, 0, mixer6, 1);
AudioConnection          patchCord17(mixer5, 0, i2s1, 0);
AudioConnection          patchCord18(mixer6, 0, i2s1, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=604,760
// GUItool: end automatically generated code


#define SDCARD_CS_PIN    10
#define SDCARD_MOSI_PIN  7
#define SDCARD_SCK_PIN   14
AudioPlaySdWav *arr_SdWav[6] = {
  &playSdWav1, &playSdWav2, &playSdWav3,
  &playSdWav4, &playSdWav5, &playSdWav6
};

char *wavname[6] = {"0.wav", "1.wav", "2.wav", "3.wav", "4.wav" , "5.wav"};
int numSensors = 6;
const int ldrpins[] = {15, 16, 17, 18, 19, 22};
int ldr_pre[] = {0, 0, 0, 0, 0, 0};
int ldr_now[] = {0, 0, 0, 0, 0, 0};
//bool ldr_state[] = {0, 0, 0, 0, 0, 0};
int treshold = 80;
int ledPins[] = {
  0,
  1,
  2,
  3,
  4,
  5,
};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  //initLEDs();
  AudioMemory(100);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.5);
  SPI.setMOSI(SDCARD_MOSI_PIN);
  SPI.setSCK(SDCARD_SCK_PIN);
  if (!(SD.begin(SDCARD_CS_PIN))) {
    while (1) {
      Serial.println("Unable to access the SD card");
      delay(500);
    }
  }
  //Left Channels
  mixer1.gain(0, 0.15);
  mixer1.gain(1, 0.15);
  mixer3.gain(0, 0.15);
  mixer3.gain(1, 0.15);
  mixer5.gain(0, 0.15);
  mixer5.gain(1, 0.15);
  //Right Channels
  mixer2.gain(0, 0.15);
  mixer2.gain(1, 0.15);
  mixer4.gain(0, 0.15);
  mixer4.gain(1, 0.15);
  mixer6.gain(0, 0.15);
  mixer6.gain(1, 0.15);
  delay(1000);

}

void loop() {
  // Read and check if LDRs is covered or not
  //showLED();
  //printsection();
  for (int i = 0; i < 6; i++) {
    ldr_now[i] = analogRead(ldrpins[i]);

    if (ldr_now[0] - ldr_pre[0] > treshold) {
      ldr_pre[0] = ldr_now[0];
      Serial.print("LDR1 STATE:  ");
      Serial.print(ldr_now[0]);
      Serial.println("Start playing");

      arr_SdWav[i]->play(wavname[i]);
      delay(10); // wait for library to parse WAV info
    }

    if (ldr_pre[0] - ldr_now[0] > treshold) {
      ldr_pre[0] = ldr_now[0];
      Serial.print("LDR1 STATE:  ");
      Serial.print(ldr_now[0]);
      Serial.println("Stop playing");
      arr_SdWav[i]->stop();

    }
  }
}
 
Status
Not open for further replies.
Back
Top