简体   繁体   中英

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). The first light works perfectly but the RGB on the left isn't working correctly. 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

(also sorry for the messy code, I'm not that used to arduinos yet so I know the code isn't great)

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 . The digitalWrite function takes a pin number and either HIGH or LOW. Passing an integer value as you are in undefined. I would guess that an integer of zero will be off and any other value will be on.

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.

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. Finally have you got a single common pull down resistor for the LED or have you got separate resistors for each color? 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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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