Ableton + Arduino + Servo

florent bodart's icon

Hi,

I have a project to do. I want to control a servo motor hitting a drum with midi sequences with an Arduino though Ableton live. Exemple in c/c 0-1-0-1 . The ‘hammer’ of the servo hits the second time and the 4th. I use Hairless-midi with IAC bus to send the midi infos on the Arduino, this part is working. Now I want to make move the hammer each time (for exemple) the C1 note pass in the midi signal. Can you help me on this? Thanks!

The servo code :

#include

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
 myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
 for (pos = 0; pos
   // in steps of 1 degree
   delay(15);                       // waits 15ms for the servo to reach the position
 }
 for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
   delay(15);                       // waits 15ms for the servo to reach the position
 }
}

musinou's icon

Hi FLORENT BODART,

First, you need to print what Hairless-midi is sending to your Arduino. And use that to trigger a function that would move your servos. You most not use delay(); if you want to have something responsive.

I have a project I made that could have up to 32 servos controlled by MIDI, and I used a Teensy and a SSC-32. The Teensy allows you to build a MIDI instrument relatively flawlessly.You would just plug your homemade interface and your computer would see it like any MIDI instrument, and it would work directly in Live without need for Max4Live or Hairless. If you are using your setup for live performances or installation, I would advise such setup.
https://www.youtube.com/watch?v=zMy7sFLJ1l0

The SSC-32 is kind of an old board, so I did not make a HOW TO for that setup. But I did one for MIDI to stepper motors that is kind of similar on the principle, but, well, it is for steppers...
http://liveelectronics.musinou.net/StepperMotorsMidiControlled.php

florent bodart's icon

Thanks a lot for the answer!

Teensy would be better maybe.. And what about the Ableton + Arduino + the callbacks function ? http://arduinomidilib.fortyseveneffects.com/a00004.html

Best,
Florent

musinou's icon

The Arduino MIDI library is for MIDI IN, MIDI OUT and MIDI Thru round connectors. Not for USB-MIDI, as far as I can tell.

There is Hiduino, for Arduino UNO or Mega 2560
https://github.com/ddiakopoulos/hiduino

And if you have a Leornardo, or other TeeOnArdu compatible board, you can use Teensyduino and it would work like a Teensy.

florent bodart's icon

Thanks!

I have this code for the moment..

#include
MIDI_CREATE_DEFAULT_INSTANCE();

#include

Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position

// -----------------------------------------------------------------------------
// This function will be automatically called when a NoteOn is received.
// It must be a void-returning function with the correct parameters,
// see documentation here:
// http://arduinomidilib.fortyseveneffects.com/a00022.html
void handleNoteOn(byte channel, byte pitch, byte velocity)
{

if (pitch == 12) { // pitch 12 corresponds to note C1 (C = 0, and there's 12 halve tones in an octave)
kickTheDrum();
}

}

void kickTheDrum() {
for (pos = 0; pos
// in steps of 1 degree
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
delay(15); // waits 15ms for the servo to reach the position
}
}

void handleNoteOff(byte channel, byte pitch, byte velocity)
{
// Do something when the note is released.
// Note that NoteOn messages with 0 velocity are interpreted as NoteOffs.
}
// -----------------------------------------------------------------------------
void setup()
{

myservo.attach(9); // attaches the servo on pin 9 to the servo object

// Connect the handleNoteOn function to the library,
// so it is called upon reception of a NoteOn.
MIDI.setHandleNoteOn(handleNoteOn); // Put only the name of the function
// Do the same for NoteOffs
MIDI.setHandleNoteOff(handleNoteOff);
// Initiate MIDI communications, listen to all channels
MIDI.begin(MIDI_CHANNEL_OMNI);
}
void loop()
{
// Call MIDI.read the fastest you can for real-time performance.
MIDI.read();
// There is no need to check if there are messages incoming
// if they are bound to a Callback function.
// The attached method will be called automatically
// when the corresponding message has been received.
}

musinou's icon

This code is code with one motor, but you can't use it if you want polyphonic. What you wrote here waits 15ms between each position of the motor and does not do anything while waiting, so if you have another MIDI note coming, when a motor is moving, it is not going to kick.

What I did is using millis() and switch case, as you can see here:
http://liveelectronics.musinou.net/StepperMotorsMidiControlled.php

florent bodart's icon

Thanks a lot Musinou. I just bought the x12 Servo shield of Adafruit https://www.adafruit.com/products/1411
I guess with Ableton+ the Arduino + the shield it could be enough to work?

musinou's icon

Not for all Arduino models.

There is Hiduino, for Arduino UNO or Mega 2560
https://github.com/ddiakopoulos/hiduino

And TeeOnArdu with Teensiduino would work for a Leonardo

florent bodart's icon

I have the Uno R3 and the Mega 2560.
I'll connect the Adafruit x12 servo shield and control them with Hiduino or the Arduino Midi library http://arduinomidilib.fortyseveneffects.com/a00004.html

musinou's icon

The Uno R3 might be neater for your shield.

You should not need the Arduino Midi library.

florent bodart's icon

Musinou, tu es français/se?

musinou's icon

Non, mais je parle français.