I suspect the problem is to do with the signal you are reading into the ADC. Reading an AC signal at 10ms intervals and expecting to get at least 15 good readings is going to be hit-and-miss at best.
My suggestion would be to ensure the audio is at least 2V peak to peak, using an amplifier if necessary, then rectify and filter it so you measure the envelope of the sound rather than individual cycles within it. In other words, the ADC sees a rise in voltage which follows the sound volume rather than trying to pick out individual cycles of the waveform.
Brian.
I can see a number of problems but first, explain what ADCBUF0 holds. For it to work I think it would have to hold the average number of samples in a set period. If it just accumulates the number of ADC readings, what makes the number drop again?
Another problem is the chance of your variables overflowing, you have no protection against them reaching maximum value and rolling over to zero or even overflowing to the next memory location.
I would be inclined to re-write the code so it is timer based rather than count based. In other words, enter the routine periodically instead of using delays in the loop to time the events. It makes it easier to produce running averages and hence detect the presence of signal to record and also makes it easier to create the longer delays needed to stop recording and produce the play control.
What processor is running this code?
Brian.
Thank you for the additional information.
You have a standard 10-bit resolution ADC with a reference voltage of 5V, in other words, 'number' x 0.048 = the input voltage.
There is a serious danger of the method you use to detect the audio not working. What you are doing is taking a single sample of the input voltage every 10mS. That sample represents just one part of the audio waveform and could easily be zero or very low on many samples, even if audio was present. It would be far better to do as I suggested with the rectifier and filter so you sample the average voltage level, it will make the audio detection far more reliable and give better immunity from triggering on interference. At its simplest you only need one diode, one capacitor and one resistor, total cost of $0.10
Going back to your original code, you might be able to fix it by adding another variable, one that is set when you record but stays set afterward. Then after detecting the end of the sound you say "I have made a recording and now there has been a silence so wait a while and operate the play function". I would still do it timer based though!
Brian.
Yes, as simple as that !
You also need to convert the AC signal to DC and provide a small 'reservoir' to hold the peak levels for a fraction of a second, this will ensure the ADC always sees something when audio is present and doesn't mistakenly sample the individual cycles when they are at or close to zero point. Doing this is very easy, all you need is a coupling capacitor from the transistor amplifier, a diode to change the AC to DC, a capacitor to hold the charge and a resistor to let it leak away slowly. The ADC measures the voltage across the capacitor holding the charge.
Brian.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?