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

**URL:** <https://forum.arduino.cc/t/code-for-controlling-a-servo-with-midi-mine-doesnt-work-yet/644455>\
**Category:** Programming\
**Created:** [March 25, 2020, 9:19pm UTC](https://forum.arduino.cc/t/code-for-controlling-a-servo-with-midi-mine-doesnt-work-yet/644455 "2020-03-25T21:19:11Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![robinhunt](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robinhunt/32/521173_2.png) [@robinhunt](https://forum.arduino.cc/u/robinhunt)\
**Post date:** [March 25, 2020, 9:19pm UTC](https://forum.arduino.cc/t/code-for-controlling-a-servo-with-midi-mine-doesnt-work-yet/644455/1 "2020-03-25T21:19:11Z")

</div>

Hi All,

As a newbie I am looking for some help with my project.

What I want is pretty simple, I just want to control a servo motor with midi cc messages. It would be cool if the servo only responds to a certain midi channel and with a certain midi cc number, so that it doesn't respond to all the midi messages on all channels / cc numbers. I made a code which I put down here.

To give some background info as well: I made the midi input circuit for the arduino (Uno) with the optocoupler and that works. I tested it by using some code which makes the arduino LED light up when playing the middle C. The servo motor I connected with the PWM to pin 9 and the grnd to grnd... The servo has an external power supply.

Please let me know if you need more info or if i need to research some essential stuff first .

Thanks again for helping me out with this little project!!

```cpp
#include <Servo.h>

#include <MIDI.h>

// Define Servo
Servo myservo;
// Define Value
int val;

MIDI_CREATE_DEFAULT_INSTANCE();

 void setup()
 {
    MIDI.begin(MIDI_CHANNEL_OMNI);  // Listen to All Midi input
      // Connect servo to pin 9
  myservo.attach(9);
  // Start Serial communication
  Serial.begin(9600);
}

 void loop()
 {

    // Read all incoming midi
    MIDI.read();
    // Connect midi with the Value parameter
  val = MIDI.read();
  // Calculate Midi values (0-127) to servo angle
  val = map(val, 0, 127, 0, 180);
 
  // Start serial monitoring
  Serial.println(val);
  // Send servo angle
  myservo.write(val);
  // Delay for servo movement
  delay(50);
 }

[code]

```

---

<div class="post-metadata">

**Author:** ![slipstick](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@slipstick](https://forum.arduino.cc/u/slipstick)\
**Post date:** [March 25, 2020, 11:04pm UTC](https://forum.arduino.cc/t/code-for-controlling-a-servo-with-midi-mine-doesnt-work-yet/644455/2 "2020-03-25T23:04:23Z")

</div>

You do need to investigate MIDI.h in a bit more detail. To get you started:

If you want data from a specific channel just change MIDI\_CHANNEL\_OMNI to the channel number.

MIDI.read() may be o.k. for switching an LED on but it only returns true or false not any of the message data. You need to use getType, getData1 and getData2 to get the real information. E.g. getType will tell you if message is a CC or not.

There are several example programs with MIDI.h that should give you some ideas. E.g. try input.ino

Steve

---

<div class="post-metadata">

**Author:** ![Deva\_Rishi](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/deva_rishi/32/227254_2.png) [@Deva\_Rishi](https://forum.arduino.cc/u/Deva_Rishi)\
**Post date:** [March 26, 2020, 9:41am UTC](https://forum.arduino.cc/t/code-for-controlling-a-servo-with-midi-mine-doesnt-work-yet/644455/3 "2020-03-26T09:41:01Z")

</div>

`MIDI_CREATE_DEFAULT_INSTANCE();`doesn't this creates a midi object on the hwSerial port, and there after you call`  Serial.begin(9600);`That changes the baud-rate to, well something other than the 31500 midi needs for it, so now it won't receive any meaningful data.

```cpp
  // Start serial monitoring
  Serial.println(val);

```

I suppose you could do this since you are not using midi-output, but the SM doesn't support the midi baud-rate though. So either move the midi input to a swSerial, which should work just fine, or move the Serial debug there.

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [March 26, 2020, 9:53am UTC](https://forum.arduino.cc/t/code-for-controlling-a-servo-with-midi-mine-doesnt-work-yet/644455/4 "2020-03-26T09:53:02Z")

</div>

> Deva\_Rishi:  
> ` Serial.begin(9600);`That changes the baud-rate to, well something other than the 31500 midi needs for it, so now it won't receive any meaningful data.

sp. "31250"

---

<div class="post-metadata">

**Author:** ![Deva\_Rishi](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/deva_rishi/32/227254_2.png) [@Deva\_Rishi](https://forum.arduino.cc/u/Deva_Rishi)\
**Post date:** [March 26, 2020, 1:42pm UTC](https://forum.arduino.cc/t/code-for-controlling-a-servo-with-midi-mine-doesnt-work-yet/644455/5 "2020-03-26T13:42:04Z")

</div>

> TheMemberFormerlyKnownAsAWOL:  
> sp. "31250"

Sorry, something like that.

---

<div class="post-metadata">

**Author:** ![robinhunt](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robinhunt/32/521173_2.png) [@robinhunt](https://forum.arduino.cc/u/robinhunt)\
**Post date:** [March 26, 2020, 4:05pm UTC](https://forum.arduino.cc/t/code-for-controlling-a-servo-with-midi-mine-doesnt-work-yet/644455/6 "2020-03-26T16:05:14Z")

</div>

Hi Guys!

Thanks a lot for this all, im going to dive into this 🙂

---

<div class="post-metadata">

**Author:** ![robinhunt](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robinhunt/32/521173_2.png) [@robinhunt](https://forum.arduino.cc/u/robinhunt)\
**Post date:** [March 29, 2020, 7:22pm UTC](https://forum.arduino.cc/t/code-for-controlling-a-servo-with-midi-mine-doesnt-work-yet/644455/7 "2020-03-29T19:22:08Z")

</div>

Hi All!

I got it working 🙂

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

[Here](https://www.instagram.com/p/B-UA6Y7o8uR/) a little movie I made:

Please find my working code here:

```cpp
/*
   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;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Deva\_Rishi](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/deva_rishi/32/227254_2.png) [@Deva\_Rishi](https://forum.arduino.cc/u/Deva_Rishi)\
**Post date:** [March 29, 2020, 7:38pm UTC](https://forum.arduino.cc/t/code-for-controlling-a-servo-with-midi-mine-doesnt-work-yet/644455/8 "2020-03-29T19:38:48Z")

</div>

🙂

---

<div class="post-metadata">

**Author:** ![guscac](https://avatars.discourse-cdn.com/v4/letter/g/ecc23a/32.png) [@guscac](https://forum.arduino.cc/u/guscac)\
**Post date:** [June 20, 2020, 9:59am UTC](https://forum.arduino.cc/t/code-for-controlling-a-servo-with-midi-mine-doesnt-work-yet/644455/9 "2020-06-20T09:59:39Z")

</div>

Hi!  
I wan't do do something exactly like this!!!! I was trying too to modify tjhe notes and volts code!  
Well, I try your code and i have the same issues i got with mine...  
the servos (sg90) are driving crazy!  
a little video [here](https://www.facebook.com/gus.voodoo1/videos/3247653601954273/)  
the probleme is not a hardware issue, the wiring is good...  
An idea?

---

<div class="post-metadata">

**Author:** ![Deva\_Rishi](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/deva_rishi/32/227254_2.png) [@Deva\_Rishi](https://forum.arduino.cc/u/Deva_Rishi)\
**Post date:** [June 20, 2020, 2:09pm UTC](https://forum.arduino.cc/t/code-for-controlling-a-servo-with-midi-mine-doesnt-work-yet/644455/10 "2020-06-20T14:09:42Z")

</div>

> An idea?

Start your own topic ?! Explain exactly what you want, what you have done (code within \</\> code-tags ), how you have wired everything.

---

<div class="post-metadata">

**Author:** ![guscac](https://avatars.discourse-cdn.com/v4/letter/g/ecc23a/32.png) [@guscac](https://forum.arduino.cc/u/guscac)\
**Post date:** [June 20, 2020, 2:53pm UTC](https://forum.arduino.cc/t/code-for-controlling-a-servo-with-midi-mine-doesnt-work-yet/644455/11 "2020-06-20T14:53:27Z")

</div>

yes, i'm gone start a new topic, i posted here because I used the same code and it doesn't work... i'll be doing this tomorow, i have no time today. thank's for answering me.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 12:24am UTC](https://forum.arduino.cc/t/code-for-controlling-a-servo-with-midi-mine-doesnt-work-yet/644455/12 "2021-05-06T00:24:16Z")

</div>


