Dependencies:   mbed

Committer:
oczeides
Date:
Thu Nov 26 13:07:50 2009 +0000
Revision:
0:835b0c5a6f94

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
oczeides 0:835b0c5a6f94 1 //----------------------------------------------------------------------------------------
oczeides 0:835b0c5a6f94 2 // Project: MIDIExpressionPedal
oczeides 0:835b0c5a6f94 3 // Function: use the mbed board to convert a potentiometr into a MIDI expression pedal
oczeides 0:835b0c5a6f94 4 // Author: Dr.-Ing. O.C. Zeides, using some code from the Cookbook
oczeides 0:835b0c5a6f94 5 // Version: 26.11.2009
oczeides 0:835b0c5a6f94 6 //----------------------------------------------------------------------------------------
oczeides 0:835b0c5a6f94 7
oczeides 0:835b0c5a6f94 8 #include "mbed.h"
oczeides 0:835b0c5a6f94 9 #include "MIDI.h"
oczeides 0:835b0c5a6f94 10
oczeides 0:835b0c5a6f94 11 MIDI midi(p13, p14); // our MIDI communication channel
oczeides 0:835b0c5a6f94 12 DigitalOut myled(LED1); // status LED, should flash if we update the controller
oczeides 0:835b0c5a6f94 13 AnalogIn potentiometer(p20); // here is where we connect our potentiometer
oczeides 0:835b0c5a6f94 14
oczeides 0:835b0c5a6f94 15 float last_pot = 0.0; // memorize the old potentiometer value
oczeides 0:835b0c5a6f94 16 float actual_pot; // actual poentiometer value
oczeides 0:835b0c5a6f94 17 float hysterese = 0.1; // only send new message if we have moved the potentiometer that far
oczeides 0:835b0c5a6f94 18 float fvalue = 0.0;
oczeides 0:835b0c5a6f94 19
oczeides 0:835b0c5a6f94 20 int controllerNo = 1; // MIDI controller No
oczeides 0:835b0c5a6f94 21 int channelNo = 1; // MIDI channel No.
oczeides 0:835b0c5a6f94 22 int value = 0; // MIDI value for controller
oczeides 0:835b0c5a6f94 23
oczeides 0:835b0c5a6f94 24 // send the new value to the controller
oczeides 0:835b0c5a6f94 25 void updateMIDIController(void)
oczeides 0:835b0c5a6f94 26 {
oczeides 0:835b0c5a6f94 27 myled = 1;
oczeides 0:835b0c5a6f94 28 fvalue = actual_pot * 127.0;
oczeides 0:835b0c5a6f94 29 value = (int) fvalue;
oczeides 0:835b0c5a6f94 30 midi.controller(controllerNo,value,channelNo);
oczeides 0:835b0c5a6f94 31 wait(0.5);
oczeides 0:835b0c5a6f94 32 myled = 0;
oczeides 0:835b0c5a6f94 33 } // updateMIDIController()
oczeides 0:835b0c5a6f94 34
oczeides 0:835b0c5a6f94 35 int main() {
oczeides 0:835b0c5a6f94 36
oczeides 0:835b0c5a6f94 37 while(1) { // do forever
oczeides 0:835b0c5a6f94 38 // get new pot value
oczeides 0:835b0c5a6f94 39 actual_pot = potentiometer;
oczeides 0:835b0c5a6f94 40 // compare with last pot value
oczeides 0:835b0c5a6f94 41 if(actual_pot > (last_pot + hysterese))
oczeides 0:835b0c5a6f94 42 updateMIDIController();
oczeides 0:835b0c5a6f94 43 if(actual_pot < (last_pot - hysterese))
oczeides 0:835b0c5a6f94 44 updateMIDIController();
oczeides 0:835b0c5a6f94 45
oczeides 0:835b0c5a6f94 46 last_pot = actual_pot;
oczeides 0:835b0c5a6f94 47
oczeides 0:835b0c5a6f94 48 } // while
oczeides 0:835b0c5a6f94 49
oczeides 0:835b0c5a6f94 50 } // main()
oczeides 0:835b0c5a6f94 51
oczeides 0:835b0c5a6f94 52
oczeides 0:835b0c5a6f94 53