Attaching a servo seems to disable serial reading

I am using Vixen 3 in conjunction with an Arduino Uno to interpret the serial stream coming from Vixen 3's generic serial adapter. Everything seems fine on the Vixen side, because as long as my Arduino outputs are attached to simple LEDs, everything is fine. However, as soon as I attach a servo to any of the outputs, only the servos seem to work. Anything else ceases to do so.

Here's a greatly simplified example:

#include <Servo.h>
Servo servo1;

int pos = 0;

int Chan1 = 3;     //PWM-Servo 1
int Chan4 = 9;     //PWM-Light 1


int i = 0;     // Loop counter
int incomingByte[8];   // array to store the 25 values from the serial port

//setup the pins/ inputs & outputs
void setup()
{
  Serial.begin(115200);   // set up Serial at 9600 bps

  pinMode(Chan1, OUTPUT);
  //servo1.attach(3);
  pinMode(Chan4, OUTPUT);

}

void loop()
{  // 8 channels are coming in to the Arduino with two header characters
   if (Serial.available() >= 10) {
    
    // consume and check the header
    int header1 = Serial.read();
    if (header1 != '~') {return;}
    int header2 = Serial.read();
    if (header2 != '!') {return;}
    for (int i=0; i<9; i++) {
      // read each byte
      incomingByte[i] = Serial.read();
    }
   
    analogWrite(Chan4, incomingByte[3]);      //Light 1
   }
}

As is, this code works perfectly. However, if I uncomment the line that reads

servo1.attach(3);

Chan4 no longer functions. I've even tried downloading other people's code from around the web, such as zparticle's code. Same thing happens. As soon as I attach a servo, all other channels stop working. I can't spot anything wrong, though. Anybody know what's going on here?

Go read the page on this site about the servo library. Therein you should find the answer you're looking for. It's a fairly common issue. Always read the documentation.

Delta_G:
Go read the page on this site about the servo library. Therein you should find the answer you're looking for. It's a fairly common issue. Always read the documentation.

Can you point me to this? I had just come from the servo documentation before I posted here, and I didn't see anything that seemed to meet this. In fact, I just re-read it again, and I still do not see anything about this issue there. Are you talking about this document?

Yes, that document. Second paragraph. It doesn't say anything about serial comms but that's because you don't have a serial issue. It does address why you don't see analogWrite working on pin 9 anymore.

Delta_G:
Yes, that document. Second paragraph. It doesn't say anything about serial comms but that's because you don't have a serial issue. It does address why you don't see analogWrite working on pin 9 anymore.

I see it. Sometime you RTFM and still miss things. Thanks.

Hi,

The Servo library supports up to 12 motors on most Arduino boards and 48 on the Arduino Mega. On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins.

When you un-comment

servo1.attach(3);

You disable the servo function of

Chan4

because it is using pin 9.

Change Chan4 pin to another, other than, 9 or 10.

Tom... :slight_smile:

Thanks. I got it when I properly read the docs. Feel kind of silly for asking now, since I'm hardly a neophyte. It was one of those cases of staring at something so long you over-complicate it. Here I was back in raw AVR code playing with interrupts, thinking it was something like that, when all I really had to do was look it up. Oops.