BrunoTS
BrunoTS

Reputation: 171

Issues with RGB led and Servomotors

I'm on my first Arduino project... On this project, I use an RGB led and 2 Servomotors...

First off all, following OOP, I create a class to control my RGB led...

class StatusLED {
  private:
    int pinRed;
    int pinGreen;
    int pinBlue;

  public:
    StatusLED(int pinRed, int pinGreen, int pinBlue);
    void RGB(int redValue, int greenValue, int blueValue);
};

It's working very well, without issues...

After there is all fine with the RGB led, I start to include my Servomotor code...

#include <Servo.h>

#define PIN_RGBLED_R 9
#define PIN_RGBLED_G 10
#define PIN_RGBLED_B 11
#define PIN_SERVO_H 12
#define PIN_SERVO_V 13

Servo servoH;
Servo servoV;
LED led(PIN_RGBLED_R, PIN_RGBLED_G, PIN_RGBLED_B);

void setup() {
  servoH.attach(PIN_SERVO_H);
  servoV.attach(PIN_SERVO_V);
}

And after I include the servo.attach() lines, my RBG led has a strange behavior, the colors that I used before, like Light Purple RGB(2, 0, 2);, doesn't work anymore, now when i try it, the led turns on with Red color.

If I comment the servo.attach() lines, the led works well.

Already tried:

Someone can please help me?

EDIT:

Just to eliminate the doubt of my LED class are the problem, I create a new file...

#include <Servo.h>

#define PIN_SERVO_H 3
#define PIN_SERVO_V 4
#define PIN_RGBLED_R 9
#define PIN_RGBLED_G 10
#define PIN_RGBLED_B 11

Servo servoH;
Servo servoV;

void setup() {
  pinMode(PIN_RGBLED_R, OUTPUT);
  pinMode(PIN_RGBLED_G, OUTPUT);
  pinMode(PIN_RGBLED_B, OUTPUT);
  servoH.attach(PIN_SERVO_H);
  servoV.attach(PIN_SERVO_V);  
}

void loop() {
  RGB(2,0,2);
  delay(100);
  RGB(4,0,4);
  delay(100);
  RGB(8,0,8);
  delay(100);
  RGB(16,0,16);
  delay(100);
  RGB(0,0,0);
  delay(1000);  
}

void RGB(int redValue, int greenValue, int blueValue) {
  if (redValue > 255) {
    redValue = 255;
  }
  if (greenValue > 255) {
    greenValue = 255;
  }
  if (blueValue > 255) {
    blueValue = 255;
  }
  if (redValue < 0) {
    redValue = 0;
  }
  if (greenValue < 0) {
    greenValue = 0;
  }
  if (blueValue < 0) {
    blueValue = 0;
  }
  // This is a common anode RGB Led.
  // So 255 is OFF and 0 is Fully ON
  analogWrite(PIN_RGBLED_R, 255 - redValue);
  analogWrite(PIN_RGBLED_G, 255 - greenValue);
  analogWrite(PIN_RGBLED_B, 255 - blueValue);
} 

And the problem continues... if I comment the lines of attach() the led works fine, without comment, it only blinks in Red color...

Upvotes: 1

Views: 61

Answers (1)

Alex Shirley
Alex Shirley

Reputation: 425

Depending on the version of your arduino, servo.attach only works on Pin 9 and Pin 10, which conflicts with your RGB pins. See https://www.arduino.cc/en/Reference/ServoAttach

Upvotes: 2

Related Questions