Sunday, February 03, 2008

Arduino serial servo control

Video example: http://www.youtube.com/watch?v=NW7G_mqVWnA



A quick test with a servo motor. Serial data controls the position of a micro servo motor.


Code:

/* Quick Serial Servo Control

by Sebastian Tomczak

3 February 2008

Adelaide, Australia

*/


byte data;


void setup() {

Serial.begin(57600);

pinMode(2, OUTPUT);

}


void loop() {

if(Serial.available() > 0) {

data = Serial.read();

for(int i = 0; i < 21; i++) {

digitalWrite(2, HIGH);

delayMicroseconds(data * 6 + 700);

digitalWrite(2, LOW);

delayMicroseconds(22000 - (data * 6 + 700));

}

}

}



And of course, by extension it is quite easy to modify the above code to run two servo motors and sequence them from Ableton Live...

Code:
/* Quick 2 x Serial Servo Control

by Sebastian Tomczak



3 February 2008

Adelaide, Australia

*/

byte data1;

byte data2;


void setup() {

Serial.begin(57600);

pinMode(2, OUTPUT);

pinMode(3, OUTPUT);

}


void loop() {

if(Serial.available() > 1) {

data1 = Serial.read();

data2 = Serial.read();

for(int i = 0; i < 20; i++) {

digitalWrite(data1, HIGH);

delayMicroseconds(data2 * 6 + 700);

digitalWrite(data1, LOW);

delayMicroseconds(22000 - (data2 * 6 + 700));

}

}

}

6 comments:

Anonymous said...

Hi Sebastian,

This is cool! Quick question: what connections did you make between Live and the Arduino? Is it just Midi Out from Live to Arduino RX?
Does the Arduino just understand the Midi messages it's receiving from Live or you have to parse it in some way?

Cheers,
Ben

Sebastian Tomczak said...

Hi Ben,
thanks for your message.

in this case i think i used a software layer in the form of a Max/MSP patch which took MIDI data, formatted it, and sent it to the Arduino via a serial connection.

but of course this could be done directly via MIDI as you suggest. you would have to write / find a MIDI input driver --- i have some code that i reuse time and time again, i can send it to you if you email me.


kind regards

Unknown said...

Hi Sebastian,

Thanks for your reply. It would be great to check out your Max/Arduino patch or whatever you can share. It sounds very similar to what I'm trying to do.

So far I have a Max patch that takes Midi data from 5 midi channels, then sends it serially to Arduino, for example "255 120 77 88 205"

But then I'm having trouble parsing that in Arduino and have it control the 5 servos.
Perhaps I should arrange the data differently in Max before it goes out to Arduino?

I appreciate any help. My email is ithaib@gmail.com

Thanks again,

Mr. Andros, the dead (Junco) said...

hello! i'm trying to make 4 servos respond to max msp messages and i dont know how to make the numbers go out from max to the arduino... i would really appreciate some help.
i'm a rookie on these "platforms"...

Sebastian Tomczak said...

i have recently had a massive HD failure, so i cant help you right at this moment because i dont have a working setup.

but if you email me in about three days on seb dot tomczak at gmail dot com, i can give you a hand.

peace,
seb

contactos en barcelona said...

Very useful piece of writing, much thanks for your post.