Mini IR Theremin

Jump To:
Theory For Tone Generation
           With the IR Proximity theory clear in our minds, let's change gears and take a look at how to generate the beautiful tones of music that we all love. So, if you ever took physics, you probably learned that in our world, things move like waves. Water, Sound, Light, Air...all of it. The different speeds that they move and change at create different effects, with sound that is called pitch.
           Think about when you were young and watching ninjas fight. They swung their swords so fast they seemed to make noise. The faster the sword moved the louder the sound, well, same idea here. Except we will be dealing with generating different frequencies in electrical signals...not swords. Below is a list of the frequencies we are insterested in seeing.

 Note  Frequency (Hz) Wavelength (cm)
C4 261.63 132
D4 293.66 117
E4 329.63 105
F4 349.23 98.8
G4 392.00 88.0
A4 440.00 78.4
B4 493.88 69.9
C5 523.25 65.9

           Each one of these frequencies will be hard coded into the system to play whenever certain distances are detected by the IR Proximity sensors. However, the question remains is how will the timer be setup so that these tones can be played? The sequence of formula below shows you how I calculated the number of PIC instruction cycles necessary to reach different tones.

           Talking through how the above picture formulas worked, first we start off with our operating frequency: 20 MHz, which is actually 5 MHz because of the PIC's 4 clock cycle instruction cycle length. Then we look at the tone we want to generate, in this case it was C4 which is 261.63 Hz. When we Divide 261.63 by 5 MHz we get the number of PIC instruction cycles equal to the length of the tone.
           Remember, for half of the tone, the electrical signal should be +5v and the other half of the tone +0v. The next step we look at the timer value. I used a prescalar factor of 1 to 2, which means every instruction cycle tic actually counts as 2. So therefore, we divide the resultant value 19,110 in half. Now we have the true length for the timer, for C4. The problem is we need to change the signal back and forth between on and off. So we split the timer value in half once more, to a final value of 4777.
           When the PIC timer interrupts after counting to 4777, the speaker signal should reverse polarity. So if the current state is +0v, change to +5v and vice-versa. Doing this ensures a pure tone free of distortions.