Advertisement
Guest User

Light Sensitive MIDI Controller (Arduino)

a guest
May 8th, 2013
881
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. int lightPin = 0;
  2. int potPin = 5;
  3.  
  4. int lightLED = 9;
  5. int potLED = 11;
  6.  
  7.  
  8. boolean pentatonic = true;
  9. boolean debug = false;
  10.  
  11. ////////////////////////////
  12.  
  13. int minLight;
  14. int maxLight;
  15. int lightLevel;
  16. int lightMap;
  17. int potMap;
  18. int toneMap;
  19. int count;
  20. int pot;
  21.  
  22.  
  23. void setup() {
  24.  
  25.   pinMode(potPin, OUTPUT);
  26.   if(debug) {
  27.     Serial.begin(9600); //correct rate fot debugging
  28.   }
  29.   else{
  30.     Serial.begin(31250); //correct rate for MIDI
  31.     pinMode(2, OUTPUT);
  32.  
  33.   }
  34.  
  35.   lightLevel = analogRead(lightPin);
  36.   minLight = lightLevel-20;
  37.   maxLight = lightLevel;
  38.   /*
  39.   for(int i=0x00; i < 77; i++) {
  40.     noteOn(0x80, i, 0x00);
  41.   }
  42.   */
  43.  
  44. }
  45.  
  46. void loop(){
  47.  
  48.   lightLevel = analogRead(lightPin);
  49.   pot = analogRead(potPin);
  50.   if(minLight > lightLevel) minLight = lightLevel;
  51.   if(maxLight < lightLevel) maxLight = lightLevel;
  52.  
  53.   lightMap = map(lightLevel, minLight, maxLight, 0, 255);
  54.   potMap = map(pot, 0, 1023, 0, 255);
  55.  
  56.   analogWrite(lightLED, potMap);
  57.   analogWrite(potLED, lightMap);
  58.  
  59.   if(debug) {
  60.     Serial.print("lightMap: ");
  61.     Serial.print(lightMap);
  62.     Serial.print(", potMap: ");
  63.     Serial.println(potMap);
  64.   }
  65.  
  66.  
  67.   lightMap = map(lightLevel, minLight, maxLight, 0,2);
  68.   potMap = map(pot, 0, 1023, 0, 5000);
  69.  
  70.   //                                          4 octaves from bottom 2 from top
  71.   toneMap = map(lightLevel, minLight, maxLight, 46, 104);
  72.  
  73.   if(pentatonic) {
  74.     int nn = toneMap;
  75.     while(nn >= 12) nn -= 12;
  76.     //0 C  1 C#  2 D  3 Eb  4 E  5 F  6 F#  7 G  8 Ab  9 A  10 Bb  11 B
  77.     //  C    -     D    -     E    -    -     G          A
  78.     if(nn == 1 || nn == 3 || nn == 5 || nn == 8 || nn == 10) toneMap--;
  79.     else if(nn == 6 || nn == 11) toneMap++;
  80.   }
  81.  
  82.  
  83.   float toneL = potMap*2;
  84.  
  85.   if(debug) {
  86.     Serial.print("Tone mapped: ");
  87.     Serial.println(toneMap);
  88.     Serial.print("Note delay: ");
  89.     Serial.println(toneL);
  90.   }
  91.   //tone(8, potMap, lightMap);
  92.   noteOn(0x90, toneMap, 0x45);
  93.   delay(toneL);
  94.   //noTone(8);
  95.   noteOn(0x80, toneMap, 0x00);
  96.  
  97. }
  98. //Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
  99. void noteOn(int cmd, int note, int velocity) {
  100.   if(debug) {
  101.     Serial.print("Note: ");
  102.     Serial.print(note);
  103.     Serial.print(", Velocity: ");
  104.     Serial.println(velocity);
  105.   }
  106.   else{
  107.     Serial.write(cmd);
  108.     Serial.write(note);
  109.     Serial.write(velocity);
  110.   }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement