简体   繁体   English

RGB LED 和伺服电机的问题

[英]Issues with RGB led and Servomotors

I'm on my first Arduino project... On this project, I use an RGB led and 2 Servomotors...我在我的第一个 Arduino 项目中......在这个项目中,我使用了一个 RGB LED 和 2 个伺服电机......

First off all, following OOP, I create a class to control my RGB led...首先,在 OOP 之后,我创建了一个 class 来控制我的 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...在 RGB LED 一切正常后,我开始包含我的伺服电机代码......

#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.在我包含servo.attach()行之后,我的 RBG led 有一个奇怪的行为,我之前使用的 colors,比如 Light Purple RGB(2, 0, 2) ;,现在当我尝试时不再工作它,led 亮起红色。

If I comment the servo.attach() lines, the led works well.如果我评论servo.attach()行,则 LED 工作正常。

Already tried:已经尝试过:

  • Change Servo libraries version;更改伺服库版本;
  • Change servos to another pins;将舵机换到另一个引脚;

Someone can please help me?有人可以帮帮我吗?

EDIT:编辑:

Just to eliminate the doubt of my LED class are the problem, I create a new file...为了消除我的 LED class 的疑问,我创建了一个新文件...

#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...问题还在继续……如果我评论attach()的行,led 工作正常,没有评论,它只会以红色闪烁……

Depending on the version of your arduino, servo.attach only works on Pin 9 and Pin 10, which conflicts with your RGB pins.根据您的 arduino 的版本,servo.attach 仅适用于 Pin 9 和 Pin 10,这与您的 RGB 引脚冲突。 See https://www.arduino.cc/en/Reference/ServoAttach参见https://www.arduino.cc/en/Reference/ServoAttach

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM