Servo and LED not working together

#include <Servo.h>

Servo myservo;

unsigned long PreviousTime = 0;
const unsigned long Interval = 100;
int pos = 0;
int amount = 1;

int led_w = 10;
int brightness_w = 0;
int fadeAmount = 5;
unsigned long PreviousTime_w = 0;
const unsigned long fade_time = 300;


void setup() {
  Serial.begin(9600);
  myservo.attach(3);
  pinMode(led_w, OUTPUT);
}

void loop() {

  unsigned long CurrentTime = millis();

  if (CurrentTime > 3000 && CurrentTime < 15000) {
    if (CurrentTime - PreviousTime_w >= fade_time) {
      int brightness_c = constrain(brightness_w, 0, 150);
      Serial.println(brightness_c);
      analogWrite(led_w, brightness_c);
      PreviousTime_w = CurrentTime;
      brightness_w = brightness_w + fadeAmount;
    }
  }

  if (CurrentTime >= 5000 && CurrentTime <= 15000) {
    if (CurrentTime - PreviousTime >= Interval) {
      int pos_c = constrain(pos, 0, 90);
      myservo.write(pos_c);
      PreviousTime = CurrentTime;
      pos = pos + amount;
    }
  }
  if (CurrentTime >= 20000 && CurrentTime <= 40000) {
    if (CurrentTime - PreviousTime >= Interval) {
      PreviousTime = CurrentTime;
      pos = pos - amount;
      int pos_c = constrain(pos, 0, 90);
      myservo.write(pos_c);
    }
  }
}

Hi I am new to this. I am trying to gradually light up my led at 3 sec and start servo after 5 sec. For some reasons the led never lights up; but it lights up when I copy the same code (just led part) to a new file. Not sure what went wrong. Your help is much appreciated.

What is this doing for you ?

if (CurrentTime > 3000 && CurrentTime < 15000) 

When time is between 3 seconds and 15 seconds, turn on led gradually. Something wrong with this approach? Thanks

CurrentTime is the same as millis( ).

millis( ) advances in value every 1 millisecond.

CurrentTime > 3000 && CurrentTime < 15000 This will be true only from 3 seconds to 15 seconds after the power is turned on; after 15 seconds, millis( ) will not again be in this range for 49 days when it overflows.

Guess this is not what you wanted ?

Yes that’s what I want.

My problem is that the led won’t light up with my code above. But if I copy and paste the led part to a new file it works perfectly. I also found out as soon as I added “myservo.attach(3)” in setup, the led won’t turn on. Maybe I missed something? Really appreciate the help. Thanks!

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.

Hi,
you are right.
When using the servo library and starting the servo, pin 10 is only working as PWM if the PWM value is 255.
I made a very simple sketch and I could see the problem.
By commenting out the line myservo.attach(3); the LED works correctly,
but without commenting the LED only lights up with analogWrite(led_w, 255);
Any value less than 255 does not light the LED.
The error only occurs with pins 9 and 10.
I will try to find out the reason, but I think my knowledge may not be enough for this determination.
Maybe someone else here on the forum can help.

Follow my very simple code.

PS: While I was testing your code and writing my conclusions, @LarryD . presented the description of the problem.

#include <Servo.h>
int led_w = 10;
Servo myservo;
//----------------------------------
void setup() {
  Serial.begin(9600);
  myservo.attach(3);
  pinMode(led_w, OUTPUT);
}
//----------------------------------
void loop() {
  analogWrite(led_w, 254);
}

@LarryD told you the reason that analogWrite will not work on pins 9 and 10 if the Servo library is used. Read the reference for the Servo library that he linked.

Hi,
Dear @groundFungus please read my PS.

PS: While I was testing your code and writing my conclusions, @LarryD . presented the description of the problem.

BTW.
PS means " post scriptum " .

FWIW - I found out the other day that Servo.h tanks writeRGB (and writeRed, writeGreen and writeBlue) on the Esplora.

Sorry.

That's it! It works after I switched my led to pin 6 instead of pin 10. Learned a good lesson here -- need to thoroughly read library description before using it. Thank you everyone for your comments and input. It helps a beginner like me a lot. Really appreciate it!