Dear all,
the following Arduino sketch isn't running fast enough but I'm not yet smart or experienced enough ( ) to make further improvements on it. The situation: The Arduino (it's not an NG version) receives two byte information snippets via USB in this pattern and sends their corresponding values to three servos, that are connected to digital pins:
E6 AA
or
F0 BB
or
FA CC
The first byte always represents the ID for the servo, like E6 (230 in DEC) is servo #1, F0 (240 in DEC) is servo #2 and FA (250 in DEC) is servo #3. The second byte carries the servo turning values.
My problem is, that the code itself runs very slow. And I don't have any clue what to do about it. Please help, any thoughts appreciated!
Thanks, Patrick
/*
Servo control from vvvv via RS232 and a virtual COM port
The minimum (minPulse) and maxiumum (maxPulse) 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.
Original Servo code by Tom Igoe, Carlyn Maw
modified by milo (milo.digialsoul.org) and Patrick Raddatz (dingbatz.net)
*/
int servoPin; // control pin for servo motor
int minPulse = 500; // minimum servo position
int maxPulse = 550; // maximum servo position
int pulse = 2; // amount to pulse the servo
int incomingByte = 0; // value returned from vvvv
int memoryPulse2 = 0; // stores last pwm value of servo 1
int memoryPulse4 = 0; // stores last pwm value of servo 2
int memoryPulse6 = 0; // stores last pwm value of servo 3
void setup() {
pinMode(2, OUTPUT); // set servo 1 pin as an output pin
pinMode(4, OUTPUT); // set servo 2 pin as an output pin
pinMode(6, OUTPUT); // set servo 3 pin as an output pin
pulse = minPulse; // set the motor position value to the minimum
Serial.begin(9600); // begin serial readout
}
void loop() {
incomingByte = Serial.read(); // read incoming serial data
if (incomingByte > -1) // do we get anything?
{
if (incomingByte == 230) // checks for servo 1 data
{
servoPin = 2; // sets the corresponding pin
}
else if (incomingByte == 240) // checks for servo 2 data
{
servoPin = 4; // sets the corresponding pin
}
else if (incomingByte == 250) // checks for servo 3 data
{
servoPin = 6; // sets the corresponding pin
}
else
{
pulse = (incomingByte * 8) + 500; // convert the serial value to pwm value
if (servoPin == 2)
{
memoryPulse2 = pulse; // stores the pulse value
}
else if (servoPin == 4)
{
memoryPulse4 = pulse;; // stores the pulse value
}
else if (servoPin == 6)
{
memoryPulse6 = pulse;; // stores the pulse value
}
}
//Serial.println(incomingByte); // only for debugging
}
else { // if there's no new data, send the last known value
if (servoPin == 2)
{
pulse = memoryPulse2;
}
else if (servoPin == 4)
{
pulse = memoryPulse4;
}
else if (servoPin == 6)
{
pulse = memoryPulse6;
}
}
constrain(pulse,minPulse,maxPulse); // constrain pwm values to a range between minPulse & maxPulse
// pwm generation
delay(50); // pulse the servo again after (50 ms) have passed
digitalWrite(servoPin, HIGH); // turn the motor on
delayMicroseconds(pulse); // length of the pulse sets the motor position
digitalWrite(servoPin, LOW); // turn the motor off
}