Monday, February 29, 2016

Sound Module, Rollover Switches, and RC Time Constants




Over the weekend I experimented with one of my options for sound. The MOL-AU5121 .mp3 module from MDFLY. I've been hard pressed to find a good example for designing a .wav player, or even one that was easily addressable from an outside source, but the board from MDFLY was an easy to use option that is really tempting to stick with. The module reads serial data in, and plays MP3 files stored on the sd card mounted to the back of the device. The documentation for this thing is wrong on at least a couple of things I have found. It states the Baud rate for the SR-232 is 4800 when in reality it's 9600. It also states that the addresses of the files are dependent on what you name them ie. address "01" = "001.mp3". This is however, not true. The addresses for the individual .mp3's depend on the order they are loaded onto the sd card. I'll give them that if were to name them all that way before hand and then load them all at the same time, this should work fine, but if you saved 001.mp3 then 003.mp3 then realized you forgot about 002.mp3, then 002.mp3 would be addressed with "03". There are programs out there that let you specify what order files are loaded onto the cards, so I will have to look into that. I'm thinking I'm going to need three or four of these modules chained together to be able to play multiple sound files at once. One for dialogue, one for music, and one or two for various sound effects. I will have to program in some sort of hierarchy for the audio files to make sure the more important files can interrupt and not be disturbed by less important sounds. 

I ripped the audio in the video from the actual TV show. Unfortunately I got one where Walter says "engaged" instead of "encoded". I have started keeping a notepad file open and recording when and what is said on the show as well as various sound effects I want to go back and take later. I love that research for this includes watching my favorite show.

The switch I'm using is an old leaf type rollover switch used in older pins. I dont think I will be using these. They take up too much room on the bottom of the play field and are also comprised of two separate parts. A more modern switch uses the same curved wire actuator but instead of the leaf switch, it uses a micro switch.



Rollover switch slot from top of playfield.

Older, leaf type rollover switch pieces.

Modern micro switch type rollover switch.



When trying to test this thing using an Arduino, I was having problems with the ball rolling over the switch too fast and the code not catching the signal fast enough. This seamed very odd to me. How could I see the switch close faster than a 16MHz processor? Short answer, I can't. when I was programming the code I tried to get the state of my input inside my if statement instead of storing the state of the input into a variable and checking the variable inside of my if statement instead.


if(digitalRead(input)){ 
doStuff();
}

instead of:

bool switchState = digitalRead();
if(switchState){
doStuff();
}

I'm really not sure why it should make a difference at all, but I do know for sure that this is what fixed the problem. I had also thought about using a capacitor  to temporarily store the 5v signal and release it slowly through to the input. I did try this too, I even thought it was working, but it didn't. Out of curiosity, I used my scope on the input and saw that the cap was draining just as rapidly as it was charging. Maybe there isn't enough internal resistance on the input? I will have to try this again later with a 1k resistor or similar going into the input from the cap. 


The 10K resistor is just to keep the input at a logic level low until the button is pressed. A 1uF capacitor draining through a 1K resistor should keep my input around VCC for about 1 second. What I have just made here is what is known as an RC circuit. Resistor, Capacitor circuit. Capacitors charge and discharge in what is referred to as a time constant. Or rather, 5 time constants. The capacitor will be ~63.2% charged after just a single time constant passes. It then takes 4 more time constants for the other 36.8% to charge. So the first one is usually the most important. Calculating the time constant of an RC circuit is easy. Capacitance multiplied by Resistance. So 1uF is .001F and 1K ohms is 1000 ohms. .00 * 1000 = 1 second. So the time constant is 1 second. So after the button is depressed the cap will take one full second to lose 63.2% of its charge holding the input high for that whole time. It is only a function of the 1K that makes our cap drain so slow. Charging the cap is almost instantaneous. When the button is pressed, the capacitor has no resistance to the current charging it. Bad idea? Probably but what do I know. So lets get the time constant real quick for charging. .001F * 0 Ohms = 0 Seconds. Yup, theoretically that capacitor will charge instantly.

Here's some more information on RC circuits and Time Constants.