Simple midi controller for Freedom KL25 board which generates tone (midi signal) according X-axis accelerometer. Modulation wheel is controlled by Y-axis and pitch wheel is controlled by touch slider

Dependencies:   MMA8451Q TSI USBDevice mbed

Fork of Midiudelator by Pavel M

Revision:
5:421d532c6403
Parent:
4:9c81faf1b372
--- a/main.cpp	Mon Aug 19 12:16:44 2013 +0000
+++ b/main.cpp	Tue Aug 20 20:37:42 2013 +0000
@@ -5,21 +5,38 @@
 
 #define MMA8451_I2C_ADDRESS (0x1d<<1)
 
-#define C1  0 
-#define D1  0.125
-#define E1  0.250
-#define F1  0.375
-#define G1  0.5
-#define A1  0.625
-#define H1  0.750
-#define C2  0.875
-#define D2  1
-#define E2  1.125
-#define F2  1.250
-#define G2  1.375
-#define A2  1.5
-#define H2  1.625
-#define C3  1.750
+#define C1  0,187  
+#define D1  0.250 
+#define E1  0.312 
+#define F1  0.375      
+#define G1  0.437 
+#define A1  0.5        
+#define B1  0.562 
+#define C2  0.625  
+#define D2  0.687      
+#define E2  0.750   
+#define F2  0.812     
+#define G2  0.875  
+#define A2  0.937       
+#define B2  1      
+#define C3  1.062       
+#define D3  1.125  
+#define E3  1,187       
+#define F3  1.250      
+#define G3  1.312       
+#define A3  1.375  
+#define B3  1.437       
+#define C4  1.5    
+#define D4  1.562       
+#define E4  1.625  
+#define F4  1.687 
+#define G4  1.749 
+#define A4  1.811 
+#define B4  1.873 
+
+//1.935
+
+
 
 #define NC1 48
 #define ND1 50
@@ -27,26 +44,28 @@
 #define NF1 53
 #define NG1 55
 #define NA1 57
-#define NH1 59
+#define NB1 59
 #define NC2 60
 #define ND2 62
 #define NE2 64
 #define NF2 65
 #define NG2 67
 #define NA2 69
-#define NH2 71
+#define NB2 71
 #define NC3 72
-
-/* tempo delays */
-#define EITH    5
-#define QUATER  25
-#define HALF    50
-#define FULL    75
-
-#define T_EITH    0.08  /* set the basic dealy here */
-#define T_QUATER  (T_EITH*2)
-#define T_HALF    (T_QUATER*2)
-#define T_FULL    (T_HALF*2)
+#define ND3 74
+#define NE3 76
+#define NF3 77
+#define NG3 79
+#define NA3 81
+#define NB3 83
+#define NC4 84
+#define ND4 86
+#define NE4 88
+#define NF4 89
+#define NG4 91
+#define NA4 93
+#define NB4 95
 
 Serial pc(USBTX,USBRX);
 USBMIDI midi;
@@ -76,11 +95,11 @@
  
 int main() { 
 
-    int note=48;
+    int note=48, n=48;
     float notechng;
-    float fpitch;
-    int tempo;
-    float pause = T_HALF;
+    int pitch;
+    int modulation;
+    float m;
     
     MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
              
@@ -88,34 +107,28 @@
      
     while (1) { 
 #if 0      
+        /* TEST CODE */
         /* test pitch - commented in in - endif*/
         midi.write(MIDIMessage::NoteOn(57));
-        for(int c=-8000; c<8000; c= c+100){  
+        for(int c=0; c<127; c= c+1){  
             
-            midi.write(MIDIMessage::PitchWheel(c));
+            midi.write(MIDIMessage::ControlChange(1, c));
+            //midi.write(MIDIMessage::PitchWheel(c));
             pc.printf("Pitch: %d \n", c);         
         }   
         midi.write(MIDIMessage::NoteOff(57));          
             wait(0.5);         
 #endif
-
+#if 1
         /* read note */
         notechng = 1 - acc.getAccX();
-        /* get pitch */
-        fpitch = acc.getAccY()*100;
-               
-        /* get tempo */
-        tempo =int(tsi.readPercentage() * 100);
+
+        /* get modulatio */
+        m = ((1 + acc.getAccY()) * 63); 
+        modulation = int(m); 
         
-        pause = T_EITH;
-        if(tempo > QUATER) pause = T_QUATER;
-        if(tempo > HALF) pause = T_HALF;
-        if(tempo > FULL) pause = T_FULL;        
-
-        pc.printf("pause: %f, note: %f, pitch: %d \n",pause, note, (int)fpitch*100); 
-        
-        midi.write(MIDIMessage::NoteOff(note));
-        wait(pause);
+        /* get pitch */
+        pitch = (int(tsi.readPercentage() * 8192) - 8192);
         
         if(notechng > C1) note= NC1;
         if(notechng > D1) note= ND1;
@@ -123,20 +136,42 @@
         if(notechng > F1) note= NF1;
         if(notechng > G1) note= NG1;
         if(notechng > A1) note= NA1;
-        if(notechng > H1) note= NH1;
+        if(notechng > B1) note= NB1;
         if(notechng > C2) note= NC2;
         if(notechng > D2) note= ND2;
         if(notechng > E2) note= NE2;
         if(notechng > F2) note= NF2;
         if(notechng > G2) note= NG2;
         if(notechng > A2) note= NA2;
-        if(notechng > H2) note= NH2;  
-        if(notechng > C3) note= NC3;          
+        if(notechng > B2) note= NB2;  
+        if(notechng > C3) note= NC3;
+        if(notechng > D3) note= ND3;
+//        if(notechng > E3) note= NE3;
+        if(notechng > F3) note= NF3;
+        if(notechng > G3) note= NG3;
+        if(notechng > A3) note= NA3;
+        if(notechng > B3) note= NB3;
+        if(notechng > C4) note= NC4;
+        if(notechng > D4) note= ND4;
+        if(notechng > E4) note= NE4;
+        if(notechng > F4) note= NF4;
+        if(notechng > G4) note= NG4;
+        if(notechng > A4) note= NA4;
+        if(notechng > B4) note= NB4; 
+        
+        pc.printf("note: %d \n", note);          
+        pc.printf("notechng: %f \n", notechng);
 
-        midi.write(MIDIMessage::NoteOn(note));
-        midi.write(MIDIMessage::PitchWheel(int(fpitch)));
-
-        wait(pause);
-      
+        if (note != n) {
+            midi.write(MIDIMessage::NoteOff(n));
+            n=note; 
+            midi.write(MIDIMessage::NoteOn(n));       
+        }
+        
+        midi.write(MIDIMessage::ControlChange(1, modulation));
+        midi.write(MIDIMessage::PitchWheel(pitch));
+        
+        wait(0.1);
+#endif      
     }
 }
\ No newline at end of file