Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Fretter / Clamper information
- //This bit's only necessary because the servos are inconsistent from one to the next
- //Better
- // INIT LOW HIGH PITCHBEND LOW PITCHBEND HI
- //String 1:(Low)57 47 55 1110 1800
- //String 2: 44 36 44 1150 1825
- //String 3: 47 43 47 1150 1850
- //String 4: 38 32 43 1140 1825
- //String 5: 62 56 62 1140 1825
- //String 6: 40 34 40 1200 1825
- //Import MIDI library for arduino
- //see arduinomidilib.sourceforge.net/a00001.html
- #include <MIDI.h>
- #include <Servo.h>
- //Servos go into corresponding slots on Swivel PCB
- Servo plucker; //servo 4
- Servo damper; //servo 3
- Servo fretter; //servo 2
- Servo clamper; //servo 1
- //diagnostic LEDs
- int ledPin1 = 2;
- int ledPin2 = 9;
- int ledPin3 = 10;
- int channel = 0;
- int fretSpeed = 20;
- int pluckPos = 1; //pick state variable
- int clampPos = 1;
- //Pins used to assign channel to each Swivel PCB
- int dipPins[] = {A0, A1, A2, A3};
- void setup()
- {
- channel = (1+setMidiChannelFromSwitch());
- //Enable outputs, blink LED on powerup
- pinMode(ledPin1, OUTPUT);
- pinMode(ledPin2, OUTPUT);
- pinMode(ledPin3, OUTPUT);
- digitalWrite(ledPin1, HIGH);
- delay(1000);
- digitalWrite(ledPin1,LOW);
- //set up hexadecimal switch
- int i;
- for(i = 0; i<=3; i++){
- pinMode(dipPins[i], INPUT);
- }
- delay(100);
- //read switch, set its value to the device's MIDI channel
- MIDI.begin(channel);
- for(int n = 0; n < 16; n++){
- if(n == channel){
- plucker.attach(8);
- plucker.write(125);
- damper.attach(7);
- damper.write(80);
- fretter.attach(6);
- fretter.writeMicroseconds(1500);
- clamper.attach(5);
- //5 = 65 //lowest = 58
- clamper.write(40);
- digitalWrite(ledPin2, HIGH);
- switch(n){
- case 1 :
- delay(2500);
- break;
- case 2 :
- delay(2000);
- break;
- case 3 :
- delay(1500);
- break;
- case 4 :
- delay(1000);
- break;
- case 5 :
- delay(500);
- break;
- case 6 :
- delay(10);
- break;
- }
- }
- else{
- digitalWrite(ledPin1, HIGH);
- digitalWrite(ledPin2, HIGH);
- delay(100);
- digitalWrite(ledPin1,LOW);
- digitalWrite(ledPin2, LOW);
- }
- }
- digitalWrite(ledPin2, LOW);
- //enable MIDI handlers
- MIDI.setHandleControlChange(midiCCHandler);
- MIDI.setHandlePitchBend(pitchBendHandler);
- }
- //Keep this loop simple and free of delays; MIDI is read in the loop
- void loop()
- {
- MIDI.read();
- }
- //Create Address from DIP Switch (4 positions used)
- byte setMidiChannelFromSwitch(){
- int i,j=0;
- //Get the switch state
- for(i=0; i<=3; i++){
- j = (j << 1) | digitalRead(dipPins[i]); // read the input pin
- }
- return j; //return address
- }
- //Note picking, clamping, and damping are handled upon receipt of CC messages
- //CC 7 = picking, CC 8 = clamping, CC 9 damping
- void midiCCHandler(byte channel, byte number, byte value){
- if((unsigned int)number == 7 && (unsigned int)value != 0){
- pluck();
- }
- else if((unsigned int)number == 8 && (unsigned int)value != 0){
- int rawClamp = map(value,0,127,34,40);
- clamper.write(rawClamp);
- }
- else if((unsigned int)number == 9 && (unsigned int)value != 0){
- undampString();
- }
- else if((unsigned int)number == 9 && (unsigned int)value == 0){
- dampString();
- }
- }
- //Pitch bend controls fretter location. Its range is mapped to the fretter's range.
- void pitchBendHandler(byte channel, int bend){
- digitalWrite(ledPin3, HIGH);
- int servoBend = map(bend,-8192,8191,1200,1825);
- fretter.writeMicroseconds(servoBend);
- digitalWrite(ledPin3, LOW);
- }
- //This function alternates between plucking down and plucking up on the string
- void pluck(){
- digitalWrite(ledPin2, HIGH);
- if(pluckPos == 1){
- plucker.write(65);
- pluckPos = 2;
- }
- else if(pluckPos == 2){
- plucker.write(95);
- pluckPos = 1;
- }
- digitalWrite(ledPin2, LOW);
- }
- //The damper pushes up on the string to damp it.
- void dampString(){
- digitalWrite(ledPin1, HIGH);
- damper.write(72);
- }
- void undampString(){
- digitalWrite(ledPin1, LOW);
- damper.write(80);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement