Code for controlling a servo with midi, mine doesn't work yet

Hi All!

I got it working :slight_smile:

I found some code for cc messages on notes and volts by dave. This I modified and I got it working :slight_smile:

Here a little movie I made:

Please find my working code here:

/*
   Adjusted code for Controlling the damper of a plate reverb with midi CC by Robin Hunt
   Original code found on www.notesandvolts.com by Dave
*/

#include <MIDI.h>  // Add Midi Library

#include <Servo.h> // Add Servo Library

// Define Servo
Servo PlateDamperServo;

//Create an instance of the library with default name, serial port and settings
MIDI_CREATE_DEFAULT_INSTANCE();

void setup() {
  MIDI.begin(9); // Initialize the Midi Library.
  // OMNI sets it to listen to all channels.. MIDI.begin(9) would set it
  // to respond to notes on channel 9 only.
  MIDI.setHandleControlChange(MyCCFunction); 
  // This command tells the MIDI Library
  // the function you want to call when a Continuous Controller command
  // is received. In this case it's "MyCCFunction".
   PlateDamperServo.attach(3);
  // Attach the servo to pin 3 on the Arduino
}

void loop() { // Main loop
  MIDI.read(); // Continuously check if Midi data has been received.
}

// MyCCFunction is the function that will be called by the Midi Library
// when a Continuous Controller message is received.
// It will be passed bytes for Channel, Controller Number, and Value
// The following code checks if the controller number is on 27
// If it is, the servo receive PWM and will move corresponding to the Value byte
void MyCCFunction(byte channel, byte number, byte value) {
  switch (number) {
    case 27:
    PlateDamperServo.attach(3);
      PlateDamperServo.write (map (value, 0, 255, 90, 180));
      // Delay for servo movement
  delay(30);
   PlateDamperServo.detach();
   delay(5);
      break;
  }
}