让 RGB LED 灯更亮?

Make RGB LED Light up brighter?

我一直在尝试制作一个交通灯模拟器,但在使一个 RGB 灯亮起时遇到了问题(在代码中它被称为 (colour)Pin2)。第一盏灯工作正常,但左侧的 RGB 工作不正常。当光线是白色时,我成功地让它变亮了,但是当我试图把它变成黄色时,光线太暗了,我几乎看不到它。它接线正确,所以我认为代码中可能有错误。如果有人需要,这里是项目的 link https://www.tinkercad.com/things/g0UkhvSKylW-copy-of-traffic-light-with-pedestrian-crossing

(对于凌乱的代码也很抱歉,我还不习惯arduinos所以我知道代码不是很好)

int redLight = 13;
int yellowLight = 12;
int greenLight = 11;
int redLight2 = 10;
int yellowLight2 = 9;
int greenLight2 = 8;
int redPin = 7;
int bluePin = 6;
int greenPin = 5;
int redPin2 = 4;
int bluePin2 = 3;
int greenPin2 = 2;

void setup() {
  pinMode(redLight, OUTPUT);
  pinMode(yellowLight, OUTPUT);
  pinMode(greenLight, OUTPUT);
  pinMode(redLight2, OUTPUT);
  pinMode(yellowLight2, OUTPUT);
  pinMode(greenLight2, OUTPUT);
  pinMode(redPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(greenPin, OUTPUT);
}

void loop() {
  //Note: leftmost traffic light is used for refrence
  //all other lights are based on each others timing
  while(digitalRead(redLight) ==HIGH){
    redLightOn();
    break;
  }
  while(digitalRead(greenLight) == HIGH){
   //pedestrian light is white
    digitalWrite(redPin, 255);
    digitalWrite(greenPin, 255);
    digitalWrite(bluePin, 255);
    //pedestrian light is off
    digitalWrite(redLight2, HIGH);
    delay(6000);
    digitalWrite(bluePin, 0);//make sure to turn blue pin off!
    digitalWrite(redPin, 255);
    digitalWrite(greenPin, 40);
    delay(1000);
    digitalWrite(redPin, 0);
    digitalWrite(greenPin, 0);
    delay(500);
    digitalWrite(redPin, 255);
    digitalWrite(greenPin, 40);
    delay(1000);
    digitalWrite(redPin, 0);
    digitalWrite(greenPin, 0);
    delay(500);
    digitalWrite(redPin, 255);
    digitalWrite(greenPin, 40);
    delay(1000);
    digitalWrite(greenLight, LOW);
    digitalWrite(redPin, 0);
    digitalWrite(greenPin, 0);
    digitalWrite(bluePin, 0);
    digitalWrite(yellowLight, HIGH);
    //pedestrial light flashing
    digitalWrite(redPin, 255);
    digitalWrite(greenPin, 40);

  delay(5000);
    //yellow light changes to red
    digitalWrite(yellowLight, LOW);
    digitalWrite(redLight, HIGH);
  }

//Initial starting light: starts off red
  digitalWrite(redLight, HIGH);
}
//create an initial red light function
void redLightOn(){
    digitalWrite(redLight2, LOW);
    digitalWrite(greenLight2, HIGH);
    digitalWrite(redPin2, 255);
    digitalWrite(greenPin2, 255);
    digitalWrite(bluePin2, 255);
    //pedestrian light on white
    digitalWrite(redPin, 255);
    digitalWrite(greenPin, 40);
    //pedestrian light is off
    delay(6000);
    digitalWrite(bluePin2, 0);//make sure to turn blue pin off!
    digitalWrite(redPin2, 255);
    digitalWrite(greenPin2, 255);
    delay(1000);
    digitalWrite(redPin2, 0);
    digitalWrite(greenPin2, 0);
    delay(500);
    pYellow();
    delay(1000);
    digitalWrite(redPin2, 0);
    digitalWrite(greenPin2, 0);
    delay(500);
    pYellow();
    delay(1000);
    digitalWrite(greenLight, LOW);
    //digitalWrite(redPin, 0);
    //digitalWrite(greenPin, 0);
    //digitalWrite(bluePin, 0);
    digitalWrite(greenLight, LOW);

    //pedestrial light off
  
    digitalWrite(greenLight2, LOW);
    digitalWrite(yellowLight2, HIGH);
    delay(5000);
    digitalWrite(yellowLight2, LOW);
    digitalWrite(redLight, LOW);
    digitalWrite(greenLight, HIGH);
}

void pYellow(){
    digitalWrite(redPin2, 255);
    digitalWrite(greenPin2, 40);
}

我看不出你的代码有什么问题。实际上这不是软件问题而是物理问题。如果您测量 LED 两脚之间的电压,您可能会看到电压根据您的代码发生变化。问题是亮度是您用眼睛测量的东西。有一些情况,比如你从同一个供应商那里购买了不同颜色的 LED,但亮度似乎不同。

如果你想获得所有颜色的均匀亮度,你可以尝试通过代码将较浅的颜色变暗或尝试在腿上使用不同值的电阻器。

您似乎对 digitalWrite 和 analogWrite. The digitalWrite 函数采用 PIN 号和 HIGH 或 LOW 感到困惑。在 undefined 中传递一个整数值。我猜整数零将关闭,任何其他值将打开。

您似乎想将引脚设置为模拟引脚,然后使用 analogWrite 更新它们。使用此命令,您可以使用值 0 - 255 设置 0 - 5v 之间的任何电压电平。

还有一个检查它是代码还是硬件的好方法,就是将 LED 的黄色引脚简单地连接到 5V 电源线上,然后查看它的亮度。最后,您是否为 LED 配备了一个公共下拉电阻,或者您是否为每种颜色配备了单独的电阻?如果是这样,其中一个可能是错误的值,或者您在黄线上的某处有一个不可靠的连接。