简体   繁体   English

让 RGB LED 灯更亮?

[英]Make RGB LED Light up brighter?

I've been trying to make a traffic light simulator but am having trouble with making one RGB light up brightly (in the code it's referred to with (colour)Pin2).我一直在尝试制作一个红绿灯模拟器,但无法使一个 RGB 灯亮起来(在代码中它被称为(颜色)Pin2)。 The first light works perfectly but the RGB on the left isn't working correctly.第一个灯工作正常,但左侧的 RGB 工作不正常。 I succeeded in making it bright when the light is white, but when I try to make it yellow the light is so dim I can barely see it.当灯是白色的时候,我成功地把它弄亮了,但是当我试图把它变成黄色时,灯太暗了,我几乎看不到它。 It's wired correctly so I think there may be an error in the code.它接线正确,所以我认为代码中可能有错误。 Here's the link to the project if anyone needs it https://www.tinkercad.com/things/g0UkhvSKylW-copy-of-traffic-light-with-pedestrian-crossing如果有人需要,这是该项目的链接https://www.tinkercad.com/things/g0UkhvSKylW-copy-of-traffic-light-with-pedestrian-crossing

(also sorry for the messy code, I'm not that used to arduinos yet so I know the code isn't great) (也很抱歉代码混乱,我还没有习惯 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);
}

You seem to be getting confused between digitalWrite and analogWrite .您似乎对 digitalWrite 和analogWrite感到困惑。 The digitalWrite function takes a pin number and either HIGH or LOW. digitalWrite function 采用引脚号和高电平或低电平。 Passing an integer value as you are in undefined.在未定义时传递 integer 值。 I would guess that an integer of zero will be off and any other value will be on.我猜想零的 integer 将关闭,任何其他值都将打开。

It looks like you want to setup the pins as analog pins then update them with analogWrite.看起来您想将引脚设置为模拟引脚,然后使用模拟写入更新它们。 Using this command you can set any voltage level between 0 - 5v using the values 0 - 255.使用此命令,您可以使用值 0 - 255 设置 0 - 5v 之间的任何电压电平。

Also a good way of checking if it's code or hardware is to simply connect the yellow pin of the LED to the 5V power line and see how bright it gets.检查它是代码还是硬件的一个好方法是简单地将 LED 的黄色引脚连接到 5V 电源线,看看它有多亮。 Finally have you got a single common pull down resistor for the LED or have you got separate resistors for each color?最后,您是否有一个用于 LED 的通用下拉电阻器,或者您是否为每种颜色配备了单独的电阻器? If so it's possible one of these is the wrong value, or you have a dodgy connection somewhere on the yellow wire.如果是这样,其中一个可能是错误的值,或者您在黄线上的某处有一个狡猾的连接。

I can't see anything wrong in your code.我看不出你的代码有什么问题。 Actually it's not a software problem but a physics thing.实际上,这不是软件问题,而是物理问题。 If you measure the voltage between the legs of the LED, you will probably see that voltage changes according to your code.如果您测量 LED 脚之间的电压,您可能会看到电压根据您的代码而变化。 The thing is brightness is something you measure with your eye.问题是亮度是你用眼睛测量的东西。 There are some situations like you buy different colors of LEDS from the same supplier but brigthnes seems different.在某些情况下,例如您从同一供应商处购买不同的 colors LED,但亮度似乎不同。

If you want to get even brightness with the all colors, you can try to make lighter colors darker from the code or try to use different value resistors on legs.如果您想通过所有 colors 获得均匀的亮度,您可以尝试从代码中使较轻的 colors 更暗,或者尝试在腿上使用不同值的电阻。

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

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