Advertisement
Guest User

Swivel 2

a guest
Jun 23rd, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.41 KB | None | 0 0
  1.  
  2. //Fretter / Clamper information
  3. //This bit's only necessary because the servos are inconsistent from one to the next
  4. //Better
  5. //             INIT LOW HIGH        PITCHBEND LOW       PITCHBEND HI
  6. //String 1:(Low)57      47   55     1110            1800
  7. //String 2: 44  36  44      1150            1825
  8. //String 3: 47  43  47      1150            1850
  9. //String 4: 38  32  43      1140            1825
  10. //String 5: 62  56  62      1140            1825
  11. //String 6: 40  34  40      1200            1825
  12.  
  13.  
  14.  
  15.  
  16.  
  17. //Import MIDI library for arduino
  18. //see arduinomidilib.sourceforge.net/a00001.html‎
  19. #include <MIDI.h>
  20. #include <Servo.h>
  21.  
  22. //Servos go into corresponding slots on Swivel PCB
  23. Servo plucker; //servo 4
  24. Servo damper;  //servo 3
  25. Servo fretter; //servo 2
  26. Servo clamper; //servo 1
  27.  
  28. //diagnostic LEDs
  29. int ledPin1 = 2;
  30. int ledPin2 = 9;
  31. int ledPin3 = 10;
  32.  
  33. int channel = 0;
  34.  
  35. int fretSpeed = 20;
  36.  
  37. int pluckPos = 1; //pick state variable
  38. int clampPos = 1;
  39.  
  40. //Pins used to assign channel to each Swivel PCB
  41. int dipPins[] = {A0, A1, A2, A3};
  42.  
  43. void setup()
  44. {
  45.   channel = (1+setMidiChannelFromSwitch());
  46.   //Enable outputs, blink LED on powerup
  47.   pinMode(ledPin1, OUTPUT);
  48.   pinMode(ledPin2, OUTPUT);
  49.   pinMode(ledPin3, OUTPUT);  
  50.  
  51.   digitalWrite(ledPin1, HIGH);
  52.   delay(1000);
  53.   digitalWrite(ledPin1,LOW);
  54.  
  55.   //set up hexadecimal switch
  56.   int i;
  57.   for(i = 0; i<=3; i++){
  58.     pinMode(dipPins[i], INPUT);
  59.   }
  60.   delay(100);
  61.  
  62.  
  63.   //read switch, set its value to the device's MIDI channel
  64.   MIDI.begin(channel);
  65.  
  66.   for(int n = 0; n < 16; n++){
  67.     if(n == channel){
  68.         plucker.attach(8);
  69.         plucker.write(125);
  70.  
  71.         damper.attach(7);
  72.         damper.write(80);
  73.  
  74.         fretter.attach(6);
  75.         fretter.writeMicroseconds(1500);  
  76.  
  77.         clamper.attach(5);
  78.         //5 = 65  //lowest = 58
  79.         clamper.write(40);
  80.         digitalWrite(ledPin2, HIGH);
  81.         switch(n){
  82.         case 1 :
  83.           delay(2500);
  84.           break;
  85.         case 2 :
  86.           delay(2000);
  87.           break;
  88.         case 3 :
  89.           delay(1500);
  90.           break;
  91.         case 4 :
  92.           delay(1000);
  93.           break;
  94.         case 5 :
  95.           delay(500);
  96.           break;
  97.         case 6 :
  98.           delay(10);
  99.           break;          
  100.         }        
  101.     }
  102.     else{
  103.       digitalWrite(ledPin1, HIGH);
  104.       digitalWrite(ledPin2, HIGH);      
  105.       delay(100);
  106.       digitalWrite(ledPin1,LOW);
  107.       digitalWrite(ledPin2, LOW);            
  108.     }
  109.   }
  110.   digitalWrite(ledPin2, LOW);                  
  111.   //enable MIDI handlers
  112.   MIDI.setHandleControlChange(midiCCHandler);
  113.   MIDI.setHandlePitchBend(pitchBendHandler);  
  114. }
  115.  
  116. //Keep this loop simple and free of delays; MIDI is read in the loop
  117. void loop()
  118. {
  119.   MIDI.read();
  120. }
  121.  
  122. //Create Address from DIP Switch (4 positions used)
  123. byte setMidiChannelFromSwitch(){
  124.   int i,j=0;
  125.  
  126.   //Get the switch state
  127.   for(i=0; i<=3; i++){
  128.   j = (j << 1) | digitalRead(dipPins[i]);   // read the input pin
  129.   }
  130.    
  131.   return j; //return address
  132. }
  133.  
  134. //Note picking, clamping, and damping are handled upon receipt of CC messages
  135. //CC 7 = picking, CC 8 = clamping, CC 9 damping
  136. void midiCCHandler(byte channel, byte number, byte value){
  137.   if((unsigned int)number == 7 && (unsigned int)value != 0){
  138.     pluck();    
  139.   }
  140.   else if((unsigned int)number == 8 && (unsigned int)value != 0){
  141.     int rawClamp = map(value,0,127,34,40);
  142.     clamper.write(rawClamp);
  143.   }
  144.   else if((unsigned int)number == 9 && (unsigned int)value != 0){
  145.     undampString();
  146.   }
  147.   else if((unsigned int)number == 9 && (unsigned int)value == 0){
  148.     dampString();
  149.   }  
  150. }
  151.  
  152. //Pitch bend controls fretter location. Its range is mapped to the fretter's range.
  153. void pitchBendHandler(byte channel, int bend){
  154.   digitalWrite(ledPin3, HIGH);              
  155.   int servoBend = map(bend,-8192,8191,1200,1825);
  156.   fretter.writeMicroseconds(servoBend);
  157.   digitalWrite(ledPin3, LOW);                
  158. }
  159.  
  160. //This function alternates between plucking down and plucking up on the string
  161. void pluck(){
  162.   digitalWrite(ledPin2, HIGH);        
  163.   if(pluckPos == 1){
  164.     plucker.write(65);
  165.     pluckPos = 2;    
  166.   }
  167.   else if(pluckPos == 2){
  168.     plucker.write(95);
  169.     pluckPos = 1;    
  170.   }
  171.   digitalWrite(ledPin2, LOW);          
  172. }
  173.  
  174. //The damper pushes up on the string to damp it.
  175. void dampString(){
  176.   digitalWrite(ledPin1, HIGH);          
  177.   damper.write(72);
  178. }
  179.  
  180. void undampString(){
  181.   digitalWrite(ledPin1, LOW);              
  182.   damper.write(80);  
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement