hi all, I will like to control a servo motor connected in my arduino board via a pd patch using [comport] or any other object that could be good for the case.
since the examples i found are for sending data to pd from sensors connected to the arduino and what i need is precisely working in the other way round (pd -> arduino) i decided to open a thread hoping someone could share the matching basic examples from a pd patch and its corresponding arduino patch receiving the control data.
i hope to manage to do it soon, if so i will put them both here below, cheers! /a
|
|
V
here is a .pde program that i have tried with one servo, it did work in a very unprecise way since the servo moved almost randomly..but at least moved when i scrolled data from within pd puredata.
could you test it and make it better?
here it is:
/*
Two-servo control from an analog input
Moves two servos based on the value of one analog sensor,
in opposite directions.
The minimum (minPulse) and maxiumum (maxPuluse) values
will be different depending on your specific servo motor.
Ideally, it should be between 1 and 2 milliseconds, but in practice,
0.5 - 2.5 milliseconds works well for me.
Try different values to see what numbers are best for you.
by Tom Igoe
Created 28 Jan. 2006
Repurposed for Dual Servos by Carlyn Maw
24 Mar. 2006
Modified for serial control by Martin PEach 20060603
Send data via serial port at 19200 baud with angle as 0-15 in low nibble and
servo number in high nibble.
This does 2 servos but could be expanded to do up to 16.
*/
int servoPinL = 2; // Control pin for servo motor - L
int servoPinR = 3; // Control pin for servo motor - R
int pulseL = 0; // Amount to pulse the servoL
int pulseR = 0; // Amount to pulse the servoR
int delayValueMax = 20000; // the 2 millisecond maxiumum pulse range for the
servo
int delayValue = 20000; // the actual value the code should wait, determined
later
int analogValue = 0; // the value returned from the analog sensor
int analogPin = 0; // the analog pin that the sensor's on
char c, servoNumber, servoValue;
void setup(void)
{
pinMode(servoPinL, OUTPUT); // Set servo pins as an output pins
pinMode(servoPinR, OUTPUT);
pulseL = 1000; // Set the motor position values to the minimum
pulseR = 1000;
beginSerial(19200);
}
void loop(void)
{
while (serialAvailable != 0)
{
c = serialRead();
servoNumber = (c & 0xF0) >> 4; // servo ID is in high nibble
servoValue = (c & 0x0F); // servo angle is in low nibble
if (servoNumber == 0) pulseL = (servoValue*62) + 1000; // make a pulse
1000-2000 us
else if (servoNumber == 1) pulseR = (servoValue*62) + 1000; // make a
pulse 1000-2000 us
}
digitalWrite(servoPinL, HIGH); // Turn the L motor on
delayMicroseconds(pulseL); // Length of the pulse sets the motor position
digitalWrite(servoPinL, LOW); // Turn the L motor off
digitalWrite(servoPinR, HIGH); // Turn the R motor on
delayMicroseconds(pulseR); // Length of the pulse sets the motor position
digitalWrite(servoPinR, LOW); // Turn the R motor off
delayValue = delayValueMax - pulseL - pulseR; // determine how much time
you
have before the 20 ms is over...
delayMicroseconds(delayValue); // 20 millisecond delay is needed between
pulses to keep the servos in sync
}
dont forget that this is still in very early stages, please if you have done this step already (controlling servos from within pd) please share your discoveries.