Weird LED and Servo Issue

Hi,

I am having a weird issue.

I was trying to fade a LED in a bigger project, and every time I tried fading the LED it would turn green, and then keep being green until the green channel on the led was set to zero.

I used hours trying to debug my code, and I would not figure out what was wrong. I then tried a simpler code that only controlled the RGB LED, and that worked, so eventually I figured out that if I attack my servo this issued occurs. I this a bug in the development tool or what is going on?

I am using version 1.7.10 for Liux 64 bit downloaded from arduino.org.

This is my test code.

#include <Servo.h>

// Pin Definitions
const int servoPin = 3;
const int blueLedPin = 9;
const int redLedPin = 10;
const int greenLedPin = 11;

// Servo Definition
Servo servoMotor;

void setup() {
  pinMode(greenLedPin, OUTPUT);
  pinMode(redLedPin, OUTPUT);
  pinMode(blueLedPin, OUTPUT);
  //servoMotor.attach(servoPin);
}

void loop() {
  for(int i=255; i > 0 ; i -= 1) {
    analogWrite(blueLedPin, i);
    analogWrite(greenLedPin, i);
    analogWrite(redLedPin, i);
    delay(7);
  }
  for(int i=0; i < 255 ; i += 1) {
    analogWrite(redLedPin, i); 
    delay(7);
  }
  delay(10000);
}

If you uncomment the servoMotor.attach line it will instead of fading out and then fading to red it will instantly turn Green, and the power off, and the fade to red.

Not weird at all, just the usual conflict over timer usage. Servo - Arduino Reference

Huh.. RT*M would have helped. :slight_smile: And it would also have been much faster then struggling with this issue.

Thank you for your reply. It was most helpful!

Since this is a UNO R3, can I them move the servo to Pin 9, and the LED to some other PWM enabled pins other than Pin 9 and 10, and everything should work?

Best Regards
Brian

You should be able to find some combination of pins that work. Study the library documentation for the limitations.