Go Down

Topic: trouble getting started with Wave Module (Read 1 time) previous topic - next topic

May 26, 2013, 03:43 pm Last Edit: May 27, 2013, 03:35 am by teacherben Reason: 1
I am a high school tech teacher and one of my students just picked up this wav shield:

(http://www.emartee.com/product/42147/Wave%20Shield%20V2.0)

We are both new to Arduino and are having trouble getting it working for her project.  It's a pretty simple project.  

She has 6 recorded sounds on the SD card and a button attached and whenever the button is pressed, she wants it to randomly select one of the sounds and play it back, then stop and wait for the next button press.  

We can get it to play 6 tracks in sequence, then stop and it seems to work OK.  But getting a button to trigger play and have it play a random track, then stop and wait is beyond us.  Here's the code to get it to play 6 tacks in sequence (based on the sample code that came with the device):

Code: [Select]
 
int RST = 3;
int CLK = 9;
int DAT = 8;

void setup() {   

  pinMode(RST, OUTPUT);
  pinMode(CLK, OUTPUT);
  pinMode(DAT, OUTPUT);

  digitalWrite(RST, HIGH);
  digitalWrite(CLK, HIGH);
  digitalWrite(DAT, HIGH);

  digitalWrite(RST, LOW);
  delay(5);
  digitalWrite(RST, HIGH);
  delay(300);

  send(0xfff4);//set voice volumn to 0 (turn off)
  delay(3000);

}

void loop() {

  send(0x0000);//play file 0000
  delay(5000);//delay 10 seconds

  send(0x0001);//play file 0001
  delay(5000);//delay 10 seconds

  send(0x0002);//play file 0002
  delay(5000);//delay 10 seconds

  send(0x0003);//play file 0003
  delay(5000);//delay 10 seconds

  send(0x0004);//play file 0004
  delay(5000);//delay 10 seconds

  send(0x0005);//play file 0005
  delay(5000);//delay 10 seconds
  /*
   send(0xfff0);//set voice volumn to 0 (turn off)   
   delay(3000);   
   send(0xfff4);//set voice volumn to 4   
   delay(3000);   
   send(0xfff7);//set voice volumn to 7   
   delay(3000);   
   send(0xfffe);// pause 
   delay(5000);
   send(0xfffe);//play
   */   
  while(1);
}

void send(int data)
{
  digitalWrite(CLK, LOW);
  delay(2);
  for (int i=15; i>=0; i--)
  {
    delayMicroseconds(50);
    if((data>>i)&0x0001 >0)
    {
      digitalWrite(DAT, HIGH);
      //Serial.print(1);
    }
    else
    {
      digitalWrite(DAT, LOW);
      // Serial.print(0);
    }
    delayMicroseconds(50);
    digitalWrite(CLK, HIGH);
    delayMicroseconds(50);

   if(i>0)
      digitalWrite(DAT, LOW);
    else
        digitalWrite(DAT, HIGH);
    delayMicroseconds(50);

    if(i>0)
      digitalWrite(CLK, LOW);
    else
        digitalWrite(CLK, HIGH);
  }
delay(20);
}



We are using an Arduino Uno for the project.  There is a button connected to pin 6.  Using her code (below), it ignores the button and randomly selects a track, plays a 5-second snippet of the track and then automatically jumps to another random track.

Code: [Select]
 

int RST = 3; //reset pin
int CLK = 9; // clock pin
int DAT = 8; //datapin


int buttonPin = 6;
int buttonState;
long ran;
int time = 5000;

void setup ()
{

  pinMode(RST, OUTPUT);
  pinMode(CLK, OUTPUT);
  pinMode(DAT, OUTPUT);

  digitalWrite(RST, HIGH);
  digitalWrite(CLK, HIGH);
  digitalWrite(DAT, HIGH);

  digitalWrite(RST, LOW);
  delay(5);
  digitalWrite(RST, HIGH);
  delay(300);

  pinMode (buttonPin, INPUT);
  randomSeed(analogRead(0));
 
}

void loop()
{
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH){
    ran = random(1, 7);
    delay(1000);
   
    if (ran == 1){
      send(0x0000);
      delay (time);
    }
    if (ran == 2){
      send(0x0001);
      delay (time);
    }
    if (ran == 3){
      send(0x0002);
      delay (time);
    }
    if (ran == 4){
      send(0x0003);
      delay (time);
    }
    if (ran == 5){
      send(0x0004);
      delay (time);
    }
    if (ran == 6){
      send(0x0005);
      delay (time);
    }
  }

}

void send(int data) // This function is to send data to this module.
{
  digitalWrite(CLK, LOW);
  delay(2);
  for (int i=15; i>=0; i--)
  {
    delayMicroseconds(50);
    if((data>>i)&0x0001 >0)
    {
      digitalWrite(DAT, HIGH);
    }
    else {
      digitalWrite(DAT, LOW);
    }
    delayMicroseconds(50);
    digitalWrite(CLK, HIGH);
    delayMicroseconds(50);
    if(i>0)
      digitalWrite(DAT, LOW);
    else
      digitalWrite(DAT, HIGH);
    delayMicroseconds(50);
    if(i>0)
      digitalWrite(CLK, LOW);
    else
      digitalWrite(CLK, HIGH);
  }
  delay(20);
}



I am continuing to get to know the Arduino to try to help her out, but I am hoping to find someone who might be familiar with this shield that could give us a few tips.  I have done a fairly exhaustive search and every reference I have found online to this shield is just copy/pasted from the others (the same as the emartee link above) and no one on Instructables, Makezine or anywhere seems to have anything that uses this module.  The only sample code I have is what I have pasted above.

Grumpy_Mike

It helps if you read the how to use this forum sticky post.

It also helps if you post the code that you are having trouble with not some slightly related variant sample code.

Thank you for taking the time to point out my error Grumpy_Mike.  I have edited the post and hopefully it is now more suitable. Once again, any suggestions or guidance you may have to offer would be appreciated. 

Grumpy_Mike

I would suggest that first you check that the button is wired up correctly and that the arduino is reading a differant value when pressed or not. Do this by using a serial print and looking at the result, I suspect this is your problem.

I checked and the button seems to be working fine.  I added an LED that would light up while the button depressed and it worked fine.  When I uncommented the rest however, the button stayed lit up all the time.

There's a 'while(1)' that I didn't quite understand in there.  When I take it out, it selects random tracks and loops over and over.  With it in there, the program opens, it randomly selects one track, then stops.  When I press the button (6) nothing happens.  When I press the play button on the wav shield, it will always play back the same track that was randomly selected.  So, I upload the sketch and run it, and it automatically starts playing one of the tracks for 5 seconds, then stops.  It I press the button I added, nothing happens.  The LED on 13 is still lit.  If I press the play button on the shield, it plays that same track again and stops.  If I reset the board, it will then select and then play another track.  When I hit play, I hear that same one again. 

Here's the code again:

Code: [Select]
 

int RST = 3; //reset pin
int CLK = 9; // clock pin
int DAT = 8; //datapin

int buttonPin = 6;
int buttonState;
long ran;
int time = 5000;

void setup(){
  pinMode(RST, OUTPUT);
  pinMode(CLK, OUTPUT);
  pinMode(DAT, OUTPUT);
 
  pinMode(buttonPin, INPUT);
  pinMode(13, OUTPUT);
 
  digitalWrite(RST, HIGH);
  digitalWrite(CLK, HIGH);
  digitalWrite(DAT, HIGH);

  digitalWrite(RST, LOW);
  delay(5);
  digitalWrite(RST, HIGH);
  delay(300);
 
  randomSeed(analogRead(0));
}

void loop(){
  buttonState = digitalRead(buttonPin);
  if(buttonState == HIGH){
    ran = random(1, 7);
    delay(1000);
   digitalWrite(13, HIGH);
   
    if (ran == 1){
      send(0x0000);
      delay (time);
    }
    if (ran == 2){
      send(0x0001);
      delay (time);
    }
    if (ran == 3){
      send(0x0002);
      delay (time);
    }
    if (ran == 4){
      send(0x0003);
      delay (time);
    }
    if (ran == 5){
      send(0x0004);
      delay (time);
    }
    if (ran == 6){
      send(0x0005);
      delay (time);
    }
   
          while(1);
   
  } else {
   digitalWrite(13, LOW);
  }

}

void send(int data) // This function is to send data to this module.
{
  digitalWrite(CLK, LOW);
  delay(2);
  for (int i=15; i>=0; i--)
  {
    delayMicroseconds(50);
    if((data>>i)&0x0001 >0)
    {
      digitalWrite(DAT, HIGH);
    }
    else {
      digitalWrite(DAT, LOW);
    }
    delayMicroseconds(50);
    digitalWrite(CLK, HIGH);
    delayMicroseconds(50);
    if(i>0)
      digitalWrite(DAT, LOW);
    else
      digitalWrite(DAT, HIGH);
    delayMicroseconds(50);
    if(i>0)
      digitalWrite(CLK, LOW);
    else
      digitalWrite(CLK, HIGH);
  }
  delay(20);
}


Grumpy_Mike

Quote
When I uncommented the rest however, the button stayed lit up all the time.

I am not sure what the "rest" is. Can you post the code that shows the button working please.

The wait simply enters an endless loop an so in effect stops the program so I am not sure why you need it.

The code that selects the file is rather turgid you seem to be just passing the random number minus one into the function. It is not the cause of your problem but it might help you see what the code is more clearly.
First of all try this or the loop function:-
Code: [Select]

void loop(){
  if(digitalRead(buttonPin) == HIGH){
   digitalWrite(13, HIGH); 
  } else {
   digitalWrite(13, LOW);
  }

}

The light should go on and off with the button.

Then try this as a loop function

Code: [Select]
void loop(){
  if(digitalRead(buttonPin) == HIGH){
    ran = random(1, 7);
    delay(1000);
   digitalWrite(13, HIGH); 
      send(ran-1);
      delay (time);
  } else {
   digitalWrite(13, LOW);
  }

}

I replaced my loop function with the simple button one that you suggested (code below).  As soon as it finishes uploading, the LED on pin 13 turns on and the wav module randomly selects one of the tracks and plays it for 5 seconds then stops and the LED turns off.  After that, the button turns the LED on and off as expected. 

Code: [Select]
int RST = 3; //reset pin
int CLK = 9; // clock pin
int DAT = 8; //datapin

int buttonPin = 6;
int buttonState;
long ran;
int time = 5000;

void setup(){
  pinMode(RST, OUTPUT);
  pinMode(CLK, OUTPUT);
  pinMode(DAT, OUTPUT);
 
  pinMode(buttonPin, INPUT);
  pinMode(13, OUTPUT);
 
  digitalWrite(RST, HIGH);
  digitalWrite(CLK, HIGH);
  digitalWrite(DAT, HIGH);

  digitalWrite(RST, LOW);
  delay(5);
  digitalWrite(RST, HIGH);
  delay(300);
 
  randomSeed(analogRead(0));
}

void loop(){
  if(digitalRead(buttonPin) == HIGH){
   digitalWrite(13, HIGH); 
  } else {
   digitalWrite(13, LOW);
  }

}

void send(int data) // This function is to send data to this module.
{
  digitalWrite(CLK, LOW);
  delay(2);
  for (int i=15; i>=0; i--)
  {
    delayMicroseconds(50);
    if((data>>i)&0x0001 >0)
    {
      digitalWrite(DAT, HIGH);
    }
    else {
      digitalWrite(DAT, LOW);
    }
    delayMicroseconds(50);
    digitalWrite(CLK, HIGH);
    delayMicroseconds(50);
    if(i>0)
      digitalWrite(DAT, LOW);
    else
      digitalWrite(DAT, HIGH);
    delayMicroseconds(50);
    if(i>0)
      digitalWrite(CLK, LOW);
    else
      digitalWrite(CLK, HIGH);
  }
  delay(20);
}


When I used the second loop function you provided, the LED on pin 13 lights up and the wave module randomly selects a track, plays it for 5 seconds, then randomly selects another track, plays it for 5 seconds and so on as it loops.  The button doesn't have much effect, although I can hear a little clicking sound in the headphones when I press it. 

Code: [Select]
int RST = 3; //reset pin
int CLK = 9; // clock pin
int DAT = 8; //datapin

int buttonPin = 6;
int buttonState;
long ran;
int time = 5000;

void setup(){
  pinMode(RST, OUTPUT);
  pinMode(CLK, OUTPUT);
  pinMode(DAT, OUTPUT);
 
  pinMode(buttonPin, INPUT);
  pinMode(13, OUTPUT);
 
  digitalWrite(RST, HIGH);
  digitalWrite(CLK, HIGH);
  digitalWrite(DAT, HIGH);

  digitalWrite(RST, LOW);
  delay(5);
  digitalWrite(RST, HIGH);
  delay(300);
 
  randomSeed(analogRead(0));
}

void loop(){
  if(digitalRead(buttonPin) == HIGH){
    ran = random(1, 7);
    delay(1000);
   digitalWrite(13, HIGH); 
      send(ran-1);
      delay (time);
  } else {
   digitalWrite(13, LOW);
  }

}

void send(int data) // This function is to send data to this module.
{
  digitalWrite(CLK, LOW);
  delay(2);
  for (int i=15; i>=0; i--)
  {
    delayMicroseconds(50);
    if((data>>i)&0x0001 >0)
    {
      digitalWrite(DAT, HIGH);
    }
    else {
      digitalWrite(DAT, LOW);
    }
    delayMicroseconds(50);
    digitalWrite(CLK, HIGH);
    delayMicroseconds(50);
    if(i>0)
      digitalWrite(DAT, LOW);
    else
      digitalWrite(DAT, HIGH);
    delayMicroseconds(50);
    if(i>0)
      digitalWrite(CLK, LOW);
    else
      digitalWrite(CLK, HIGH);
  }
  delay(20);
}


Grumpy_Mike

Quote
I replaced my loop function with the simple button one that you suggested (code below).  As soon as it finishes uploading, the LED on pin 13 turns on and the wav module randomly selects one of the tracks and plays it for 5 seconds then stops and the LED turns off.  After that, the button turns the LED on and off as expected. 

That can not be because in that first code there is no call to the wave module at all.
It might be that there is a an issue with the power up. Do you still get a random sound when you press the arduino's reset button after it is powered up.
I think you have wired the button up wrong and you have a floating input. It looks like from the code you are using the method with a pull down resistor, are you sure the value is right? Can you post a picture that shows this wiring?

Grumpy_Mike

The other thing you could try is making sure the push button is unpressed before looking for another press:-
Code: [Select]

void loop(){
  static boolean buttonNow = false;
  static boolean buttonThen = true;
  buttonNow = digitalRead(buttonPin);
  if( buttonNow == HIGH && buttonNow != buttonThen){
    ran = random(1, 7);
    delay(1000);
   digitalWrite(13, HIGH); 
      send(ran-1);
      delay (time);
  } else {
   digitalWrite(13, LOW);
  }
buttonThen = buttonNow;
}

To respond to your first question (Reply #7 in the thread) using that first block of code, even when I have nothing connected, that is, just the Arduino and the shield connected to the computer by USB and that's it, I upload the code, it randomly selects one of the tracks, plays it and then stops.  When I reset the board (the black button on the shield) it plays a randomly selected track, then stops. 

Your most recent post works!  Thanks so much Grumpy_Mike.  I get what's going on in principle.  I will try to play around a bit more so I can use it in other contexts.  Your help is really appreciated.

Grumpy_Mike

Quote
Your most recent post works!

Great.  :)

What was happening then is that you were keeping your finger on the button for longer than a second. The delay was timing out and the loop whips round and sees that your finger is still on and fires off another sound. The wave shield was being "intelligent" and storing your requests. Hence you thought that the if test was not working.

Quote
I will try to play around a bit more so I can use it in other contexts.

For an example in the Arduino IDE see the menu option:-
File -> Examples -> 02.Digital -> StateChangeDetection

worf1471

i have a arduino uno board and a arduino wave v2.1


check my youtube vid out all i get is a click noise iam using the code that was supplied with the board but to no avail i have purchased two of these and both are doing the same as you can see from vid im using a 1gb sd card which is formated to fat file

hope someone with this card can help as really struggling

Go Up