Advertisement
Guest User

Arduino MIDI - ovinophobia blogspot

a guest
Dec 19th, 2012
7,485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.75 KB | None | 0 0
  1. // include MIDI library
  2. #include <MIDI.h>
  3.  
  4. // read digital pins number 2, 4, 7
  5. int pushButton2 = 2;
  6. int pushButton4 = 4;
  7. int pushButton7 = 7;
  8.  
  9. // checks if the button is pressed
  10. int buttonState2 = 0;
  11. int buttonState4 = 0;
  12. int buttonState7 = 0;
  13.  
  14. // play/stop notes in relation to buttons pressed
  15. int note2 = 0;
  16. int note4 = 0;
  17. int note7 = 0;
  18.  
  19. // read potentiometer value
  20. int analogValue = 0;
  21. // maximum MIDI value is 127, first/previous potentiometer
  22. // move has to always be different than previous value
  23. int lastAnalogValue = 128;
  24.  
  25. void setup() {
  26.   MIDI.begin(4);
  27.   // 115200 hairless MIDI
  28.   Serial.begin(115200);
  29.   pinMode(pushButton2, INPUT);
  30.   pinMode(pushButton4, INPUT);
  31.   pinMode(pushButton7, INPUT);
  32. }
  33.  
  34. void loop() {
  35.   // read state of buttons
  36.   int buttonState2 = digitalRead(pushButton2);
  37.   int buttonState4 = digitalRead(pushButton4);
  38.   int buttonState7 = digitalRead(pushButton7);
  39.  
  40.   // Analog potentiometer
  41.  
  42.   // potentiometer gives values up to 1023
  43.   // we need values not bigger than 127
  44.   int analogValue = analogRead(A0)/8;
  45.  
  46.   // potentiometer could be too sensitive and
  47.   // give different (+-1) values.
  48.   // send CC only when the difference between last value
  49.   // is more than 1
  50.   if ((analogValue-lastAnalogValue) > 1 || (analogValue-lastAnalogValue) < -1) {
  51.     // value changed?
  52.     if (analogValue != lastAnalogValue) {
  53.       // send serial value (ControlNumber 1, ControlValue = analogValue, Channel 1)
  54.       // more info: http://arduinomidilib.sourceforge.net/a00001.html
  55.       MIDI.sendControlChange(1, analogValue, 1);
  56.       lastAnalogValue = analogValue;
  57.     }
  58.   }
  59.  
  60.   // Button 2
  61.  
  62.   // when button pressed:
  63.   if (buttonState2 == HIGH) {
  64.     // if note not playing
  65.     if (note2 == 0) {
  66.       // play note (note number, velocity, channel)
  67.       // more info: http://arduinomidilib.sourceforge.net/a00001.html
  68.       // MIDI notes chart http://www.phys.unsw.edu.au/jw/notes.html
  69.       // 55 = G3, 127 = trigger note with max volume
  70.       MIDI.sendNoteOn(55,127,1);
  71.       // note is playing
  72.       note2 = 1;
  73.     }
  74.   // when button released
  75.   } else {
  76.     // if note playing
  77.     if (note2 == 1) {
  78.       // if playing - stop
  79.       MIDI.sendNoteOff(55,0,1);
  80.     }
  81.     // if button released note is off
  82.     note2 = 0;
  83.   }
  84.  
  85.   // Button 4
  86.   if (buttonState4 == HIGH) {
  87.     if (note4 == 0) {
  88.       MIDI.sendNoteOn(63,127,1);
  89.       note4 = 1;
  90.     }
  91.   } else {
  92.     if (note4 == 1) {
  93.       MIDI.sendNoteOff(63,0,1);
  94.     }
  95.     note4 = 0;
  96.   }
  97.  
  98.   // Button 7
  99.   if (buttonState7 == HIGH) {
  100.     if (note7 == 0) {
  101.       MIDI.sendNoteOn(60,127,1);
  102.       note7 = 1;
  103.     }
  104.   } else {
  105.     if (note7 == 1) {
  106.       MIDI.sendNoteOff(60,0,1);
  107.     }
  108.     note7 = 0;
  109.   }
  110.  
  111.   delay(1);
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement