Delay in the following Servo Control - Stuttering

So, the following code-problem is relating to the beginning of a multi-servo setup for a project that I'm currently working on. It's test code and redundant and not pretty. The problem that I'm running into is related to delay. I'm trying to work delay elements into the code and am currently finding that when/wherever I position the delay in the code the servo stutters through the angle change over a period of time (which would appear to link up to the delay that I've input). The delay results in my code are different than those that I receive with the simple servo. I will post the code in the next message for this topic. Thanks in advance.

N

int servoPinL = 0; // Control pin for servo motor - L COUNTER-CLOCKWISE int servoPinR = 1; // Control pin for servo motor - R CLOCKWISE int servoPinL1 = 2; // L int servoPinR1 = 3; // R int servoPinL2 = 4; // L int servoPinR2 = 5; // R int servoPinL3 = 6; // L int servoPinR3 = 7; // R int servoPinL4 = 8; // L int servoPinR4 = 9; // R int servoPinL5 = 10; // L int servoPinR5 = 11; // R int servoPinL6 = 12; // L int servoPinR6 = 13; // R

int minPulse = 800; // Minimum servo position int maxPulse = 2000; // Maximum servo position

int minPulse1 = 800; int maxPulse1 = 2000;

int minPulse2 = 800; int maxPulse2 = 2000;

int minPulse3 = 1600; // PIN 6 IS ALTERED BELOW int maxPulse3 = 2000;

int minPulse4 = 800; int maxPulse4 = 2000;

int minPulse5 = 800; int maxPulse5 = 2000;

int minPulse6 = 800; int maxPulse6 = 2000; // Maximum servo position

long adjustedAV = 0; // Analog value adjusted for determining pulse values

int pulseL = 0; // Amount to pulse the servoL int pulseR = 0; // Amount to pulse the servoR int pulseL1 = 0; int pulseR1 = 0; int pulseL2 = 0; int pulseR2 = 0; int pulseL3 = 0; int pulseR3 = 0; int pulseL4 = 0; int pulseR4 = 0; int pulseL5 = 0; int pulseR5 = 0; int pulseL6 = 0; int pulseR6 = 0;

int delayValueMax = 20; // the 20 microsecond maxiumum pulse range for the servo int delayValue = 20; // 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

void setup() { pinMode(servoPinL, OUTPUT); // DIGITAL 0 output, Set servo pins as an output pins pinMode(servoPinR, OUTPUT); // DIGITAL 1 output pinMode(servoPinL1, OUTPUT); // DIGITAL 2 output pinMode(servoPinR1, OUTPUT); // DIGITAL 3 output pinMode(servoPinL2, OUTPUT); // DIGITAL 4 output pinMode(servoPinR2, OUTPUT); // DIGITAL 5 output pinMode(servoPinL3, OUTPUT); // DIGITAL 6 output pinMode(servoPinR3, OUTPUT); // DIGITAL 7 output pinMode(servoPinL4, OUTPUT); // DIGITAL 8 output pinMode(servoPinR4, OUTPUT); // DIGITAL 9 output pinMode(servoPinL5, OUTPUT); // DIGITAL 10 output pinMode(servoPinR5, OUTPUT); // DIGITAL 11 output pinMode(servoPinL6, OUTPUT); // DIGITAL 12 output pinMode(servoPinR6, OUTPUT); // DIGITAL 13 output

pulseL = minPulse; // Set the motor position values to the minimum pulseR = maxPulse;

pulseL1 = minPulse1; pulseR1 = maxPulse1;

pulseL2 = minPulse2; pulseR2 = maxPulse2;

pulseL3 = minPulse3; pulseR3 = maxPulse3;

pulseL4 = minPulse4; pulseR4 = maxPulse4;

pulseL5 = minPulse5; pulseR5 = maxPulse5;

pulseL6 = minPulse6; pulseR6 = maxPulse6; }

void loop() {

analogValue = analogRead(analogPin); // read the analog input (0-1024) adjustedAV = (analogValue / 12) * 11; // IF IT CLICKS... ADJUST THE LAST NUMBER

pulseL = adjustedAV + minPulse; // 0 Set value for left motor pulseR = maxPulse - adjustedAV; // 1 and the inverse for the right

pulseL1 = adjustedAV + minPulse1; // PIN 2 pulseR1 = maxPulse1 - adjustedAV; // PIN 3

pulseL2 = adjustedAV + minPulse2; // PIN 4 pulseR2 = maxPulse2 - adjustedAV; // PIN 5

pulseL3 = adjustedAV + minPulse3 / 2; // PIN 6 Dividing by 2 limits pulseR3 = maxPulse3 - adjustedAV; // PIN 7 the range of Motion for this motor

pulseL4 = adjustedAV + minPulse4; // PIN 8 pulseR4 = maxPulse4 - adjustedAV; // PIN 9

pulseL5 = adjustedAV + minPulse5; // PIN 10 pulseR5 = maxPulse5 - adjustedAV; // PIN 11

pulseL6 = adjustedAV + minPulse6; // PIN 12 pulseR6 = maxPulse6 - adjustedAV; // PIN 13

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 L motor on delayMicroseconds(pulseR); // Length of the pulse sets the motor position digitalWrite(servoPinR, LOW); // Turn the L motor off

digitalWrite(servoPinL1, HIGH); delayMicroseconds(pulseL1); digitalWrite(servoPinL1, LOW);

digitalWrite(servoPinR1, HIGH); delayMicroseconds(pulseR1); digitalWrite(servoPinR1, LOW);

digitalWrite(servoPinL2, HIGH); delayMicroseconds(pulseL2); digitalWrite(servoPinL2, LOW);

digitalWrite(servoPinR2, HIGH); delayMicroseconds(pulseR2); digitalWrite(servoPinR2, LOW);

digitalWrite(servoPinL3, HIGH); delayMicroseconds(pulseL3); digitalWrite(servoPinL3, LOW);

digitalWrite(servoPinR3, HIGH); delayMicroseconds(pulseR3); digitalWrite(servoPinR3, LOW);

digitalWrite(servoPinL4, HIGH); delayMicroseconds(pulseL4); digitalWrite(servoPinL4, LOW);

digitalWrite(servoPinR4, HIGH); delayMicroseconds(pulseR4); digitalWrite(servoPinR4, LOW);

digitalWrite(servoPinL5, HIGH); delayMicroseconds(pulseL5); digitalWrite(servoPinL5, LOW);

restofcodeinnextpost!!

digitalWrite(servoPinR5, HIGH);
delayMicroseconds(pulseR5);
digitalWrite(servoPinR5, LOW);

digitalWrite(servoPinL6, HIGH);
delayMicroseconds(pulseL6);
digitalWrite(servoPinL6, LOW);

digitalWrite(servoPinR6, HIGH);
delayMicroseconds(pulseR6);
digitalWrite(servoPinR6, LOW);

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

}

I suggest you either purchase a dedicated servo controller or you make your routines non-blocking (i.e. timer-based rather than reliant on delays).

  • Ben

Hi Bens, I was assuming that it'd be the only solution to the issue, but, was hoping I'd be able to find a work-around for the meanwhile, but it's not really a control parameter that's near and dear to this particular project, more of a last minute thought during the first stages of working out the interactivity. Thanks for the feedback.

N