Basic Problem with RGB configuration

Hi guys,

I'm new into Arduino and I am currently facing the first struggles with the code :grinning: .

Well, I plugged an rgb led to a broadboard and I'm trying to figure out what's the function of the highlighted code in the code below. I don't know what it does. Could you be so kind to explain me its specific chore and wether it's compulsory to include it?

// Define Pins
#define BLUE 3
#define GREEN 5
#define RED 6

void setup()
{
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
digitalWrite(RED, HIGH);
digitalWrite(GREEN, HIGH);
digitalWrite(BLUE, HIGH);
}

// define variables
int redValue;
int greenValue;
int blueValue;

// main loop
void loop()
{
#define delayTime 5// fading time between colors

redValue = 255;// choose a value between 1 and 255 to change the color.
greenValue = 0;
blueValue = 0;

// this is unnecessary as we've either turned on RED in SETUP
// or in the previous loop ... regardless, this turns RED off
// analogWrite(RED, 0);
// delay(1000);

for(int i = 0; i < 255; i += 1) // fades out red bring green full when i=255
{
redValue -= 1;
greenValue += 1;
// The following was reversed, counting in the wrong directions
// analogWrite(RED, 255 - redValue);
// analogWrite(GREEN, 255 - greenValue);
analogWrite(RED, redValue);
analogWrite(GREEN, greenValue);
delay(delayTime);
}

redValue = 0;
greenValue = 255;
blueValue = 0;

for(int i = 0; i < 255; i += 1) // fades out green bring blue full when i=255
{
greenValue -= 1;
blueValue += 1;
// The following was reversed, counting in the wrong directions
// analogWrite(GREEN, 255 - greenValue);
// analogWrite(BLUE, 255 - blueValue);
analogWrite(GREEN, greenValue);
analogWrite(BLUE, blueValue);
delay(delayTime);
}

redValue = 0;
greenValue = 0;
blueValue = 255;

for(int i = 0; i < 255; i += 1) // fades out blue bring red full when i=255
{
// The following code has been rearranged to match the other two similar sections
blueValue -= 1;
redValue += 1;
// The following was reversed, counting in the wrong directions
// analogWrite(BLUE, 255 - blueValue);
// analogWrite(RED, 255 - redValue);
analogWrite(BLUE, blueValue);
analogWrite(RED, redValue);
delay(delayTime);
}
}

Thank you very much for your time!

Ivan V.

With analogWrite you are using PWM, i.e. pulsing the output on/off at a fast rate, 255 means it is at +5V all the time 0 means at 0V all the time, any value in between will spend proportionally more or less time at either value, making the LED glow more or less bright.

What awful code!

Please reassure us that you were/ are using resistors in series with the LEDs?

and I'm trying to figure out what's the function of the highlighted code in the code below.

Assuming you mean the code in red, then they are setting the initial three brightness levels for each led colour. The code then fades down one component from that value to off over a time given by the delay, and at the same time fades up another colour to full brightness.

As each section of code only uses two colour components, the third colour which is assigned a value but is not used can be removed.

The code is very bad, and shows a lack of understanding about coding.

Thank you guys!

I just copied it from the Elegoo Kit tutorial n°4. It was exactly in that way. The thing was that I didn't understand the red code function, but now it's clear. Thanks again!

This bit is bad for all sorts of reasons

#define delayTime 5// fading time between colors

Anyone who wrote that and put it in a tutorial needs shooting.
It should be at the very top of the program, not in the loop function. But actually if you should use

int delayTime = 5;

Either where it is or at the top of the program.

Grumpy_Mike:
It should be at the very top of the program, not in the loop function. But actually if you should use

int delayTime = 5;

Either where it is or at the top of the program.

That is also fairly bad coding, you are asking the compiler to reserve two bytes of RAM memory for a constant.

const int delayTime = 5;
const byte delayTime = 5;

Is a better definition. This way the compiler knows that no RAM is needed and that it directly can use the value. On a 8 bit CPU the byte definition may make more compact code, but the compiler is probably smart enough to generate the same code with both definitions.

Note: An optimizing compiler may be smart enough to automatic add the missing const.

That is also fairly bad coding, you are asking the compiler to reserve two bytes of RAM memory for a constant.

Rubbish.
That is giving you the option to extend the delay longer than a quarter of a second.

It is using the const that is bad, almost as bad as the original code.

Grumpy_Mike:
Rubbish.
That is giving you the option to extend the delay longer than a quarter of a second.

It is using the const that is bad, almost as bad as the original code.

Of course you remove the "const" if you add some way to change the value, but as long as there is no code to change the value const is the correct way. It may generate better code and it shows anyone reading the code that you do not change the value.

But that is more suited to things that won’t change like pin assignments.

Grumpy_Mike:
But that is more suited to things that won’t change like pin assignments.

When you have no code to change the value, it will not change without a recompilation (There are exception to that and they are also a good argument for using const).
It can be necessary to change any value, also pin assignments. Using the const indicates that the program do not change the value, as I wrote above it can help the compiler generating better code and it can help the programmer maintaining the code. The part about the programmer is very important if you need to modify a older code, it makes it easier when you know what the program do and do not change.

I think the conversation is more about you then trying to help the OP. You are getting too hunger up and precious about this.

Grumpy_Mike:
I think the conversation is more about you then trying to help the OP. You are getting too hunger up and precious about this.

You stated by complaining about a #define and then showed a "fix" that was worse, I tried to correct that with a explanation about why it is worse and how to do it correctly.
Way to many people do not realize that "int a=5" is significantly different from "const int a=5".

Look this guy did not know what these lines did:-

redValue = 255;// choose a value between 1 and 255 to change the color.
greenValue = 0;
blueValue = 0;

Piffling about the difference between an int and a const int it just taking anal retention to its limits. Have a ******* sense of perspective will you.

As far as I am concerned I am here to help beginners not debate subtle points with people like you who are on some sort of an ego trip. Get a grip.

I will no longer be reading this thread so don't bother to reply.

I'll do: Grumpy ... YOU started to "optimize" a part of the sketch the TO didn't ask for.
just live with it, that there might be other solutions, which might be better for others.

As you all know, the compiler will do a pretty good job anyway.

compiled for the UNO:

#define delayTime 5// fading time between colors
Der Sketch verwendet 1432 Bytes (4%) des Programmspeicherplatzes. Das Maximum sind 32256 Bytes.
Globale Variablen verwenden 15 Bytes (0%) des dynamischen Speichers, 2033 Bytes für lokale Variablen verbleiben. Das Maximum sind 2048 Bytes.

int delayTime = 5;
Der Sketch verwendet 1432 Bytes (4%) des Programmspeicherplatzes. Das Maximum sind 32256 Bytes.
Globale Variablen verwenden 15 Bytes (0%) des dynamischen Speichers, 2033 Bytes für lokale Variablen verbleiben. Das Maximum sind 2048 Bytes.

const int delayTime = 5;
Der Sketch verwendet 1432 Bytes (4%) des Programmspeicherplatzes. Das Maximum sind 32256 Bytes.
Globale Variablen verwenden 15 Bytes (0%) des dynamischen Speichers, 2033 Bytes für lokale Variablen verbleiben. Das Maximum sind 2048 Bytes.

const byte delayTime = 5;
Der Sketch verwendet 1432 Bytes (4%) des Programmspeicherplatzes. Das Maximum sind 32256 Bytes.
Globale Variablen verwenden 15 Bytes (0%) des dynamischen Speichers, 2033 Bytes für lokale Variablen verbleiben. Das Maximum sind 2048 Bytes.

const uint16_t delayTime = 5;   // you will not need signed numbers for delays
Der Sketch verwendet 1432 Bytes (4%) des Programmspeicherplatzes. Das Maximum sind 32256 Bytes.
Globale Variablen verwenden 15 Bytes (0%) des dynamischen Speichers, 2033 Bytes für lokale Variablen verbleiben. Das Maximum sind 2048 Bytes.

now, let's have a sunny Saturday.