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);*
}