I recently assembled a Wave Shield for an art project, I was doing well until I fell into a code abyss.
My project is essentially a box. I need to be able to push a button which will cause two LEDs to light up (daisy-chained to a single pin right now), plus play an audio track (wave file). The LEDs should remain lit for the duration the audio is playing.
This is my first time using Arduino, please assume I know nothing, no coding.
The other problem is that this project has to be completed in the next 48 hrs.
I realize that I have taken on something way out of my circle of expertise, I'm pretty desperate for help.
Any information would be helpful. Many, many thanks in advance.
Here is the code I have assembled so far.
I think there is superfluous code in there...please be patient with me!
[Edit - moderator - use code button when submitting code]
Code: Select all
#include <AF_Wave.h>
#include <avr/pgmspace.h>
#include "util.h"
#include "wave.h"
AF_Wave card;
File f;
Wavefile wave; // only one!
#define redled 9
uint16_t samplerate;
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Wave test!");
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
/* pinMode(9, OUTPUT);//led
pinMode(13, INPUT);//button
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(redled, OUTPUT);*/
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;
}
putstring_nl("Files found:");
ls();//above debugging
}
void ls() {
char name[13];
int ret;
card.reset_dir();
putstring_nl("Files found:");
while (1) {
ret = card.get_next_name_in_dir(name);
if (!ret) {
card.reset_dir();
return;
}
Serial.println(name);//files found, things ok
}
}
uint8_t tracknum = 0;
void loop() {
uint8_t i, r;
char c, name[15];
buttonState = digitalRead(buttonPin);
card.reset_dir();
// scroll through the files in the directory
for (i=0; i<tracknum+1; i++) {
r = card.get_next_name_in_dir(name);
if (!r) {
// ran out of tracks! start over
tracknum = 0;
return;
}
}
putstring("\n\rPlaying "); Serial.print(name);
// reset the directory so we can find the file
card.reset_dir();
playcomplete(name);
tracknum++;
//names
}
void playcomplete(char *name) {
uint16_t potval;
uint32_t newsamplerate;
playfile(name);
samplerate = wave.dwSamplesPerSec;
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
while (wave.isplaying) {
// you can do stuff here!
delay(500);//ms
}
card.close_file(f);
}
void playfile(char *name) {
f = card.open_file(name);
if (!f) {
putstring_nl(" Couldn't open file"); return;
}
if (!wave.create(f)) {
putstring_nl(" Not a valid WAV"); return;
}
// ok time to play!
wave.play();
}