Hi. I am very new to Arduino and I am here to seek for help.
I am currently working on a recording machine that record when it reaches some sound level and record for 10 seconds and saves it in SD card.
I have manage to save the first file in SD Card but after first recording, the sensor value become 0 so it can not detect the sound level anymore.
I think I have a problem in the loop but can't figure it out.
This image shows that after recording the sensor value become 0;
Please help me thank you!
#include <SD.h>
#include <SPI.h>
#include <TMRpcm.h>
#define SD_ChipSelectPin 10
TMRpcm audio;
int audiofile = 0;
unsigned long i = 0;
bool recmode = 1;
const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;
int Reset = 4;
void setup()
{
Serial.begin(9600);
pinMode(A0, INPUT);
digitalWrite(Reset, HIGH);
pinMode(Reset, OUTPUT);
//pinMode(6, OUTPUT);
//pinMode(2, INPUT_PULLUP);
//attachInterrupt(0, button, LOW);
SD.begin(SD_ChipSelectPin);
audio.CSPin = SD_ChipSelectPin;
}
void loop()
{
unsigned long startMillis= millis(); // Start of sample window
unsigned int peakToPeak = 0; // peak-to-peak level
unsigned int signalMax = 0;
unsigned int signalMin = 1024;
// collect data for 50 mS
while (millis() - startMillis < sampleWindow)
{
sample = analogRead(0);
if (sample < 1024) // toss out spurious readings
{
if (sample > signalMax)
{
signalMax = sample; // save just the max levels
}
else if (sample < signalMin)
{
signalMin = sample; // save just the min levels
}
}
}
peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
double volts = (peakToPeak * 5.0) / 1024; // convert to volts
Serial.println(volts);
//This section is showing the sound level in the serial monitor.
if(volts>0.3){
audio.startRecording("1.wav", 16000, A0);
recmode ==1;
delay(10000);
audio.stopRecording("1.wav");
recmode ==0;
Serial.print('Y') ; // print to show the recording is done
delay(10000);
}
}