This uses a ribbon controller and a pressure sensor to control an onboard DDS generating tones

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 DigitalOut myled(LED1);
00004 
00005 DigitalIn pushButton(p23);
00006 DigitalIn rotaryA(p21);
00007 DigitalIn rotaryB(p22);
00008 
00009 AnalogIn pressure(p20);
00010 AnalogIn pos1(p17);
00011 AnalogIn top(p16);
00012 AnalogIn bottom(p15);
00013 
00014 float wiper_f;
00015 float top_f;
00016 float bottom_f;
00017 float vol_f;
00018 float tmp_f;
00019 int tmp_i;
00020 
00021 float pressureAvg[4];
00022 float wiperAvg[4];
00023 
00024 AnalogOut signal(p18);
00025 
00026 Ticker sampleClock;
00027 // 16 entry sinewave table.
00028 char sineWave[]={127,175,216,244,255,244,216,175,127,78,37,9,0,9,36,78};
00029 
00030 char sawWave[]={0,16,31,47,64,79,95,111,127,143,159,175,191,207,223,255};
00031 
00032 unsigned int counter;
00033 unsigned  int counterSum;
00034 unsigned int counterLimit = 2147483647; //31 bits  2^31 - 1
00035 
00036 char temp;
00037 
00038 float byteToFloat(char in){
00039     float i = in;
00040     return (i/255.0)*0.5;
00041 }
00042 
00043 void sample2(){
00044     
00045 
00046 
00047 }
00048 
00049 void sample(){
00050  
00051     //useing a 31 bit counter.
00052     counter = counter + counterSum;
00053     if(counter > counterLimit){
00054         //wrap the counting.
00055         counter = counter - counterLimit;
00056     }
00057     
00058     //Fout = CounterSum*Fclk/MaxCount
00059     
00060     temp = counter>>27;  //shift the top 4 bits down.
00061     //float x = temp;
00062     //signal.write(x/16.0);
00063     
00064     //write the output out 
00065     signal.write(vol_f*byteToFloat(sineWave[temp]) +0.5);    
00066 }
00067 
00068 int read_encoder()
00069 {
00070   int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
00071   static char old_AB = 0;
00072   /**/
00073   old_AB <<= 2;
00074   char x  = rotaryA.read();
00075   x<<=1;
00076   x = x+rotaryB.read();                 //remember previous state
00077   old_AB |= ( x & 0x03 );  //add current state
00078   return ( enc_states[( old_AB & 0x0f )]);
00079 }
00080 
00081 int main() {
00082     
00083     //setup variables.
00084     tmp_i = 0;
00085     counter = 0;
00086     counterSum = 302365697;
00087     counterSum = 18897856;
00088     //counterSum = 1181116;
00089     //18897856.1
00090     
00091     vol_f = 0.0;
00092     
00093     //hook up sample() to the ticker.  run the counter at 50kHz
00094     sampleClock.attach_us(sample,20);
00095     
00096     while(1){
00097         wiper_f = pos1.read();
00098         bottom_f = bottom.read();
00099         
00100         if(wiper_f >= bottom_f){
00101             // wiper is being pressed, play a note
00102             top_f = top.read();
00103             
00104             tmp_f = 1.0 - (wiper_f - bottom_f)/(top_f - bottom_f);
00105             
00106             vol_f = 1.0 - pressure.read();
00107             //ribbon is about 3 octaves worth.
00108             //if the top is 440Hz = 18897856.1
00109             //44Hz = 1889785.61
00110             //55Hz is 2362232.013 //low A
00111             tmp_i = (int)(16535624*tmp_f);
00112             
00113             counterSum = 2362232+ tmp_i;
00114             
00115         }else{
00116             vol_f = 0.0;
00117         }
00118     
00119     
00120     }
00121 }