RGB & LDR Brightness

How to program Arduino to simulate the use of an LDR to change RGB LED brightness?

int i = 0;
int counter;
void setup()
{
 pinMode(0, INPUT);
 pinMode(11, OUTPUT);
 pinMode(10, OUTPUT);
 pinMode(9, OUTPUT);
}
void loop()
{
 digitalRead(0);
 analogWrite(11, 255);
 analogWrite(10, 102);
 analogWrite(9, 102);//pink
 delay(1000); // Wait for 1000 millisecond(s)
 analogWrite(11, 102);
 analogWrite(10, 255);
 analogWrite(9, 255);//cyan
 delay(1000); // Wait for 1000 millisecond(s)
 analogWrite(11, 255);
 analogWrite(10, 0);
 analogWrite(9, 0);//red
 delay(1000); // Wait for 1000 millisecond(s)
 analogWrite(11, 255);
 analogWrite(10, 255);
 analogWrite(9, 51);//yellow
 delay(1000); // Wait for 1000 millisecond(s)
 analogWrite(11, 204);
 analogWrite(10, 51);
 analogWrite(9, 204);//purple
 delay(1000); // Wait for 1000 millisecond(s)
 analogWrite(11, 0);
 analogWrite(10, 0);
 analogWrite(9, 102);//blue
 delay(1000); // Wait for 1000 millisecond(s)
 analogWrite(11, 51);
 analogWrite(10, 51);
 analogWrite(9, 51);//gray
 for (counter = 0; counter < 10; ++counter) {
 }
}

Tell us what this does ?


digitalRead(0);

Your topic was MOVED to its current forum category as it is more suitable than the original

that was supposed to be analogread A0

Still the same question what were you expecting it to do? A read statement is normally on the other side of a =, setting the variable given before the = to the value that is read.

In fact even analogread A0 is wrong, it should be analogRead(0). The A0 is designed to be used for making digital input / output on an analogue port pin.

However so many people could not wrap their head around that simple concept, they had to change the compile to sort out those using it incorrectly.

Oh so thats not supposed to be included?

Thank you for explaining. This subject is self-study so I still have a lot to learn.

1 Like

Correct, because you never do anything with the result. It is a valid C call which is why you don't get an error, although you might get a warning if you look at all the stuff that is generated during compilation

Yes, but good on you for spotting this and understanding it. A good attitude to take.

But is the code itself good? I included that because the LDR is connected to A0 so I thought it is supposed to be included. What should I do to adjust the RGB LED brightness using the LDR?

Assign the reading from the LDR to a variable, and use the map function to get it into the range 0 to 255. Then use that variable to set one of the components of your RGB colour.

However, if this is a simulator how do you change the light level falling on it. I never use simulators so I don't know.

Do you know of all the training materials available on the main Arduino site?
In total, there's a manual though it's under updating and reformatting.

Those who can look stuff up don't have to wait for as much help.

Do you see how you have code that's the same except for numbers?

 analogWrite(11, 255);
 analogWrite(10, 102);
 analogWrite(9, 102);//pink
 delay(1000); // Wait for 1000 millisecond(s)

 analogWrite(11, 102);
 analogWrite(10, 255);
 analogWrite(9, 255);//cyan
 delay(1000); // Wait for 1000 millisecond(s)

 analogWrite(11, 255);
 analogWrite(10, 0);
 analogWrite(9, 0);//red
 delay(1000); // Wait for 1000 millisecond(s)

You could write a function...

void colorOut( byte red, byte green, byte blue )
{
 analogWrite(11, red);
 analogWrite(10, green);
 analogWrite(9, blue);//red
 delay(1000); // Wait for 1000 millisecond(s)
 return;
}

........ and in void loop()
 colorOut( 255, 102, 102 );

 colorOut( 102, 255, 255 );

 colorOut( 255, 0, 0 );
 

There is supposed to be analogRead(A0) instead of digitalRead(0). Depending on the value of analogRead(A0), the color of the LED must be changed.

how do I set a value for it?

I figured out a way to change the LEDsbrightness by using a darker shade of red to a lighter shade. But I can't use include other colours and I was supposed to include other colours while varying their brightness.

void setup()
{
  pinMode(A0, INPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
}

void loop()
{
  if (analogRead(A0) > 100) {
    analogWrite(11, 255);
    analogWrite(10, 0);
    analogWrite(9, 0);
  }
  if (analogRead(A0) < 800) {
    analogWrite(11, 255);
    analogWrite(10, 102);
    analogWrite(9, 102);
  }
  if (analogRead(A0) < 900) {
    analogWrite(11, 255);
    analogWrite(10, 204);
    analogWrite(9, 204);
  }
  delay(10); // Delay a little bit to improve simulation performance
}

like this?

int greenPin = 9;
int bluePin = 10;
int redPin = 11;

void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);  
}
 
void loop()
{
  setColor(255, 0, 0);  // bright red
  delay(1000);
  setColor(255, 50, 0);  // orange
  delay(1000);
  setColor(255, 204, 204);  // pink
  delay(1000);
  setColor(51, 102, 255);  // blue
  delay(1000);
  setColor(102, 255, 153);  //  cyan
  delay(1000);
  setColor(80, 0, 80);  // purple
  delay(1000);
  setColor(80, 24, 80);  // light purple
  delay(1000);
}
 
void setColor(int red, int green, int blue)
{
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);  
}

Mostly, maybe tuck the delay() into the function and add a parameter for millis so you can have timed color and less typing.

With Arduino, RAM is precious. Make it a habit, use the smallest variable for the job.
Once you do that, it will become automatic.
Type int is 2 bytes, type byte is 1 byte.
Type int counts from -32768 to +32767 and type byte counts from 0 to 255.

What variable type should pin numbers take?

I'd like you to see that this one new-to-you thing shortens the code and practice but while you're at it, every time the code grows -- there might be a thing that will save all that work that your time is better spent finding than pushing along the hard way.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.