Advertisement
Josueco

Proyecto

Jun 11th, 2015
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.95 KB | None | 0 0
  1. /********** Proyecto ***********/
  2. const int notes=6;          // set up number of notes
  3.  
  4. boolean notePlaying[notes];  //note states array: keeps track of whether a MIDI note is "on" or "off"
  5.  
  6. int scale[notes]={0,2,4,5,7,9};       //major scale
  7.  
  8. int beginNote=0x3C;                                  //starting note value (default middle C)
  9.  
  10. int threshold=512;                                //value which analogRead() must cross for a midi note to be triggered
  11.  
  12. boolean midiMode=false;                          //set this to "true" before uploading MIDI firmware (http://hunt.net.nz/users/darran/weblog/52882/Arduino_UNO_MIDI_USB_version_02.html) to 16u2
  13.  
  14.  
  15. void setup(){
  16.   if(midiMode){
  17.     Serial.begin(115200);           //midi USB baud rate = 115200
  18.   }else{
  19.     Serial.begin(9600);           //for testing values
  20.   }
  21. }
  22.  
  23. void loop() {
  24.   for(int i=0;i<notes;i++){  
  25.    
  26.     if((analogRead(i)<threshold)&&(notePlaying[i]==false)){        //note on & CC messages
  27.       int note=beginNote+scale[i];
  28.       midiMessage(0x90, 1, note, 100);                          
  29.       notePlaying[i]=true;                                              
  30.     }
  31.    
  32.     if((analogRead(i)>threshold)&&(notePlaying[i]==true)){        //note off messages
  33.       int note=beginNote+scale[i];
  34.       midiMessage(0x80, 1, note, 0x00);
  35.       notePlaying[i]=false;                                        
  36.     }
  37.   }
  38.  
  39.   //Serial.println(analogRead(A5));
  40.   //delay(400);
  41. }
  42.  
  43. void midiMessage(int commandByte, int channelByte, int data1Byte, int data2Byte) {
  44.   if(midiMode){
  45.     Serial.write(commandByte);
  46.     Serial.write(channelByte);
  47.     Serial.write(data1Byte);
  48.     Serial.write(data2Byte);
  49.   }else{
  50.     if(commandByte==0x90){
  51.       Serial.print("Note On message: note ");
  52.     }else if(commandByte==0x80){
  53.       Serial.print("Note Off message: note ");
  54.     }
  55.     Serial.print(data1Byte);
  56.     Serial.print(", velocity ");
  57.     Serial.println(data2Byte);
  58.   }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement