🐍 CircuitPython is the easiest way to program microcontrollers now celebrating over 200 boards!🐍
0

triggering sounds
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

triggering sounds

by cippo65 on Thu Jun 05, 2008 6:28 pm

I got the WAV shield and after some problems....due to some missed solder points...I got it working ! Now...is there some code so that I can use to address specific sound files. So, when a "button" is pressed, such and such file is played and so on ( up to as many sounds as possible...)

Thanks !
Christopher
cippo65
 
Posts: 4
Joined: Thu Jun 05, 2008 6:21 pm
Location: chicago

by adafruit on Fri Jun 06, 2008 12:05 am

there is no such example code yet...

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

by ebs on Wed Jun 11, 2008 4:50 pm

cippo65,

Here's my code that plays different sounds when you ground one of the analog input pins (0 - 5).

It uses a modified version of the Arduino example switch debounce code.
"check_switches()" return the values 1 through 6 for pins 0 through 5.
A value of 0 is returned if no pin is grounded.

Pin 0 plays "SOUND1.WAV", pin 1 plays "SOUND2.WAV", etc., up to pin 5 which plays "SOUND6.WAV".
Code: Select all | TOGGLE FULL SIZE
#include <AF_Wave.h>
#include <avr/pgmspace.h>
#include "util.h"
#include "wave.h"

#define DEBOUNCE 100

#define swPin 14

AF_Wave card;
File f;
Wavefile wave;

void setup() {
  // set up serial port
  Serial.begin(9600);

  // set up waveshield pins
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
 
  // enable pull-up resistors on
  // switch pins (analog inputs)
  digitalWrite(14, HIGH);
  digitalWrite(15, HIGH);
  digitalWrite(16, HIGH);
  digitalWrite(17, HIGH);
  digitalWrite(18, HIGH);
  digitalWrite(19, HIGH);

  // open memory card
  if (!card.init_card()) {
    putstring_nl("Card init failed!"); return;
  }
  if (!card.open_partition()) {
    putstring_nl("No partition!"); return;
  }
  if (!card.open_filesys()) {
    putstring_nl("Couldn't open filesys"); return;
  }
  if (!card.open_rootdir()) {
    putstring_nl("Couldn't open dir"); return;
  }
}

void loop() {   
  switch (check_switches()) {
    case 1:
      playcomplete("SOUND1.WAV");
      break;
    case 2:
      playcomplete("SOUND2.WAV");
      break;
    case 3:
      playcomplete("SOUND3.WAV");
      break;
    case 4:
      playcomplete("SOUND4.WAV");
      break;
    case 5:
      playcomplete("SOUND5.WAV");
      break;
    case 6:
      playcomplete("SOUND6.WAV");
  }
}

byte check_switches()
{
  static byte previous[6];
  static long time[6];
  byte reading;
  byte pressed;
  byte index;
 
  for (byte index = 0; index < 6; ++index) {
    reading = digitalRead(14 + index);
    if (reading == LOW && previous[index] == HIGH && millis() - time[index] > DEBOUNCE)
    {
      // switch pressed
      time[index] = millis();
      pressed = index + 1;
      break;
    }
    previous[index] = reading;
  }
  // return switch number (1 - 6)
  return (pressed);
}

void playcomplete(char *name) {
  playfile(name);
  while (wave.isplaying);
  card.close_file(f);
}

void playfile(char *name) {
  // stop any file already playing
  if (wave.isplaying) {
    wave.stop();
    card.close_file(f);
  }

  f = card.open_file(name);
  if (f && wave.create(f)) {
    wave.play();
  }
}


Regards,
Eric
ebs
 
Posts: 10
Joined: Thu May 22, 2008 12:42 pm

by adafruit on Thu Jun 12, 2008 12:08 am

lovely! thanks for posting it. ive been swamped :(

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

by cippo65 on Sat Jun 21, 2008 1:53 pm

This is fantastic. I will name my next piece after you.
cippo65
 
Posts: 4
Joined: Thu Jun 05, 2008 6:21 pm
Location: chicago

by rasterblaster on Wed Jul 02, 2008 5:56 am

Just thinking of buying one of these to do sound effects for a sculpture, and I was wondering what delay is there when switching between soundeffects..

I want it to play a generator 'humm' effect and then when a button is pressed pay another sample that ramps up the generator, and another that slows it back down... as the sound is supposed to be continuous, I dont want it to pause in between sounds..

if that makes sense... :o)

Also I am reading that this totally fills up the capability of a board, so I could get another arduino to control this one and 'press its buttons' as sound effects are needed I am guessing??

I have never used one of these before, but it looks like I can program them to cointrol the sound and light show on my sculpture I am guessing...
if you like science fiction art and sci-fi sculpture, have a look at my website www.marty.com.au
rasterblaster
 
Posts: 11
Joined: Wed Jul 02, 2008 5:51 am
Location: Melbourne, Australia

by adafruit on Thu Jul 03, 2008 9:59 pm

rasterblaster wrote:Just thinking of buying one of these to do sound effects for a sculpture, and I was wondering what delay is there when switching between soundeffects..


i think its quite fast to start not 'instantaneous' tho. i havent measured it...i would guess about 200 millisec

Also I am reading that this totally fills up the capability of a board, so I could get another arduino to control this one and 'press its buttons' as sound effects are needed I am guessing??


yup you could do that pretty easily. or just send it 'serial' commands

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

by ebs on Sat Jul 05, 2008 6:03 pm

Here's a slightly modified version that will start playing a new sound while another is
still playing, instead of waiting until the currently playing sound finishes.

It certainly seems like the change from one sound to the other is instantaneous.

Regards,
Eric
Code: Select all | TOGGLE FULL SIZE
#include <AF_Wave.h>
#include <avr/pgmspace.h>
#include "util.h"
#include "wave.h"

#define DEBOUNCE 20

#define swPin 14

AF_Wave card;
File f;
Wavefile wave;

void setup() {
  byte i;
 
  // set up serial port
  Serial.begin(9600);

  // set up waveshield pins
  for (byte i = 2; i <= 5; ++i) {
    pinMode(i, OUTPUT);
  }
 
  // enable pull-up resistors on
  // switch pins (analog inputs)
  for (byte i = swPin; i <= swPin + 5; ++i) {
    digitalWrite(i, HIGH);
  }

  // open memory card
  if (!card.init_card()) {
    putstring_nl("Card init failed!"); return;
  }
  if (!card.open_partition()) {
    putstring_nl("No partition!"); return;
  }
  if (!card.open_filesys()) {
    putstring_nl("Couldn't open filesys"); return;
  }
  if (!card.open_rootdir()) {
    putstring_nl("Couldn't open dir"); return;
  }
}

void loop() {   
  switch (check_switches()) {
    case 1:
      playfile("SOUND1.WAV");
      break;
    case 2:
      playfile("SOUND2.WAV");
      break;
    case 3:
      playfile("SOUND3.WAV");
      break;
    case 4:
      playfile("SOUND4.WAV");
      break;
    case 5:
      playfile("SOUND5.WAV");
      break;
    case 6:
      playfile("SOUND6.WAV");
  }
}

byte check_switches()
{
  static byte previous[6];
  static long time[6];
  byte reading;
  byte pressed;
  byte index;
 
  for (index = 0; index < 6; ++index) {
    reading = digitalRead(swPin + index);
    if (reading == LOW && previous[index] == HIGH && millis() - time[index] > DEBOUNCE)
    {
      // switch pressed
      time[index] = millis();
      pressed = index + 1;
      break;
    }
    previous[index] = reading;
  }
  // return switch number (1 - 6)
  return (pressed);
}

void playfile(char *name) {
  // stop any file already playing
  if (wave.isplaying) {
    wave.stop();
  }
  // close file if open
  if (f) {
    card.close_file(f);
  }
  // play specified file
  f = card.open_file(name);
  if (f && wave.create(f)) {
    wave.play();
  }
}
ebs
 
Posts: 10
Joined: Thu May 22, 2008 12:42 pm

by adafruit on Sun Jul 06, 2008 5:14 pm

lovely! thanks, i actually had a working example but have been too lazy to post it :(

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

How can I do this with one button?

by benhalsall on Tue Jul 08, 2008 11:39 am

Hi,

I'm just trying to get this to work with one button rather than 6 but am struggling.

Basically I have a number of tracks and want to know how to get one button to jump to the next track.

Any thoughts?

Thanks,

Ben
Ben Halsall

Artist // Apple Mac Guru

w: http://www.benhalsall.co.uk

** Apple Certified Trainer - Final Cut Pro **
** Apple Certified Pro - DVD Studio Pro **
** Apple Certified Pro - Motion **
benhalsall
 
Posts: 2
Joined: Fri Jul 04, 2008 5:53 am

by adafruit on Tue Jul 08, 2008 7:26 pm

look at the 'DAP' example on the website

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

by pfeifmusic on Wed Jul 09, 2008 9:35 pm

Thanks for the code above! I got it working and it's so close to what I need, but I have one question. I'm trying to make a sound box for my little girl's first birthday and I would like four buttons to make individual sounds. This works, but after a sound ends, no more buttons can be pressed without reseting the arduino. Is there an easy fix that would allow me to reset the arduino after a file finishes playing or something like that?

Thanks.
pfeifmusic
 
Posts: 7
Joined: Wed Jul 09, 2008 9:32 pm
Location: MASS

by rasterblaster on Wed Jul 09, 2008 10:14 pm

Getting very excited about all this, just ordered two from ladyada a few days ago.. cant wait to have a play, and I am sure I will have lots more questions when they arrive...

Marty
if you like science fiction art and sci-fi sculpture, have a look at my website www.marty.com.au
rasterblaster
 
Posts: 11
Joined: Wed Jul 02, 2008 5:51 am
Location: Melbourne, Australia

by intellijel on Wed Aug 27, 2008 5:19 pm

I have tried the code and would not work for me.

My wave shield definitely works (I tried other sketches that just play the audio files on the disk)

and I tried editing the code so it is not waiting for buttons presses, it just plays the first file in a loop.

What I can't get to work is button responses. I measured the analog in pins and the voltage is 5V (so the pullup is working) and when I ground the pins the input goes to 0 but I get no response.

I need to put some debugging points in but I was wondering if anyone else experienced issues. Is this due to the debouncing settings?



thanks,
Danjel

intellijel
 
Posts: 48
Joined: Mon Aug 15, 2005 6:14 pm

by pfeifmusic on Wed Aug 27, 2008 11:06 pm

Hi Danjel,

Can you post your code so we can see if something looks goofy?

Thanks,
Mike
pfeifmusic
 
Posts: 7
Joined: Wed Jul 09, 2008 9:32 pm
Location: MASS

Please be positive and constructive with your questions and comments.