For my project I want to play an Interview after having inplugged in a 3,5mm jack with integrated switch and having stopped and rewind after having unplugged.
I built an external Interrupt using PIN3 (changed PIN3 --> PIN7 which works) which reads the open or closed circuit.
The electric drawing is: 5V (connected to L PIN of jack) / switch(free PIN of jack)/ PIN3/ resistor 1M Ohm / GND
I got it work in the sense of being able to hear the Interview but after having inplugged (which makes an uncomfortable sound?) I have to wait several seconds while hearing a peep sound, until the actual sound starts. Do I have to improve my skech which I added? Is it a software or a hardware problem?
And how can I avoid a disturbing backround noise while hearing the Interview? I there sth i have to be aware of while export the wav. file?
I would appreciate your help!
Code: Select all
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"
//variablen
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 (wav)
WaveHC wave; // This is the only wave (audio) object, since we will only play one at a time
#define DEBOUNCE 1 // button debouncer
volatile byte pluggedIn;
volatile byte pluggedInLast = 1;
//functions
// 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(0);
}
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(7, OUTPUT);
//pinMode(4, OUTPUT);
//pinMode(5, OUTPUT);
// pin3 input
pinMode(3, INPUT);
// 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(0); // then 'halt' - do nothing!
}
// Now we will look for a FAT partition!--> 1 partition in my case
if (!vol.init(card, 1)) {
putstring_nl("no valid FAT partition");
sdErrorCheck();
while(0);
}
// Lets tell the user about what we found
putstring("Using partition ");
Serial.print(1, 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(0);
}
putstring_nl("load wave file");
// look in the root directory and open the file
if (!f.open(root, "STADTTUR.WAV")) {
putstring("Couldn't open file STADTTUR.WAV");
}
// OK read the file and turn it into a wave object
if (!wave.create(f)) {
putstring_nl("Not a valid WAV"); return;
}
// Whew! We got past the tough parts.
putstring_nl("Ready!");
//TCCR2A = 0;
//TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20;
//Timer1 Overflow Interrupt Enable
//TIMSK2 |= 1<<TOIE2;
}
//SIGNAL(TIMER1_OVF_vect) {
// putstring_nl("signal ausgerufen");
// pluggedIn = digitalRead (3);
// if (pluggedIn == 1) {
// putstring_nl("klinkenstecker in buchse");
// }
//}
void loop() {
//putstring_nl("loop ausgerufen");
byte i;
pluggedIn = digitalRead (3);//Pin auslesen
//beim Wechsel des Signals (von 1 auf 0 oder von 0 auf 1)
if (pluggedIn != pluggedInLast) {
pluggedInLast = pluggedIn;//aktueller Wert als letzten Wert speichern
//bei Wechsel von 1 auf 0
if (pluggedIn == 0) {
putstring_nl("signal went from 1 to 0");
playfile();
}
else {
putstring_nl("signal went from 0 to 1");
stopfile();
}
}
}
void playfile() {
stopfile();
// ok time to play! start playback
putstring_nl("start to play file ");
wave.play();
}
void stopfile() {
// see if the wave object is currently doing something
if (wave.isplaying) {// already playing something, so stop it!
putstring_nl("wave is playing...stop it");
wave.stop();
f.rewind();
wave.create(f);
}
}
// stop it