Showing posts with label servo motor. Show all posts
Showing posts with label servo motor. Show all posts

Tuesday, January 08, 2013

Control a Small Servo Motor from Live via a USB MIDI Connection




I wanted to be able to control a servo motor from Live directly via a USB connection using MIDI data and no software layers. This was done via a Teensy board.

Most servo motors have three connections. The ground is usually black, the power is usually red and the signal is usually white.

In terms of setting up the hardware: 
• Connect ground to Teensy ground
• Connect power to Teensy VCC
• Connect signal to Teensy digital pin 0

Below is some basic code. With this code, the servo will respond to MIDI CC messages. Edit to taste.



 
void setup() {
  usbMIDI.setHandleControlChange(OnControlChange);
  pinMode(0, OUTPUT);
}

void loop() {
  usbMIDI.read(); 
}


void OnControlChange(byte channel, byte control, byte value) {
  for(int i = 0; i < 20; i++) {
    digitalWrite(channel, HIGH);
    delayMicroseconds(value * 6 + 700);
    digitalWrite(channel, LOW);
    delayMicroseconds(22000 - (value * 6 + 700));

  }
}