Go Down

Topic: Help With A Sound Detection Sensor (Read 1 time) previous topic - next topic

mattnelson521

Im working on a project that uses a LM393 Sound Detection Sensor. What i want to happen is the module will detect when im talking but once im done it will play my audio file automatically. I should have everything wired correctly i think im just running into a coding issue.

What is currently happening is when i talk and it detects my voice it will automatically play the audio file. It wont wait until im done talking.

Here is my code and some photos.

Code: [Select]
const int buttonPinStatic = 7;   // Static

// variables will change:
int buttonState = HIGH;         // variable for reading the pushbutton status

void setup() {
 
   // initialize the button pin as a input:
pinMode(buttonPinStatic, INPUT);

// initialize serial communication:
   Serial.begin(9600);
  

  
   Serial.write(0x7E);
   Serial.write(0x03);
   Serial.write(0xA7);
   Serial.write(0x1F); //  volume max
   Serial.write(0x7E);
  
   // start sound
   Serial.write(0x7E);
   Serial.write(0x04);
   Serial.write(0xA0); // A0 for SD card
   Serial.write((byte)0x00);
   Serial.write(0x02); // track number
   Serial.write(0x7E);
   delay(3000);
    
   }
  
   void loop()
{
 
   Serial.write(0x7E);
   Serial.write(0x02); //STOP Sound
   Serial.write(0xA4);
   Serial.write(0x7E);                    
  
 buttonState = digitalRead(buttonPinStatic);
if (buttonState == HIGH) {

   Serial.write(0x7E);
   Serial.write(0x04);
   Serial.write(0xA0); // A0 for SD card
   Serial.write((byte)0x00);
   Serial.write(0x01); // track number
   Serial.write(0x7E);
delay(1000);
}
  
         }


PieterP

It wont wait until im done talking.
Of course not, you don't even check the input from the sound sensor.

Use something like:
Code: [Select]
#define CALIBRATION 512 // try and change until it works

[...]

int volume;
do {
  int val1 = analogRead(A0);
  delay(100);
  int val2 = analogRead(A0);
  volume = (val1 + val2) / 2;
} while (volume>CALIBRATION); // wait until average volume drops (i.e. you stop speaking)

[...]


A quick thought though: the sound from the speaker will be picked up by the microphone, so how will it know the difference between your voice and the audio file?

mattnelson521

Thank you for getting back to me.

So i tried what you said and adjusted the 512. I did both 400 and 600 with it still doing the same thing. im kinda new to this so im not sure how far i should be adjusting it?

Below is the code with the stuff suggested.


Code: [Select]
const int buttonPinStatic = 7;   // Static

// variables will change:
int buttonState = HIGH;         // variable for reading the pushbutton status

#define CALIBRATION 512 // try and change until it works

void setup() {
 
   // initialize the button pin as a input:
pinMode(buttonPinStatic, INPUT);

// initialize serial communication:
   Serial.begin(9600);
   
int volume;
do {
  int val1 = analogRead(A0);
  delay(100);
  int val2 = analogRead(A0);
  volume = (val1 + val2) / 2;
} while (volume>CALIBRATION); // wait until average volume drops (i.e. you stop speaking)

   
   Serial.write(0x7E);
   Serial.write(0x03);
   Serial.write(0xA7);
   Serial.write(0x1F); //  volume max
   Serial.write(0x7E);
   
   // start sound
   Serial.write(0x7E);
   Serial.write(0x04);
   Serial.write(0xA0); // A0 for SD card
   Serial.write((byte)0x00);
   Serial.write(0x02); // track number
   Serial.write(0x7E);
   delay(3000);
   
   }
   
   void loop()
{
 
   Serial.write(0x7E);
   Serial.write(0x02); //STOP Sound
   Serial.write(0xA4);
   Serial.write(0x7E);                     
   
 buttonState = digitalRead(buttonPinStatic);
if (buttonState == HIGH) {

   Serial.write(0x7E);
   Serial.write(0x04);
   Serial.write(0xA0); // A0 for SD card
   Serial.write((byte)0x00);
   Serial.write(0x01); // track number
   Serial.write(0x7E);
delay(1000);
}

 
         }

TomGeorge

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

You pictures are good, but tell us nothing about what is connected to what.

What is connected to A0.
I hope you are not expecting an analog output from the sound sensor?
It is a digital output, it goes HIGH when the sound received is higher than the threshold set by the pot on the board.

Do you have a DMM to measure the voltage level at A0.

Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

mattnelson521

I just put this together in paint. I have used this same layout and almost coding for a normal push button switch and it worked fine. so im kind of trying to make it work with the Sound Detector module so i dont have to use a push button switch.

Im not using A0. Just D7 and TX1




TomGeorge

Hi,
Okay, but why have you got a 10K in the gnd circuit of the sound detector?

Tom..... :)
Everything runs on smoke, let the smoke out, it stops running....

TomGeorge

Hi,
Quote
Im not using A0
But you last code does, A0 is open circuit at the moment so it could have any voltage level on it.

I suggest you connect A0 pin to gnd so it has a constant 0 level.

Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

OldSteve

#7
Aug 27, 2016, 10:53 am Last Edit: Aug 27, 2016, 10:57 am by OldSteve
Hi,But you last code does, A0 is open circuit at the moment so it could have any voltage level on it.
I suggest you connect A0 pin to gnd so it has a constant 0 level.
Tom... :)
The A0 and 'analogRead()' stuff was due to bad advice in the first reply. :(

@mattnelson521, forget the 'analogRead()' stuff.

First I have to say, the pin and variable names make your code very confusing to read.
I assume that by "buttonPinStatic" you mean the sound sensor pin? Maybe a better name like "sndSensorPin"?

Anyway, you wait for the sensor to indicate that it's detecting sound, then immediately play the audio file, so of course it will play while you're still talking.
N.B. I'm referring to your originally posted code, not that in reply #2, (which is messed up).

You need to wait until it goes low again, then play the file. Also, if when talking you speak multiple words, or syllables with quite moments in between, you need to allow some time after it goes low, while continuously checking it to see if it went high again, so the audio file doesn't start to play between words.

Break it down into small steps:-

1. Sound detected - flag the event and start a short 'millis()'-based timing period.

2. During the timed period, continually check the pin connected to the sound sensor, re-starting the timing if the pin is high.

3. At the end of the timed period, if the sensor pin is low, play the audio file.

Make the timed period just long enough to bridge any gaps between words/syllables, then the audio file will play when the timed period ends after the last sound is detected.


Also, you need to ensure that the sound sensor doesn't pick up the sound of the playing audio file and start the sequence again, if retriggering is possible.

I have no idea what you're using to play the audio track, but does it let you know when a track has finished playing? (Set a pin high, for example.)
Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

PieterP

The A0 and 'analogRead()' stuff was due to bad advice in the first reply. :(
Indeed, I'm sorry, the 'buttonPinStatic' made me think it was some kind of alternative button to turn off the sound, and I wrongly assumed it was a sound level indicator with an analog output. My bad.

OldSteve's advice looks like the best solution.

OldSteve

Indeed, I'm sorry, the 'buttonPinStatic' made me think it was some kind of alternative button to turn off the sound, and I wrongly assumed it was a sound level indicator with an analog output. My bad.

OldSteve's advice looks like the best solution.
I agree, the names are misleading.

No harm done.  :)

There's a fair chance that I made a mistake too, when I said "I have no idea what you're using to play the audio track, but does it let you know when a track has finished playing? (Set a pin high, for example.)"

Since it has a serial interface, chances are that it doesn't have a pin to indicate when a track has finished playing.
(It would make things easier if it did, though.)

I'm still concerned about whether or not it's retriggerable. If so, a delay would need to be implemented so that the audio track doesn't restart if another sound is detected by the sound sensor module.
Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

mattnelson521




So maybe a little backstory on this. The circuit and everything is from a trigger switch setup that i used in my Han Solo blaster. Which is where the 10K resistor came from. Off that setup.

My thoughts on this was rework it for this project. To be able to play static at the end of me talking like Walkie Talkies.

 So the Audio track is only a quick burst of static.

The mic and speakers shouldn't be picking each other up since they will be placed apart from each other. and the sound detection module will be next to my mouth. I just have the setup from the above photos for working on it.

My MP3 decoder is WT5001M02-28P U-disk as well just in case.

I have removed the resistor and named that stuff as suggested i also changed the buttonState to LOW.

What im not sure about is how to code all this. for example how to set the timed period of how long the sensor pin is low before it plays. That way it wont play between words and syllables.

 Im only a couple month into working with Arduino and coding is not really my strong point yet. sorry guys.

Here is a updated code. I also changed the buttonstate to LOW

Code: [Select]
const int sndSensorPin = 7; 

// variables will change:
int buttonState = LOW;         // variable for reading the sndSensorPin status


void setup() {
 
   // initialize the button pin as a input:
pinMode(sndSensorPin, INPUT);

// initialize serial communication:
   Serial.begin(9600);
 
   
   Serial.write(0x7E);
   Serial.write(0x03);
   Serial.write(0xA7);
   Serial.write(0x1F); //  volume max
   Serial.write(0x7E);
   
   // start sound
   Serial.write(0x7E);
   Serial.write(0x04);
   Serial.write(0xA0); // A0 for SD card
   Serial.write((byte)0x00);
   Serial.write(0x02); // track number
   Serial.write(0x7E);
   delay(3000);
   
   }
   
   void loop()
{                   
   
 buttonState = digitalRead(sndSensorPin);
if (buttonState == LOW) {

   Serial.write(0x7E);
   Serial.write(0x04);
   Serial.write(0xA0); // A0 for SD card
   Serial.write((byte)0x00);
   Serial.write(0x01); // track number
   Serial.write(0x7E);
delay(1000);
}

 
         }

OldSteve

#11
Aug 28, 2016, 05:16 am Last Edit: Aug 28, 2016, 05:17 am by OldSteve
I'm sort of surprised that when you changed the pin name to "sndSensorPin", you left the name of the pin state as "buttonState". It's a good idea to make all names as relevant to their purpose as possible. Since it's not a button, it's not a button state. It's a sound sensor pin state. So maybe "sndSnsPinState" or similar, so that at a glance you can immediately tell the purpose from the name.
This will become particularly important when you start writing longer programs.

End of lecture, back to the job at hand:-
The first thing you need to do is work out the method, or go by the method I outlined in reply #7.
Then, if there's something that you don't know how to do, research it and learn how. Write a short test program, (or two if necessary), and practice/perfect the method, then apply it to your main program.

So in the case of setting up a 'millis()'-based timing period, you could check out the "BlinkWithoutDelay" example provided with the IDE, under ">File >Examples >Digital", then you could follow up with Demonstration code for several things at the same time.
By the time you've studied those and wrote written your own test code, you should know how to work with 'millis()'

As your revised code stands, you check to see if there's no sound detected by the sensor, then immediately play the audio track. That's not what you want to happen. You have to think logically about it, and plan the steps needed as if you were going to do it manually, then write code to suit.

So you do want the audio track to play when the pin goes LOW, but not until after it's been HIGH. (Called "state change detection".) And not until it's been low for more than a pre-determined time. Hence the short timed period. And that period must be timed using 'millis()', because you need to check the pin during that period, which is impossible if you use 'delay()'.

Read through the examples I mentioned, study them and practice a little, then follow the procedure I mentioned above and in reply #7.

You still didn't mention if there's any way for your MP3 player to indicate when a track has finished playing. Is there?
Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

mattnelson521


Thank you ill keep that in mind about the names and ill do some research on that stuff as well and see what i can come up with. Honestly this stuff seems so foreign to me that it might be better to just watching some tutorials and learn my A B C i guess be for trying to tackle this project. I kind of thought i could jump right into it without taking those baby steps.

As for the MP3 Player i didn't think there is anything that will allow it to indicate when a track is finished. I did  have a line of code that would automatically stop the MP3 once it was finished playing but i took that out. Originally the Han Solo blaster code i was using this for the MP3 would just keep repeating it self when i would pull the trigger. But now its not when i added in the Sound Detection Sensor. so i didn't think it would be necessary to keep the code in. I was trying to keep it as simple as possible.

OldSteve

Thank you ill keep that in mind about the names and ill do some research on that stuff as well and see what i can come up with. Honestly this stuff seems so foreign to me that it might be better to just watching some tutorials and learn my A B C i guess be for trying to tackle this project. I kind of thought i could jump right into it without taking those baby steps.

As for the MP3 Player i didn't think there is anything that will allow it to indicate when a track is finished. I did  have a line of code that would automatically stop the MP3 once it was finished playing but i took that out. Originally the Han Solo blaster code i was using this for the MP3 would just keep repeating it self when i would pull the trigger. But now its not when i added in the Sound Detection Sensor. so i didn't think it would be necessary to keep the code in. I was trying to keep it as simple as possible.
Right. So if you send a second "play" message while a track is playing, it's ignored?
Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

Go Up