wave shield

Hey everyone just a quick question?

I've been looking for a sketch for my uno r3 with wave shield that would take the input from the rx pin like a letter D and a number after to play a wave file so D1 would play wave 1.wav and D2 would play 2.wav is this posible?

Does anyone know were i can find something close to this i could use or modify?

Thanks for your time J.Robinson AKA: Slip

Yes this is possible.

Mark

You might want to start by looking at the example “ReadASCIIString” found under File → Examples → 04. Communication.

Lots of sound file playing modules here http://www.mdfly.com/index.php?main_page=advanced_search_result&search_in_description=1&zenid=7bb148a0794958999d74eee7c07b7138&keyword=mp3&x=-310&y=-103

if (Serial.available()>1){ // 2 bytes received
byte1 = Serial.read();
if (byte1 == 'D'){
  // read number
  byte2 = Serial.read();
switch (byte2){
case '0':
// play  wave0.wav
break;
case '1':
// play wave1.wav
break;
// etc
} // end switch
} // end 'D' check
} // end serial available

This looks promissing can't Wait to try it out, was up all night trying out things close to that but I was off a bit, I will post my results today after trying it out and what my actual sketch is. thanks a lot for the help

This is my full sketch (its for my r2d2 unit that i want to play sound on.) i have 30 sounds i want to play I can put 10 in now and play them fine the arduino only picks up the first digit entered in the rx. if i put in

if (c == '22') {
           toplay = ARTOO22;
         }

it plays ARTOO2 not 22 and same for 11,12,13,14 and so on only reads first number I tried every way i could find only to take the hole number coming in witch is actually going to be $1 or $11 or $2 and so on and use it.

My sketch that plays but only first number in =

#include <FatReader.h>
    #include <SdReader.h>
    #include <avr/pgmspace.h>
    #include <WaveHC.h>
    #include <WaveUtil.h>
    #include <SoftwareSerial.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 playing

    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 ARTOO1 "1.WAV"
    #define ARTOO2 "2.WAV"
    #define ARTOO3 "3.WAV"
    #define ARTOO4 "4.WAV"
    #define ARTOO5 "5.WAV"
    #define ARTOO6 "6.WAV"
    #define ARTOO7 "7.WAV"
    #define ARTOO8 "8.WAV"
    #define ARTOO9 "9.WAV"
    #define ARTOO10 "10.WAV"
    #define ARTOO11 "11.WAV"
    #define ARTOO12 "12.WAV"
    #define ARTOO13 "13.WAV"
    #define ARTOO14 "14.WAV"
    #define ARTOO15 "15.WAV"
    #define ARTOO16 "16.WAV"
    #define ARTOO17 "17.WAV"
    #define ARTOO18 "18.WAV"
    #define ARTOO19 "19.WAV"
    #define ARTOO20 "20.WAV"
    #define ARTOO21 "21.WAV"
    #define ARTOO22 "22.WAV"
    #define ARTOO23 "23.WAV"
    #define ARTOO24 "24.WAV"
    #define ARTOO25 "25.WAV"
    #define ARTOO26 "26.WAV"
    #define ARTOO27 "27.WAV"
    #define ARTOO28 "28.WAV"
    #define ARTOO29 "29.WAV"
    #define ARTOO30 "30.WAV"


SoftwareSerial mySerial(14, 15);



   int wasplaying = 0;

   void setup() {
    Serial.begin(9600); // set up Serial library at 9600 bps
    Serial.println("r2test!");

    pinMode(2, OUTPUT);
    pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
    pinMode(5, 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
    }

    // 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!");
    }

    // 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("Ready!");

    }

   void loop() {
    char c, *toplay;
     if (Serial.available()) {
     c = Serial.read(); 
      delay(100);
       Serial.println(c);
         if (c == '1') {
           toplay = ARTOO1;
         }        
         else if (c == '2') {
           toplay = ARTOO2;
         }
         else if (c == '3') {
           toplay = ARTOO3;
         }
         else if (c == '4') {
           toplay = ARTOO4;
         }
         else if (c == '5') {
           toplay = ARTOO5;
         }
         else if (c == '6') {
           toplay = ARTOO6;
         }
         else if (c == '7') {
           toplay = ARTOO7;
         }
           else {
           return;
         }


         if (wave.isplaying) {// already playing something, so stop it!
             wave.stop(); // stop it
         }
         playfile(toplay);
       }

       wasplaying = wave.isplaying;
    }

    


    // 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) {
      // 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
      }
      // look in the root directory and open the file
      if (!f.open(root, name)) {
        putstring("Couldn't open file "); Serial.print(name); return;
      }
      // OK read the file and turn it into a wave object
      if (!wave.create(f)) {
        putstring_nl("Not a valid WAV"); return;
      }
     
      // ok time to play! start playback
      wave.play();
    }

any help would be greatly appreciated thanks.

J.Robinson

if (c == '22') {

Which ONE key on your keyboard did you press to get the [u]single[/u] character in the quotes there?

Why not expand the list thusly:
0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h, … ,x,y,z, A,B,C,D, …, X,Y,Z
Then you can have 62 unique sounds playable from 1 key.
Either that or require 2 characters for every entry

if (Serial.available() >1){  // 2 characters received?
inByte1 = Serial.read() - 0x30;  / receive number & convert from ASCII to a number 0 to 9
inByte2 = Serial.read() - 0x30;
if ( (numBer >=0) && (numBer <=99) ){ // sanity check
numBer = inByte1 * 10 + inbyte2; //  convert to 00 to 99

switch (numBer){
case 0:
toplay = ARTOO00;
break;
 // etc 
case 9:
toplay = ARTOO09;
break;
//etc
case 99:
toplay = ATROO99;
break;
} // end switch
} // end sanity check
} // end serial available check

hi pauls, the 22 comes from my ihpone i press a button and it sends $22 to the arduino I also have logic display on a pro mini with the same problem it send B22 to the pro mini I have, each sound matches a button on the iphone but for now i use the serial monitor or my iohone to test what happens when $22 or 22 is entered im very new to this and got as far as i can when ever I try to add more than 0-9 serial.read it dont work it just reads the first digit i hope that makes sense it wont read 22 it reads 2.

hi crossroads, that is perfect but i cant get it to work say not declared in this scope for inByte1 and inByte2. not sure how to properly add it.

void loop() {
    if (Serial.available() >1){  // 2 characters received?
inByte1 = Serial.read() - 0x30;  / receive number & convert from ASCII to a number 0 to 9
inByte2 = Serial.read() - 0x30;
if ( (numBer >=0) && (numBer <=99) ){ // sanity check
numBer = inByte1 * 10 + inbyte2; //  convert to 00 to 99

switch (numBer){
case 0:
toplay = ARTOO00;
break;
 // etc 
case 9:
toplay = ARTOO09;
break;
//etc
case 30:
toplay = ATROO30;
break;
} // end switch
} // end sanity check
} // end serial available check

         if (wave.isplaying) {// already playing something, so stop it!
             wave.stop(); // stop it
         }
         playfile(toplay);
       }

       wasplaying = wave.isplaying;
    }

this is what i added, but what do i add to the void setup and before that to make it work. thanks J.robinson

got the sketch to compile but dont work! at all now.

my new full code

//
    #include <FatReader.h>
    #include <SdReader.h>
    #include <avr/pgmspace.h>
    #include <WaveHC.h>
    #include <WaveUtil.h>
    #include <SoftwareSerial.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 playing

    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 ARTOO1 "1.WAV"
    #define ARTOO2 "2.WAV"
    #define ARTOO3 "3.WAV"
    #define ARTOO4 "4.WAV"
    #define ARTOO5 "5.WAV"
    #define ARTOO6 "6.WAV"
    #define ARTOO7 "7.WAV"
    #define ARTOO8 "8.WAV"
    #define ARTOO9 "9.WAV"
    #define ARTOO10 "10.WAV"
    #define ARTOO11 "11.WAV"
    #define ARTOO12 "12.WAV"
    #define ARTOO13 "13.WAV"
    #define ARTOO14 "14.WAV"
    #define ARTOO15 "15.WAV"
    #define ARTOO16 "16.WAV"
    #define ARTOO17 "17.WAV"
    #define ARTOO18 "18.WAV"
    #define ARTOO19 "19.WAV"
    #define ARTOO20 "20.WAV"
    #define ARTOO21 "21.WAV"
    #define ARTOO22 "22.WAV"
    #define ARTOO23 "23.WAV"
    #define ARTOO24 "24.WAV"
    #define ARTOO25 "25.WAV"
    #define ARTOO26 "26.WAV"
    #define ARTOO27 "27.WAV"
    #define ARTOO28 "28.WAV"
    #define ARTOO29 "29.WAV"
    #define ARTOO30 "30.WAV"
    #define ARTOO31 "31.WAV"
    #define ARTOO32 "32.WAV"
    #define ARTOO33 "33.WAV"


SoftwareSerial mySerial(14, 15);


   
   int wasplaying = 0;
   int inByte1 = 0;
   int inByte2 = 0;
   
   
   void setup() {
    Serial.begin(9600); // set up Serial library at 9600 bps
    Serial.println("r2test!");

    pinMode(2, OUTPUT);
    pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
    pinMode(5, 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
    }

    // 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!");
    }

    // 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("Ready!");

    }

   void loop() {
     char numBer, *toplay;
    if (Serial.available() >1){  // 2 characters received?
inByte1 = Serial.read() - 0x30;  //receive number & convert from ASCII to a number 0 to 9
inByte2 = Serial.read() - 0x30;
if ( (numBer >=0) && (numBer <=99) ){ // sanity check
numBer = inByte1 * 10 + inByte2; //  convert to 00 to 99
Serial.println(numBer);


switch (numBer){
case 0:
toplay = ARTOO1;
break;
 // etc 
case 9:
toplay = ARTOO9;
break;
//etc
case 99:
toplay = ARTOO11;
break;
} // end switch
} // end sanity check

         if (wave.isplaying) {// already playing something, so stop it!
             wave.stop(); // stop it
         }
         playfile(toplay);
       }

       wasplaying = wave.isplaying;
    }

    


    // 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) {
      // 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
      }
      // look in the root directory and open the file
      if (!f.open(root, name)) {
        putstring("Couldn't open file "); Serial.print(name); return;
      }
      // OK read the file and turn it into a wave object
      if (!wave.create(f)) {
        putstring_nl("Not a valid WAV"); return;
      }
     
      // ok time to play! start playback
      wave.play();
    }

errors i get typing 1 or 2, 3,4,5,6,7,8,9,0 I think it dont read as a number now.

r2test!
Using partition 1, type is FAT32
Ready!
Ý
Couldn't open file ?
Couldn't open file |
Couldn't open file û
Couldn't open file ?
Couldn't open file |
Couldn't open file 
Couldn't open file ?
Couldn't open file |
Couldn't open file #
Couldn't open file ?
Couldn't open file |
Couldn't open file 7
Couldn't open file ?
Couldn't open file |
Couldn't open file 
inByte1 = Serial.read() - 0x30;  / receive number & convert from ASCII to a number 0 to 9
inByte2 = Serial.read() - 0x30;

I think this would be more obvious as:

inByte1 = Serial.read() - '0';  / receive number & convert from ASCII to a number 0 to 9
inByte2 = Serial.read() - '0';

The problem with this approach is that you MUST send two digit numbers. 0 -> 00; 4 -> 04

If you don't want to do that, you need to send some kind of delimiter after the number, so that the Arduino can tell that the end of the packet has arrived. A comma, a carriage return, a linefeed, or some other non-digit delimiter would be useful.

I'm sorry I don't under stand . Are u saying I need to remove the 0x30 and put in inBite1 = serial.read () >00 - 4 - >04;

Sorry, slipnotz, I figured you knew your way around coding some. The code obviously needs some filling in - I only wrote 2 of the case statements for playing files so you could see what is needed.

Yes sorry crossroads I'm doing the best I can from the reference. Allthough I got the sketch made and the serial to read and play 9 files I can't get any farther I'm not smart enough is there any sketch examples you would know of that do what or close to what I'm doing so I could see what the code looks like.

Question? Are you saying in the last post that I need one serial read command for each byte I want to receive then add them bytes to give me a number. Or am I just leaving something out of my code that allows it to read all of the strong coming in.

If you're going to read in two bytes, then you need two serial.read's. Will all the numbers come in like this: $01 $02 $03 ... $09 $10 $11 ... $29 $30 $31 ? If so, you can look for the $ to start:

if (serial.available() >2){
byte1 = Serial.read();
if (byte1 == '

){  // or 'B' I think you mentioned // keep reading byte2 = Serial.read(); byte3 = Serial.read(); // convert from ascii to numbers byte2 = byte2 - 0x30; // see www.asciitable.com byte3 = byte3 - 0x30; // put the numbers together combinedByte = (byte2 * 10) + byte3; // now play the song ... } // end byte1 check } // end serial.available check ```

thank you for your help this is exacly what i need and yes it will be the $ i just used b as an example sorry, this looks great im going to play with it for a bit and see what i come out with need to learn a little more about the scope get allot of not declared when ever i put new code in the void loop.

ok Ive completed changing what i believe needed to be changed and this is what i have:

   int inByte1 = 0;
   int inByte2 = 0;
   int inByte3 = 0;

void loop() {
     char combinedByte, *toplay;
    if (Serial.available() >2){
inByte1 = Serial.read();
if (inByte1 == '

And let me tell you it runs beautiful! woot you rock man. I hope i did good for my first real sketch probably not, lmao.

I will post a video of it working on youtube for you to see thanks agian and if you have time can you tell me what this means Couldn't open file @° the file plays but it still tells me this in the monitor.){  // or 'B' I think you mentioned // keep reading inByte2 = Serial.read(); inByte3 = Serial.read(); // convert from ascii to numbers inByte2 = inByte2 - 0x30; // see www.asciitable.com inByte3 = inByte3 - 0x30; // put the numbers together combinedByte = (inByte2 * 10) + inByte3; Serial.println(combinedByte); // now play the song switch (combinedByte){ case 2: toplay = ARTOO1; break; // etc case 20: toplay = ARTOO2; break; //etc case 21: toplay = ARTOO3; break; } // end byte1 check } // end serial.available check ```

And let me tell you it runs beautiful! woot you rock man. I hope i did good for my first real sketch probably not, lmao.

I will post a video of it working on youtube for you to see thanks agian and if you have time can you tell me what this means Couldn't open file @° the file plays but it still tells me this in the monitor.

I don't know what this means "Couldn't open file @° "

I now have full logic and sound control over Serial. rx1 in on my promini and uno r3 I want to thank CrossRoads for his help with out it I would not have this completed and i would still be messing around. bellow is a link to my video of it working.