Go Down

Topic: Triggering Wave shield to play different .WAV files. (Read 543 times) previous topic - next topic

ojr0215

I have worked on this project, and I am a straight newbie does not have much of coding background.


GOALS : Triggering Wave shield by the volume of area to play different .WAV files.

I have a MIC-AMP max4466 which has Analog pin , gnd, 3v pin.


I have a Wave shield from Adafurit


I have a Arduino Uno.

So far what I have done.
-playing WAV files from WaveHC example's dapch example - successful
-measuring Volume level through mixing someone's (https://gist.github.com/bkeating/1d8dda2bbbdb48621ec7#file-sound_sensor-ino-L2) + (https://learn.adafruit.com/adafruit-microphone-amplifier-breakout/measuring-sound-levels) = learned how to set a threshold values to trigger LEDs to light up with certain volume.


Now, I have to combine these two together, and code it.

But, I am stuck at this phase that adding two different codes does not really work.


Here is my logics


1. 1. read the WaveShield settings, and read wav files from the SD Card.
2. play  'NOTRAINN.WAV' from the sd card,
3.  read Analog output's value through serial monitor,
4. Set a threshold values, and if the input goes over the thresvalue, play different WAV files which is loaded in the sd card.
5. if the volume goes back to normal, play 'NOTRAINN.WAV' from the sd card. from step 1
6. loop it.

Code: [Select]

#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"

SdReader card;    // This object holds the information for the card
FatVolume vol;    // This holds the information for the partition on the card
FatReader root;   // This holds the information for the filesystem on the card
FatReader f;      // This holds the information for the file we're play
WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time
int threshold = 1; //Change This has to be higher than 1
int volts;
const int sampleWindow = 80; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;
int Analoginput = A0;
int timer = 143;           // The higher the number, the slower the timing.
char ledPins[] = { 6, 7, 8, 9, 15, 16, 17 };       // an array of pin numbers to which LEDs are attached
int pinCount = 7;           // the number of pins (i.e. the length of the array)
int val = 0;
int thisPin;

// this handy function will return the number of bytes currently free in RAM, great for debugging!   
int freeRam(void)
{
  extern int  __bss_end;
  extern int  *__brkval;
  int free_memory;
  if((int)__brkval == 0) {
    free_memory = ((int)&free_memory) - ((int)&__bss_end);
  }

  else {
    free_memory = ((int)&free_memory) - ((int)__brkval);
  }
  return free_memory;
}
void sdErrorCheck(void)
{
  if (!card.errorCode()) return;
  putstring("\n\rSD I/O error: ");
  Serial.print(card.errorCode(), HEX);
putstring(", ");
 Serial.println(card.errorData(), HEX);
  while(1);
}

void setup() {
  //byte i;
  // set up serial port
  Serial.begin(9600);
  putstring("Free RAM: ");       // This can help with debugging, running out of RAM is bad
  Serial.println(freeRam());      // if this is under 150 bytes it may spell trouble!
  // Set the output pins for the DAC control. This pins are defined in the library
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  //pinMode(led, OUTPUT);
  // pin13 LED
  //pinMode(13, OUTPUT);
     int thisPin;


  // the array elements are numbered from 0 to (pinCount - 1).


  // use a for loop to initialize each pin as an output:


  for (int thisPin = 0; thisPin < pinCount; thisPin++)  {


    pinMode(ledPins[thisPin], OUTPUT);     


  }
  //  if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you


  if (!card.init()) {         //play with 8 MHz spi (default faster!) 


    putstring_nl("Card init. failed!");  // Something went wrong, lets print out why


    sdErrorCheck();


    while(1);                            // then 'halt' - do nothing!


  }
// enable optimize read - some cards may timeout. Disable if you're having problems
card.partialBlockRead(true);
// Now we will look for a FAT partition!
  uint8_t part;


  for (part = 0; part < 5; part++) {     // we have up to 5 slots to look in


    if (vol.init(card, part))


      break;                             // we found one, lets bail


  }


  if (part == 5) {                       // if we ended up not finding one  :(


    putstring_nl("No valid FAT partition!");


    sdErrorCheck();      // Something went wrong, lets print out why


    while(1);                            // then 'halt' - do nothing!


  }
  // Lets tell the user about what we found


  putstring("Using partition ");


  Serial.print(part, DEC);


  putstring(", type is FAT");


  Serial.println(vol.fatType(),DEC);     // FAT16 or FAT32?

  // Try to open the root directory


  if (!root.openRoot(vol)) {


    putstring_nl("Can't open root dir!"); // Something went wrong,


    while(1);                             // then 'halt' - do nothing!


  }

   // Whew! We got past the tough parts.


  putstring_nl("Files found (* = fragmented):");
  // Print out all of the files in all the directories.


  root.ls(LS_R | LS_FLAG_FRAGMENTED);
}

void loop() {


}
// Plays a full file from beginning to end with no pause.
void playcomplete(char *name) {
// call our helper to find and play this name
playfile(name);
 while (wave.isplaying) {
      for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);   
    delay(timer);                 
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);   
  }
  // loop from the highest pin to the lowest:
  for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);
  }
// wave.stop();
    }
  // do nothing while its playing
  }
  // now its done playing
void playfile(char *name) {
  // see if the wave object is currently doing something
  if (wave.isplaying) {// already playing something, so stop it!
    wave.stop(); // stop it
[\code]
I am so stuck. I know that I should start from the baby steps, but I do not have any coding experiences that I can use. I've worked on this project for a month so far, and I am so ready to move on.
please
Help me Guys.
:( thanks! :)
[/color]

slipstick

In order to get it to work you will need some code in loop() that does something. So far everything is in setup() and that only runs once at startup. Oh and you will need a program which compiles and yours doesn't now.

If I understand you correctly you are trying to combine two sketches each of which you have working separately. If that's right it might be a good idea to post both of them (using </> code tags) so we know what you're starting from.

Steve

ojr0215

Hi! How are you guys doing?
I really have to figure this out
"how the Arduino identify two different sounds ,And how to make it play certain ones during certain instance."
 
------------------------------------------------
1. 1. read the WaveShield settings, and read wav files from the SD Card.
2. play  'NOTRAINN.WAV' from the sd card,
3.  read Analog output's value through serial monitor,
4. Set a threshold values, and if the input goes over the thresvalue, play different WAV files which is loaded in the sd card.
5. if the volume goes back to normal, play 'Train.WAV' from the sd card. from step 1
6. loop it.
----------------------------------------------------------------

I tried, but I cannot fix this to work.
I need a desperate help.




Code: [Select]
//SOUND SENSOR PRESETUP
int led = 13;
int threshold = 1;
int volts;
const int sampleWindow = 50;
unsigned int sample;

//SD WAV SHIELD PRESETUP
#include <WaveHC.h>
#include <WaveUtil.h>

SdReader card;    // This object holds the information for the card
FatVolume vol;    // This holds the information for the partition on the card
FatReader root;   // This holds the information for the volumes root directory
WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time


uint8_t dirLevel; // indent level for file/dir names    (for prettyprinting)
dir_t dirBuf;     // buffer for directory reads

#define error(msg) error_P(PSTR(msg))

// Function definitions (we define them here, but the code is below)
void play(FatReader &dir);

//-----------------------------------
void setup() {
  Serial.begin(9600);

  //SD WAW SHEILD SETUP
  putstring_nl("\nWave test!");  // say we woke up!
  putstring("Free RAM: ");
  Serial.println(FreeRam());

 if (!card.init()) {         //play with 8 MHz spi (default faster!) 
    error("Card init. failed!");  // Something went wrong, lets print out why
  }

  card.partialBlockRead(true);

  //SD WAV SHIELD FAT PARTITION
  uint8_t part;
for (part = 0; part < 5; part++) {   // we have up to 5 slots to look in
    if (vol.init(card, part))
    break;
  }
   if (part == 5) {                     // if we ended up not finding one  :(
    error("No valid FAT partition!");  // Something went wrong, lets print out why
  }
 

  //FAT partition Complete!
  putstring("Using partition ");
  Serial.print(part, DEC);
  putstring(", type is FAT");
  Serial.println(vol.fatType(), DEC);     // FAT16 or FAT32?
 
  if (!root.openRoot(vol)) {
    error("Can't open root dir!");      // Something went wrong,
  }
 
  putstring_nl("Files found (* = fragmented):");
  root.ls(LS_R | LS_FLAG_FRAGMENTED);
  //-----------------------------------------
    //Testing using the Sound Sensor
    pinMode(led, OUTPUT);
}
//---------------------------------------------------

void loop() {
  root.rewind();
  play(root);

  unsigned long startMillis=millis();
  unsigned int peakToPeak = 0;
  unsigned int singalMax = 0;
  unsigned int singalMin = 1024;

  while (miilis() - startMillis < samplewindow)
  {
    sample = analogRead(0);
    if (sample < 1024)
    {
      if (smaple > signalMax)
    }
    else if (sample < signalMin)
    {
      signalMin = sample;
    }
  }
}
   peakToPeak = signalMax - signalMin;  // max - min = peak-peak amplitude
   double volts = (peakToPeak * 7.0) / 1024;  // convert to volts
Serial.println(volts);

//conditional
if(volts>=threshold){
  digitalWrite(led, HIGH);
  wave.Stop()
  delay(1)
  wave.play("TRAIN.WAV");
}
else{
  digitalWrite(led,LOW);
  wave.stop()
  delay(1)
  wave.play("NOTRAIN.WAV");
}



Error Codes
Code: [Select]
Arduino: 1.8.5 (Mac OS X), Board: "Arduino/Genuino Uno"

/Users/Ryah/Documents/Arduino/PLEASE/PLEASE.ino: In function 'void setup()':
PLEASE:21: error: 'error_P' was not declared in this scope
 #define error(msg) error_P(PSTR(msg))
                                     ^
/Users/Ryah/Documents/Arduino/PLEASE/PLEASE.ino:36:5: note: in expansion of macro 'error'
     error("Card init. failed!");  // Something went wrong, lets print out why
     ^
PLEASE:21: error: 'error_P' was not declared in this scope
 #define error(msg) error_P(PSTR(msg))
                                     ^
/Users/Ryah/Documents/Arduino/PLEASE/PLEASE.ino:48:5: note: in expansion of macro 'error'
     error("No valid FAT partition!");  // Something went wrong, lets print out why
     ^
PLEASE:21: error: 'error_P' was not declared in this scope
 #define error(msg) error_P(PSTR(msg))
                                     ^
/Users/Ryah/Documents/Arduino/PLEASE/PLEASE.ino:59:5: note: in expansion of macro 'error'
     error("Can't open root dir!");      // Something went wrong,
     ^
/Users/Ryah/Documents/Arduino/PLEASE/PLEASE.ino: In function 'void loop()':
PLEASE:79: error: 'miilis' was not declared in this scope
   while (miilis() - startMillis < samplewindow)
                 ^
PLEASE:79: error: 'samplewindow' was not declared in this scope
   while (miilis() - startMillis < samplewindow)
                                   ^
PLEASE:84: error: 'smaple' was not declared in this scope
       if (smaple > signalMax)
           ^
PLEASE:84: error: 'signalMax' was not declared in this scope
       if (smaple > signalMax)
                    ^
PLEASE:85: error: expected primary-expression before '}' token
     }
     ^
PLEASE:86: error: 'signalMin' was not declared in this scope
     else if (sample < signalMin)
                       ^
/Users/Ryah/Documents/Arduino/PLEASE/PLEASE.ino: At global scope:
PLEASE:92: error: 'peakToPeak' does not name a type
    peakToPeak = signalMax - signalMin;  // max - min = peak-peak amplitude
    ^
PLEASE:93: error: conflicting declaration 'double volts'
    double volts = (peakToPeak * 7.0) / 1024;  // convert to volts
           ^
/Users/Ryah/Documents/Arduino/PLEASE/PLEASE.ino:4:5: note: previous declaration as 'int volts'
 int volts;
     ^
PLEASE:93: error: 'peakToPeak' was not declared in this scope
    double volts = (peakToPeak * 7.0) / 1024;  // convert to volts
                    ^
PLEASE:94: error: 'Serial' does not name a type
 Serial.println(volts);
 ^
PLEASE:97: error: expected unqualified-id before 'if'
 if(volts>=threshold){
 ^
PLEASE:103: error: expected unqualified-id before 'else'
 else{
 ^
exit status 1
'error_P' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Robin2

If it is urgent wouldn't it have been a good idea to take a few minutes to figure out the proper section of the Forum before posting your question.

And I don't like it when people say their problem is urgent. It seems as if you expect to get an answer before someone else who is more patient.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

ojr0215

My apologies, I kinda knew that someones would reply me back in this way.

But, I had to do this. Somebody really generous might feel bad for my urgency and help me out..?

R.

Coding Badly


@ojr0215, do not cross-post.  Threads merged.


Grumpy_Mike

Quote
how the Arduino identify two different sounds
What sort of sound? Normally this sort of thing is impossible on the Arduino because it has so little memory.

Go Up