|Self Powered mini speaker|
ideal if you want to listen to it and charge it at the same time but still, quite portable.
So back to the testing. Well the most important part is to have everything ready. I am using a cheap mp4 player as one of the input and my phone as the other. Because I am short of a some male-to-female header jumper cables I am using a breadboard and some tricky use of header pins ( I do have some male-male and female-female header jumper cables). Here is the full setup:
|The full testing setup|
|Test setup closeup|
Here is the sketch.
The code is rather simple but I will go over the major parts of it.
The 2 functions at the end are just for switching between the 2 sources; ListenToPhone() and ListenToSound(). That being said, you should notice that at no time will there be no input selected. This is to ensure that the value being read fluctuates as little as possible. If there is no input selected than the sensorValue would be 0 (or below 2 taking into account stray currents).
int phonePin = 6;
//always on source
int soundPin = 7;
These are my control pins for each channel, 6 left side and 7 right side.
int sensorPin = A1;
I am only monitoring the left side so I only have one input pin setup, analog pin 1.
int qvalue = 45;
This variable holds the nominal value of the reading. So the sensing value will be something like this:
- 0 (less than 2 to be exact) when there is not input or the monitored input is not selected
- around 52 when there is only the selectable input and there is no sound coming through it
- around 45 when there are 2 sources and there is no sound coming through the selectable input
int delta = 2;
This variable is very important, this will give you false positives if it is too low and will dismiss some sounds from the input if it is too high and you will end up not hearing the beginning of the selectable input. 2 is a value that works for me; from what I saw the variance is somewhere around 1,2 and rarely 3. I want to make sure I don't miss anything that is why I set it to 2.
int IncomingSound = 0;
int LastHigh = 99;
int MaxSilence = 20;
IncomingSound is used to signal that there should be sound comming from the selectable input. It is used as a boolean values so only 0 and 1 for this variable. LastHigh is counting the number of ticks (the number of time the loop() function has been called) since the last sensor reading was considered as positive (sound was coming from that input). MaxSilance keeps the number of ticks to keep the selectable input selected before going back to the default sound source when there has been no more positives sensor readings. The loop() function has a delay of 100 milliseconds so a value of 20 for the MaxSilence would give you approximately 2 seconds of silence before going back to the default source.
Looking at the loop() function, it basically does the following:
- checks to see if the acceptable period of silence has passed so that it may mark it as no sound coming from the selectable input;
- keeps the LastHigh variable to a maximum value of 100;
- reads the monitoring pin;
- mark as a high value if the sensor read more than 2 (meaning there is some input) and the reading deviates from the normal reading (qvalue) by more than the delta: the modulus of (qvalue - sensorValue) >= delta;
- and last, based on the boolean value of IncomingSound set the correct input source.
I left in the debug code that outputs to the serial port the values read so that it will be easier to note down the values you get when you have no sources, one source, 2 sources and different volume levels.
Have fun with this and try to improve my sloppy work!