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

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!!

#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]

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

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.

  // 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.

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”

TheMemberFormerlyKnownAsAWOL:
sp. "31250"

Sorry, something like that.

Hi Guys!

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

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;
  }
}

:slight_smile:

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
the probleme is not a hardware issue, the wiring is good...
An idea?

An idea?

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

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.