Servo stops working when Serial for debugging purposes is uncommented

Hi guys,
I’m programming a mechanical 7 segment clock with 4 digits. While testing and debugging my code with one digit, I noticed something strange:
Everything works as anticipated, until I add an extra line of Serial test to display on the computer. I managed to show similar behavior in a more simple example sketch:

#include <Servo.h>

Servo myservo[7]; //Now we have 7 servos
byte seven_seg_digits[2][7] =  {
  { 1, 1, 1, 1, 1, 1, 1 }, 
  { 0, 0, 0, 0, 0, 0, 0 } 
};


byte testarray[2][7] =  {
  { 1, 2, 3, 4, 5, 6, 7 }, // used for debugging reasons 
  { 1, 2, 3, 4, 5, 6, 7 } // used for debugging reasons
};

void setup() {
  Serial.begin(9600);
}

void sevenSegWrite(int digit) {

  for (int i = 1; i < 8 ; i++) {      // attach servos
    myservo[i].attach(i + 6);
  }

  for (int ii = 1; ii < 8 ; ii++) {
    myservo[ii].write(90 * seven_seg_digits[digit][ii - 1]);        // tell servo to go to position in variable 'pos'
        Serial.print("servo ");  Serial.print(ii); Serial.print(" - signal "); Serial.println(testarray[1][ii - 1]);
  }
}

void loop() {
  sevenSegWrite(1); delay(1000);
  sevenSegWrite(0); delay(1000);
  sevenSegWrite(1); delay(1000);
  sevenSegWrite(0); delay(1000000);
}

All servo’s move their sweep, until I uncomment the following serial print:

//    Serial.print("servo ");  Serial.print(ii); Serial.print(" - signal "); Serial.println(testarray[1][ii - 1]);

The last servo, myservo[7] doesn’t move anymore. I already tried connecting the last two servo’s to pins 5 and 6, but that didn’t help.

Who can help me out? For the final program the Serial won’t be used anymore, but for testing it’s quite nice to now that everything works as anticipated.

Your Serial.print() statements slow down your code. You may be writing too fast to your servos… Servos take time to actually move…

Hi blh46, Thanks for the fast reply. What would be the best way to deal with this? I guess control the servo’s in a for-loop in smaller increments?

It seems that it is also related to the fact that I attach the servos in the sevenSegWrite function. When I declare them in the setup, the issue of servos not moving doesn’t arise.

The reason though for me to declare them in the sevenSegWrite-function is that I later on detach them in order to stop them from rattling.

Look at the servo examples. You write a value, delay() a few milliseconds and write the next increment

Hi,
Try bumping your serial speed up.

Serial.begin(115200);

Tom… :grinning: :+1: :coffee: :australia:

You interate over your servos from 1 to 7. But if you have 7 servos, the index runs from 0 to 6 .

1 Like

Good catch… :+1: :+1: :+1:

Tom… :grinning: :+1: :coffee: :australia:

Good one. Still need to incorporate blh64’s suggestion though since the original issue remains.
Thanks for all the good suggestions.