Hello everyone!
So i have my arduino and i just got a waveshield from Adafruit. I have it assembled and on my arduino ready to go.
Where can i look for code to play sound when my arduino turns on? Basically when the arduino chip receives power it will play the sounds. I cant really seem to find any tutorials on waveshields.
There are examples in the wave shield libary. Just put the code to call the sound playing in the start up section of your code.
I saw another posting you replied to about where i need sd reader and some other things under import library. I dont have that, I have WAVE HC under it though.
And other then that, i have under sketchbook libraries, is wave hc then some examples.
When i load the sd card initializer im getting a "AF_wave is not a type"
Look at this:-
http://www.ladyada.net/make/waveshield/examples.html
Now get the WaveHC library from:-
http://www.ladyada.net/make/waveshield/download.html
Un zip it and drag the WaveHC folder it contains into your libraries folder.
Restart the arduino IDE .
When i load the sd card initializer im getting a "AF_wave is not a type"
What code?
Then you can run this code:-
/*
SERIAL TO SAMPLE - By Mike Cook Nov 2010
Based on the Lady Ada code
This will take a byte from the serial port and play the sample corresponding to the ASCII character
received minus 0x30, so for example if it receives '2' (ASCII 0x32) it will play the sample file 02.WAV
*/
#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
#define DEBOUNCE 5 // button debouncer
// Global variables
int num=0;
char toPlay[8]; // file to play 00.WAV to 99.WAV
// This handy macro lets us determine how big the array up above is, by checking the size
// 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_nl("WaveHC with ");
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);
// initilise file extension part of file to play
toPlay[2] = '.';
toPlay[3] = 'W';
toPlay[4] = 'A';
toPlay[5] = 'V';
toPlay[6] = '\0'; // terminator
// 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!
}
// Try to open the root directory
if (!root.openRoot(vol)) {
putstring_nl("Can't open root dir!"); // Something went wrong,
while(1);
}
}
void loop() {
if(Serial.available() > 0) {
num = Serial.read();
num -= 0x30;
// Serial.println(num,HEX);
makeName(num);
playfile(toPlay);
delay(1000);
// wave.stop();
}
}
void makeName(int number){ // generates a file name 00.WAV to 99.WAV
int i=0;
if(num > 9) {
toPlay[0] = ((num/10) & 0xf) | 0x30;
toPlay[1] = ((num%10) & 0xf) | 0x30;
}
else {
toPlay[0] = 0x30;
toPlay[1] = (num & 0xf) | 0x30;
}
}
// 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_nl("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();
}
Youre freaking awesome -.-!!! So i ran the code and from what i can tell its working.
It just says done uploading. I havent gotten a finished compiling or anything yet.
So heres another question. I still want it to play some audio files when it gets powered on.
And i have another sketch that controls servos. Can i just add it to the bottom of this one or vice versa?
I still want it to play some audio files when it gets powered on
As the last thing in the setup function use something like:-
playfile("init.WAV");
Or what ever file you want to play.
Can i just add it to the bottom of this one or vice versa
Make the servo code into a function and call the function from the loop() function.
okay. I cant seem to get it working...
How do i integrate this
void setup()
{
<all the SD initialization and whatever else is already there>
playcomplete("FILE1.WAV");
playcomplete("FILE2.WAV");
} // end of setup()
into what you gave me before?
/*
SERIAL TO SAMPLE - By Mike Cook Nov 2010
Based on the Lady Ada code
This will take a byte from the serial port and play the sample corresponding to the ASCII character
received minus 0x30, so for example if it receives '2' (ASCII 0x32) it will play the sample file 02.WAV
*/
#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
#define DEBOUNCE 5 // button debouncer
// Global variables
int num=0;
char toPlay[8]; // file to play 00.WAV to 99.WAV
// This handy macro lets us determine how big the array up above is, by checking the size
// 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_nl("WaveHC with ");
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);
// initilise file extension part of file to play
toPlay[2] = '.';
toPlay[3] = 'W';
toPlay[4] = 'A';
toPlay[5] = 'V';
toPlay[6] = '\0'; // terminator
// 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!
}
// Try to open the root directory
if (!root.openRoot(vol)) {
putstring_nl("Can't open root dir!"); // Something went wrong,
while(1);
}
void loop() {
if(Serial.available() > 0) {
num = Serial.read();
num -= 0x30;
// Serial.println(num,HEX);
makeName(num);
playfile(toPlay);
delay(1000);
// wave.stop();
}
}
void makeName(int number){ // generates a file name 00.WAV to 99.WAV
int i=0;
if(num > 9) {
toPlay[0] = ((num/10) & 0xf) | 0x30;
toPlay[1] = ((num%10) & 0xf) | 0x30;
}
else {
toPlay[0] = 0x30;
toPlay[1] = (num & 0xf) | 0x30;
}
}
// 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_nl("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();
}
So i have my code all ready. But when i power on arduino. It doesnt seem to play. Ive tried a speaker and the head phone jack. any idea why?
heres my code
#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
#define DEBOUNCE 5 // button debouncer
// Global variables
int num=0;
char toPlay[8]; // file to play 00.WAV to 99.WAV
// This handy macro lets us determine how big the array up above is, by checking the size
// 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_nl("WaveHC with ");
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);
// initilise file extension part of file to play
toPlay[2] = '.';
toPlay[3] = 'W';
toPlay[4] = 'A';
toPlay[5] = 'V';
toPlay[6] = '\0'; // terminator
// 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!
}
// Try to open the root directory
if (!root.openRoot(vol)) {
putstring_nl("Can't open root dir!"); // Something went wrong,
while(1);
}
// Try to open the root directory
if (!root.openRoot(vol)) {
putstring_nl("Can't open root dir!"); // Something went wrong,
while(1);
}
playcomplete( "IMPORTIN.wav");
playcomplete( "ONLINE.wav");
}
void loop() {
if(Serial.available() > 0) {
num = Serial.read();
num -= 0x30;
// Serial.println(num,HEX);
makeName(num);
playfile(toPlay);
delay(1000);
// wave.stop();
}
}
void makeName(int number){ // generates a file name 00.WAV to 99.WAV
int i=0;
if(num > 9) {
toPlay[0] = ((num/10) & 0xf) | 0x30;
toPlay[1] = ((num%10) & 0xf) | 0x30;
}
else {
toPlay[0] = 0x30;
toPlay[1] = (num & 0xf) | 0x30;
}
}
// 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_nl("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();
}
Have you got the wav files in the correct form in terms of sampling rate.
Try this file and see if you can play that.
SO.WAV (22.2 KB)
okay i finally GOT IT ALL WORKING! its playing perfectly fine now. and i got a speaker wired to it as well!
ONE MORE FINAL THING I ASK OF YOU GUYS!
how do im implement these two codes together
#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
#define DEBOUNCE 5 // button debouncer
// Global variables
int num=0;
char toPlay[8]; // file to play 00.WAV to 99.WAV
// This handy macro lets us determine how big the array up above is, by checking the size
// 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_nl("WaveHC with ");
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);
// initilise file extension part of file to play
toPlay[2] = '.';
toPlay[3] = 'W';
toPlay[4] = 'A';
toPlay[5] = 'V';
toPlay[6] = '\0'; // terminator
// 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!
}
// Try to open the root directory
if (!root.openRoot(vol)) {
putstring_nl("Can't open root dir!"); // Something went wrong,
while(1);
}
// Try to open the root directory
if (!root.openRoot(vol)) {
putstring_nl("Can't open root dir!"); // Something went wrong,
while(1);
}
playcomplete( "IMPORTIN.wav");
playcomplete( "ONLINE.wav");
}
void loop() {
if(Serial.available() > 0) {
num = Serial.read();
num -= 0x30;
// Serial.println(num,HEX);
makeName(num);
playfile(toPlay);
delay(1000);
// wave.stop();
}
}
void makeName(int number){ // generates a file name 00.WAV to 99.WAV
int i=0;
if(num > 9) {
toPlay[0] = ((num/10) & 0xf) | 0x30;
toPlay[1] = ((num%10) & 0xf) | 0x30;
}
else {
toPlay[0] = 0x30;
toPlay[1] = (num & 0xf) | 0x30;
}
}
// 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_nl("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();
}
the code for the wave shield, and this code for my servos
#include <Servo.h>
Servo myservo; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int potpin2 = 1; // analog pin used to connect the potentiometer0
int val2; // variable to read the value from the analog pin
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(8); // attaches the servo on pin 9 to the servo object
}
void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 20, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(5); // waits for the servo to get there
val2 = analogRead(potpin2); // reads the value of the potentiometer (value between 0 and 1023)
val2 = map(val2, 0, 1023, 180, 20); // scale it to use it with the servo (value between 0 and 180)
myservo2.write(val2); // sets the servo position according to the scaled value
delay(5);
}
// waits for the servo to get there
It helps if you have good code to start with. That servo code looks poor. It only works because it keeps repeating over and over. Th 5mS delay isn't enough to move the servo very far.
how do im implement these two codes together
To do what, just one function after the other then look at :-
http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html
But you need to think exactly what you want it to do and implement that.
So how can i strengthen my servo code? im fairly new to all this.
And basically i wanted the sounds to play when the board came online then be able to control my servos.
Im looking at your merging code info now. Trying to make sense of everything.
So how can i strengthen my servo code?
It depends on what you want it to do.
For the code you posted I would only issue a command to the servos if the reading from the pot changed by more than 2. Something like:-
if(abs(oldReading-newReading) >= 2) {
val = map(newReading, 0, 1023, 20, 180);
myservo.write(val);
}
oldReading = newReading;
okay, ive been sick for a few days so i havnt had a chance to look anything over.
So basically what im wanting the servos to do is. With the slider potentiometer one servo goes clockwise 180 degrees. and then the other servo goes 180 counter clockwise. And i just want it to follow the slider and read its position basically.
How can i use this new code for that?
Just change the numbers in the map function for each pot.
okay awesome!
Also whats a good power source if i wanna unplug it from my computer?
Would 12v battery pack be efficient?
A 9V would be better, but not those small square batteries they are rubbish.
As the arduino needs about 6.5V minimum to regulate down to 5V any more than this is simply wasted in heat and is not very efficient.
would this one do the trick?
Yes that looks fine.