I have worked on this project, and I am a straight newbie does not have much of coding background.
GOALS : Triggering Wave shield by the volume of area to play different .WAV files.
I have a MIC-AMP max4466 which has Analog pin , gnd, 3v pin.
I have a Wave shield from Adafurit
I have a Arduino Uno.
So far what I have done.
-playing WAV files from WaveHC example's dapch example - successful
-measuring Volume level through mixing someone's (Basic sound sensor sketch for Arduino · GitHub) + (Measuring Sound Levels | Adafruit Microphone Amplifier Breakout | Adafruit Learning System) = learned how to set a threshold values to trigger LEDs to light up with certain volume.
Now, I have to combine these two together, and code it.
But, I am stuck at this phase that adding two different codes does not really work.
Here is my logics
-
- read the WaveShield settings, and read wav files from the SD Card.
- play 'NOTRAINN.WAV' from the sd card,
- read Analog output's value through serial monitor,
- Set a threshold values, and if the input goes over the thresvalue, play different WAV files which is loaded in the sd card.
- if the volume goes back to normal, play 'NOTRAINN.WAV' from the sd card. from step 1
- loop it.
#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
int threshold = 1; //Change This has to be higher than 1
int volts;
const int sampleWindow = 80; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;
int Analoginput = A0;
int timer = 143; // The higher the number, the slower the timing.
char ledPins[] = { 6, 7, 8, 9, 15, 16, 17 }; // an array of pin numbers to which LEDs are attached
int pinCount = 7; // the number of pins (i.e. the length of the array)
int val = 0;
int thisPin;
// 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("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);
//pinMode(led, OUTPUT);
// pin13 LED
//pinMode(13, OUTPUT);
int thisPin;
// the array elements are numbered from 0 to (pinCount - 1).
// use a for loop to initialize each pin as an output:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], 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
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!
}
// 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("Files found (* = fragmented):");
// Print out all of the files in all the directories.
root.ls(LS_R | LS_FLAG_FRAGMENTED);
}
void loop() {
}
// 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) {
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
// loop from the highest pin to the lowest:
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
// wave.stop();
}
// 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
[\code]
I am so stuck. I know that I should start from the baby steps, but I do not have any coding experiences that I can use. I've worked on this project for a month so far, and I am so ready to move on.
please
Help me Guys.
:( thanks! :)
[/color]