Problem knowing where to add specific functions to sketch - eg. Trigger sound

Hi,

I'm working on a conceptual art sound recording and playback project. I have a very basic understanding code and am getting stuck on anything that requires adding code from other sketches to progress my project.

Hardware I'm using:

  • Arduino uno
  • micro sd card and adapter
  • Microphone to record sound
  • 0.5w speaker to play sound
  • Piezo buzzer (as a trigger sensor - haven't got to work yet)

What I would like my project to do:

  1. ‘startRecording’
  • Trigger recording when sound level passes over xxx level.
  • Use piezo buzzer as sound sensor to detect sound level.
  • Sound is recorded with microphone and saved to sd card.
  1. ’stopRecording’
  • Recording is set to stop after 10 seconds.
  1. ‘playRecording’
  • Playback a random selected 10 second sound recording stored on sd card.
  • Playback to start 3 seconds after 'startRecording' has been triggered.
  1. Distort Playback
  • Random selected 10 second sound recording is distorted when played back.

Current project status:

  • I have had help to put together a sketch that can startRecording, stopRecording and playbackRecording using keyboard entry in the serial monitor eg. 'R' will startRecording and 's' will stopRecording.

  • The sound is recorded as a .wav and saved on the sd card in consecutive file naming. eg file is saved as test01.wav, then test02.wav etc etc.

Next step:

  • The next step I am working on is to use a piezo buzzer as a sound sensor to trigger the 'startRecording' function when a person starts talking into the device. I have looked at the 'knock' sketch in the arduino examples but don't know where to begin trying to add that code into my sketch.

  • The next step after this would be to get the other functions to work as listed above in what I would like my project to do.

Any help would be greatly appreciated!

Cheers, JC

Here is my current working sketch:

/*
This sketch demonstrates recording of standard WAV files that can be played on any device that supports WAVs. The recording
uses a single ended input from any of the analog input pins. Uses AVCC (5V) reference currently.

Requirements:
Class 4 or 6 SD Card
Audio Input Device (Microphone, etc)
Arduino Uno,Nano, Mega, etc.

Steps:
1. Edit pcmConfig.h
   a: On Uno or non-mega boards, #define buffSize 128. May need to increase.
   b: Uncomment #define ENABLE_RECORDING and #define BLOCK_COUNT 10000UL

2. Usage is as below. See https://github.com/TMRh20/TMRpcm/wiki/Advanced-Features#wiki-recording-audio for
  additional informaiton.

Notes: Recording will not work in Multi Mode.
Performance is very dependant on SD write speed, and memory used.
Better performance may be seen using the SdFat library. See included example for usage.
Running the Arduino from a battery or filtered power supply will reduce noise.
*/

#include <SD.h>
#include <SPI.h>
#include <TMRpcm.h>

#define SD_ChipSelectPin 4  //example uses hardware SS pin 53 on Mega2560
//#define SD_ChipSelectPin 4  //using digital pin 4 on arduino nano 328, can use other pins

TMRpcm audio;   // create an object for use in this sketch 
int ctr = 0;
String writeName;
String readName;
char writeVar[11];
char readVar[11];
void setup() {
 
 audio.speakerPin = 9; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc
 pinMode(12,OUTPUT);  //Pin pairs: 9,10 Mega: 5-2,6-7,11-12,46-45
 
 Serial.begin(115200);
 
 if (!SD.begin(SD_ChipSelectPin)) {  
   return;
 }else{
   Serial.println("SD OK"); 
 }
 // The audio library needs to know which CS pin to use for recording
 audio.CSPin = SD_ChipSelectPin;
 
}

//Functions for updating the file names

void updateWriteName(){
 String ctrStr = String(ctr);
 writeName = String(ctrStr+"test.wav");
 for(int i=0;i<writeName.length();i++){
  writeVar[i]=writeName.charAt(i); 
 }
 Serial.println(writeVar);
}

void updateReadName(){
 String ctrStr = String(ctr);
 readName = String(ctrStr+"test.wav");
  for(int i=0;i<readName.length();i++){
  readVar[i]=readName.charAt(i); 
 }
 Serial.println(readVar);
}

//this one lets you specify a number
void updateReadName(int num){
 String ctrStr = String(num);
 readName = String(ctrStr+"test.wav");
  for(int i=0;i<readName.length();i++){
  readVar[i]=readName.charAt(i); 
 }
 Serial.println(readVar);
}

//Functions for triggering recording
void triggerRecord(){

 updateWriteName();
 updateReadName();
 audio.startRecording(writeVar,16000,A0,1);
 
}


void loop() {
 
   if(Serial.available()){ 
     //Send commands over serial to play
     //setup file naming
     

     
     switch(Serial.read()){
       
       case 'r': audio.startRecording("test.wav",16000,A0); break;    //Record at 16khz sample rate on pin A0
       case 'R': triggerRecord();
                 break;  //Record, but with passthrough to speaker.
       case 't': audio.startRecording("test.wav",16000,A0,2); break;  //Do not record. Output direct to speaker
              //Note: If samples are dropped before writing, it
              //      will not be heard in passthrough mode
       case 's': audio.stopRecording(readVar); 
                 ctr +=1;
                 break;              //Stop recording
       case 'p': audio.play(readVar);break;                       //Play the recording 
       case '=': audio.volume(1); break;                              //Increase volume by 1. Does not affect recording
       case '-': audio.volume(0); break;                              //Decrease volume by 1. Does not affect recording
       case 'S': audio.stopPlayback(); break;           //Stop all playback
       case '1': updateReadName(1);                                //Pick file to play by name
                 audio.play(readVar);
                 break;     
     }
   }
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Hi,

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Piezo buzzer (as a trigger sensor - haven't got to work yet)

A Peizo buzzer is an output device, it makes a noise.
You detect the noise with the microphone.
Use the microphone to detect the sound level to trigger the record and to make the recording.
What you are making is a sound activated recorder.

Tom..... :slight_smile:

Thanks for the tip @TomGeorge

Any tips on the where to start with coding that function?

You may get some useful ideas in planning and implementing a program

...R