-1

Im trying too make an RGB LED that keeps changing color's randomly, and makes the brightness fade at the same time with an LDR, yet I keep getting an error that says too many arguments to functions.

could anyone possibly help me?

EDIT: (this is the correct code sorry)

 const int LED_D_RGB_G_PIN = 11;
const int LED_D_RGB_B_PIN = 10;
const int LED_D_RGB_R_PIN = 9;
const int LDR = A1; 

int inputval=0; 
int outval =0; 

void setup() {
  pinMode(LED_D_RGB_R_PIN, OUTPUT);
  pinMode(LED_D_RGB_G_PIN, OUTPUT);
  pinMode(LED_D_RGB_B_PIN, OUTPUT);
  pinMode(LDR, INPUT);

}

void loop() {
    inputval = analogRead(LDR); 
    outval = map(inputval, 0, 380, 255, 0); 

    int lightval = constrain(outval, 0,255);

    analogWrite(LED_D_RGB_R_PIN, lightval, random(0,255));
    analogWrite(LED_D_RGB_G_PIN, lightval, random(0,255));
    analogWrite(LED_D_RGB_B_PIN, lightval, random(0,255)); 
    delay (50);
}
6
  • 2
    If you get an error during compilation, please include the full error (including the information where the error happened) in the question. Also: Doing two analogWrite calls on the same pins directly after another will not work. Only the second call for each pin will have any effect.
    – chrisl
    Feb 8, 2023 at 15:52
  • 1
    you have a logic error in your code ... you want to choose a random color, and then adjust the brightness of that color ... that is not what happens in your code ... the code sets the three LEDs to random brightness... that sets a random color and random brightness at the same time ... you have to choose a random full brightness color and then adjust the brightness without changing the color
    – jsotola
    Feb 8, 2023 at 16:33
  • I want it to keep changing colors randomly with the RGB while at the same time adjusting the brightness with an LDR. I just dont know how to make them do it separately. also I accidently unloaded the wrong code. sorry. The code I had the problem with was with this at the end: analogWrite(LED_D_RGB_R_PIN, lightval, random(0,255)); analogWrite(LED_D_RGB_G_PIN, lightval, random(0,255)); analogWrite(LED_D_RGB_B_PIN, lightval, random(0,255)); delay (50); } Feb 8, 2023 at 20:19
  • 2
    You need to put that code into your question, not into the comments where we cannot read it easily. So is there still a compilation error? Please inprove your question by editing it
    – chrisl
    Feb 8, 2023 at 22:51
  • analogWrite() accepts two arguments ... analogWrite(LED_D_RGB_R_PIN, lightval, random(0,255)); has three arguments ... that's why you received the compiler error message
    – jsotola
    Feb 9, 2023 at 0:12

1 Answer 1

1

I will assume that you can correct any errors in your code and I want to get at a suggestion for a possibly better approach to what I think that you are trying to do.

When you are setting the "color", you are setting the brightness of each of the LEDs; R,G,B, or at least you are trying to do that when you generate a random PWM using analogWrite https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/.

Then, you are trying to set the brightness again, presumably based on the ambient light value from the LDR AND it is set for the combined output of the 3 LEDs which are likely at different intensity values, since intensity was randomly chosen. The problem is, how can you scale the values perfectly so that you get the same color but at a lower intensity? I think that is much more difficult than you might believe...for a bunch of reasons.

I would suggest you try something along these lines, but keep some low expectations....

  1. Use a variable called maxb, which changes the maximum possible value used in the analogWrite() values so that it it not always 255. You can adjust that value based on reading the LDR. You will have to do some experimenting and don't expect a huge range of values. Now, your color will still be picked at random, but with an adjustment for intensity.

  2. You "should" try to use resistors on each of the RGB LEDs that will produce something close to equivalent brightness. This will vary somewhat between RGB LEDs. Below is a graph of where I looked at this for a color fader project many years ago. I used an old BH1750 ambient light sensor to get the lux values. That dashed line is for the equivalent brightness of the individual LEDs by resistor value (ohms) used [220-Red, 750-Green, 470-Blue]. This is not an ideal way, but it is easy.

enter image description here

  1. That might get you near where you want to be, but for "extra credit", note that brightness scaling is not an equal interval scale. So, a PWM value of 250 is NOT 10 times brighter than a PWM value of 25. Brightness is, more or less, on an exponential scale except for very dim values, which are, again, more-or-less, linear. Look into the CIE 1931 perceived lightness formula and Weber and Stevens and so on. Since this is not a psychophysics lab, let me cut to the chase. The array below represents an intensity scale as I have described and includes values of 0 and 255 as per an 8 bit width (which is what analogWrite uses). You could use a minimum value of 4 or 8 instead of 0 if you wanted to guarantee a visible output every time. Test that out, but for the sake of discussion, let's assume you are using all values in the array.

A[13]{0,1,2,4,8,17,32,51,75,106,145,194,255};

You would scale your LDR range to 13 possible values (0-12) and then get your maximum value for analogWrite from the array. Then, you would pick a random value for 0 to maxb for each of your analogWrites to the RGB.

  1. I don't know how brightness is represented by your LDR and you should consider that when you are determining your ambient light intensity values - ideally, you would want 13 equal intensity values from your LDR - right?. I suppose something about that is probably in the data sheet.

That is how I would do it anyways :)

1
  • Thank you! this helps a lot! I didn't think it would be easy from the start but this helped me understand just how difficult it is. I will definitely start to experiment with the code some more and hopefully ill get a satisfactory result! Feb 9, 2023 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.