New to Arduino Sound detection project

Hi Guys I am new to Arduino projects, and I am really green when it comes to programming. I am doing a project for uni and is basically a sound detection system with 4 Mics and a temp and humidity module. So far I managed to code the mics part but does not seem to work that well. Any help would be much appreciated. The aim is to be able to pin point the location of sound source. I can post some images if it helps but here is my sketch so far I get some feed back that one mic works but no response on loud sounds.

// Sound detection by Washington Sanchez
//Arduino Uno Sound Detection Sensor Modules

int microphoneArray = {1, 2, 3, 4}; // We use 4 microphones
int numberOfMicrophones = 4; /// new variable to set number of microphones (so all the 4’s used in the for loop conditions have been changed to this)

define microphone 1 pin8 // These are the pins in which the mics 1, 2, 3, and 4 are plugged to

define microphone 2 pin9

define microphone 3 pin10

define microphone 4 pin11

int soundDetectValOne = HIGH; // This is where we record our Sound Measurement
int soundDetectValTwo = HIGH;
int soundDetectValThree = HIGH;
int soundDetectValFour = HIGH;

boolean bAlarmOne = false;
boolean bAlarmTwo = false;
boolean bAlarmThree = false;
boolean bAlarmFour = false;

unsigned long lastSoundDetectTimeOne; // Record the time that we measured a sound
unsigned long lastSoundDetectTimeTwo;
unsigned long lastSoundDetectTimeThree;
unsigned long lastSoundDetectTimeFour;

int soundAlarmTime = 500; // Number of milli seconds to keep the sound alarm high

void setup() {
// put your setup code here, to run once:

Serial.begin(9600);
Serial.println(“Our experiment starts …”);

for (int i = 0; i < numberOfMicrophones; i++) { /// Moved the int classification for i into this condition declaration, and the i++ is a compound opperator that means the same thing as “i = i+1” (not a massive deal, but improves code efficiency)

pinMode(microphoneArray*, INPUT); //input from each microphone*

  • }*
  • Serial.println(“all mikes are set up now.”);*
  • Serial.println();*
    }
    void loop() {
  • // put your main code here, to run repeatedly:*
  • /// To address potential issue of previous script,will try to run different loops for each mic.*
  • /// I noticed this is how other people doing triangulation with microphones chose to opperate as well.*
  • /// I’m not sure how this will affect your application because timing will be very slightly off (keeping in mind arduino runs millions of instructions/sec)…*
  • /// Reading SoundDetectVal for all 4 microphones initially:*
  • soundDetectValOne = digitalRead(microphoneArray[1]); //read the sound alarm time mic 1*
  • soundDetectValTwo = digitalRead(microphoneArray[2]); //read the sound alarm time mic 2*
  • soundDetectValThree = digitalRead(microphoneArray[3]); //read the sound alarm time mic 3*
  • soundDetectValFour = digitalRead(microphoneArray[4]); //read the sound alarm time mic 4*
  • //----------------------------------------------------------------------------------*
  • /// MICROPHONE 1*
  • if (soundDetectValOne == LOW) { // If we hear a sound*
  • lastSoundDetectTimeOne = millis();*
  • // The following is so you don’t scroll on the output screen.*
  • if (!bAlarmOne) {*
  • Serial.println("-------------------------");*
  • Serial.println("sound detected on mike: ");*
  • Serial.println(microphoneArray[1]); //print the microphone number that heard the sound*
  • Serial.println(“LOUD, LOUD”);*
  • bAlarmOne = true;*
  • }*
  • }*
  • else {*
  • if ( ((millis() - lastSoundDetectTimeOne) > soundAlarmTime) && bAlarmOne) { /// Added brackets. Assuming there were 2 conditions to be satisfied: (1)(millis()-lastSoundDetectTime) > soundAlarmTime) and (2) bAlarm == true. added brackets to make sure it does NOT say “if (millis()-lastSoundDetectTime) is greater than soundAlarmTime and also greater than bAlarm”*
  • Serial.println("-------------------------");*
  • Serial.println(“quiet”);*
  • Serial.println("Current time: ");*
  • Serial.println(millis()); // debug code: print vars*
  • Serial.println("Last time when sound was detected: ");*
  • Serial.println(lastSoundDetectTimeOne); // debug*
  • Serial.println("sound alarm Time set by user: ");*
  • Serial.println(soundAlarmTime); // debug*
  • bAlarmOne = false;*
  • }*
  • }*
  • //----------------------------------------------------------------------------------*
  • /// MICROPHONE 2*
  • if (soundDetectValTwo == LOW) { // If we hear a sound*
  • lastSoundDetectTimeTwo = millis();*
  • // The following is so you don’t scroll on the output screen.*
  • if (!bAlarmTwo) {*
  • Serial.println("-------------------------");*
  • Serial.println("sound detected on mike: ");*
  • Serial.println(microphoneArray[2]); //print the microphone number that heard the sound*
  • Serial.println(“LOUD, LOUD”);*
  • bAlarmTwo = true;*
  • }*
  • }*
  • else {*
  • if ( ((millis() - lastSoundDetectTimeTwo) > soundAlarmTime) && bAlarmTwo) {*
  • Serial.println("-------------------------");*
  • Serial.println(“quiet”);*
  • Serial.println("Current time: ");*
  • Serial.println(millis()); // debug code: print vars*
  • Serial.println("Last time when sound was detected: ");*
  • Serial.println(lastSoundDetectTimeTwo); // debug*
  • Serial.println("sound alarm Time set by user: ");*
  • Serial.println(soundAlarmTime); // debug*
  • bAlarmTwo = false;*
  • }*
  • }*
  • //----------------------------------------------------------------------------------*
  • /// MICROPHONE 3*
  • if (soundDetectValThree == LOW) { // If we hear a sound*
  • lastSoundDetectTimeThree = millis();*
  • // The following is so you don’t scroll on the output screen.*
  • if (!bAlarmThree) {*
  • Serial.println("-------------------------");*
  • Serial.println("sound detected on mike: ");*
  • Serial.println(microphoneArray[3]); //print the microphone number that heard the sound*
  • Serial.println(“LOUD, LOUD”);*
  • bAlarmThree = true;*
  • }*
  • }*
  • else {*
  • if ( ((millis() - lastSoundDetectTimeThree) > soundAlarmTime) && bAlarmThree) {*
  • Serial.println("-------------------------");*
  • Serial.println(“quiet”);*
  • Serial.println("Current time: ");*
  • Serial.println(millis()); // debug code: print vars*
  • Serial.println("Last time when sound was detected: ");*
  • Serial.println(lastSoundDetectTimeThree); // debug*
  • Serial.println("sound alarm Time set by user: ");*
  • Serial.println(soundAlarmTime); // debug*
  • bAlarmThree = false;*
  • }*
  • }*
  • //----------------------------------------------------------------------------------*
  • /// MICROPHONE 4*
  • if (soundDetectValFour == LOW) { // If we hear a sound*
  • lastSoundDetectTimeFour = millis();*
  • // The following is so you don’t scroll on the output screen.*
  • if (!bAlarmFour) {*
  • Serial.println("-------------------------");*
  • Serial.println("sound detected on mike: ");*
  • Serial.println(microphoneArray[4]); //print the microphone number that heard the sound*
  • Serial.println(“LOUD, LOUD”);*
  • bAlarmFour = true;*
  • }*
  • }*
  • else {*
  • if ( ((millis() - lastSoundDetectTimeFour) > soundAlarmTime) && bAlarmFour) {*
  • Serial.println("-------------------------");*
  • Serial.println(“quiet”);*
  • Serial.println("Current time: ");*
  • Serial.println(millis()); // debug code: print vars*
  • Serial.println("Last time when sound was detected: ");*
  • Serial.println(lastSoundDetectTimeFour); // debug*
  • Serial.println("sound alarm Time set by user: ");*
  • Serial.println(soundAlarmTime); // debug*
  • bAlarmFour = false;*
  • }*
  • }*
  • //----------------------------------------------------------------------------------*
  • /// Printing intermediate message between each run of void loop()*
  • Serial.println();*
  • Serial.println(“listening …”);*
  • Serial.println();*
  • delay(1000);*
    }

How to use this forum - please read

Please read that and learn how to post code right so it doesn’t turn into itallics.

Some issues:

int microphoneArray[] = {1, 2, 3, 4};         // We use 4 microphones

Your microphone pins seem to include pin 1. That’s one of the Serial pins and you’re already using Serial. You should avoid pins 0 and 1 when using Serial.

soundDetectValOne = digitalRead(microphoneArray[1]);  //read the sound alarm time mic 1

What type of microphones do you have? Most of them aren’t really digital devices.

You have a large section of code that is repeated 4 times with only different pin numbers. This could be made into a function or at least put into a for loop to make things a little easier to work with.

Delta_G:
Your microphone pins seem to include pin 1. That's one of the Serial pins and you're already using Serial. You should avoid pins 0 and 1 when using Serial.

He's put some (incorrect) #define statements just below that where the pins are defined. Or so it seems:

# define microphone 1 pin8

And this is illegal for a 4-element array:

Serial.println(microphoneArray[4]);

OP did you even try to compile this??

Thanks for the reply so far I will make a couple of mods and see how it goes, and yes my posted sketch
it compiled without problems

I can believe it. The defines are illegal but the word microphone doesn't come up in the code so they never get used and thus never got expanded. The other errors are all problems but not illegal lines. You can print past the end of an array you just can't be real sure what it is going to print.

Never realised a space between the # and the define is allowed :slight_smile: Anyway it may compile but it won't work (completely).
Writing outside an array tends to cause a crash on the ESP8266, I haven't tried this out on the ATmega/ATtiny processors.

C++ doesn't care about whitespace. Only newlines mean anything.

Delta_G:
C++ doesn't care about whitespace. Only newlines mean anything.

Not true.

This compiles:

# define CONSTANT 500
setup() {
 int var = CONSTANT;
}
loop() {}

This has less newlines but missing and surplus whitespace problems and won't compile:

#de fine CONSTANT500
setup() {intvar = CONSTANT;}
loop() {}

Ok had a go at making the correct #define without spaces but still not getting anywhere, question, Do I need to define the pin numbers that the mics are attached to?

washman:
Ok had a go at making the correct #define without spaces but still not getting anywhere, question, Do I need to define the pin numbers that the mics are attached to?

You should have some variable to hold those numbers but it doesn't have to be a #define.

The way your program is written you could simply remove the define lines and change this line:

int microphoneArray[] = {1, 2, 3, 4};

to have the right four pin numbers in it and then go change all your references to it from counting from 1 to 4 to counting from 0 to 3. Arrays elements are always counted from 0, so this array has elements microphoneArray[0], microphoneArray[1], microphoneArray[2], and microphoneArray[3].

But you still have to deal with the fact that microphones aren't really digital devices. I asked what sort of microphones you had. Why did you ignore that question? Don't you want help with this?

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

Delta_G Sorry I did not reply to your question re types of mics, was too focused on trying to understand the define and array declaration, I am not a programmer I’m a chemist.

I have two types of mics, thats what was available when I got them ideally I would have liked to get 4 of the same type but such is life. I have added three images two of the mics and the other of the set up I have no drawings and my had drawings suck.

Don't call your sound-detection boards microphones. That just confuses what everyone thinks you are using.

And if you could actually give a source and model number for the boards someone might be able to help you.

Paul

my had drawings suck

Even a drawing that sucks is better than having nothing at all. It's hard to debug something you can't see.

I am not a programmer I'm a chemist.

Me too! Hence the screen-name.

Hi Delta_G

Here is a hand drawing of the circuit.

Made a mistake on the drawing the sound detect modules are SF-SEN-12642 and XC4438, in the drawing I quoted the Tem/Humidity module

Sorry Guys I have not been in forum in years since my bike riding days on my SV. Here are some image inserted so no need to down load .

washman:
Hi Delta_G

Here is a hand drawing of the circuit.

Made a mistake on the drawing the sound detect modules are SF-SEN-12642 and XC4438, in the drawing I quoted the Tem/Humidity module

The Sparkfun board has 3 outputs:The Sound Detector not only provides an audio output, but also a binary indication of the presence of sound, and an analog representation of its amplitude.

You have the audio output going to an Arduino digital pin. You probably want the binary indication of the presence of a sound to go to the digital pin.
If you need the amplitude of the sound you need to feed the analog amplitude to an analog pin on the Arduino.

Paul

See, even with a crappy drawing it's a lot easier to find the problem than it is with no drawing.

Hi,
Diagrams of your two sound detectors


Tom... :slight_smile: