The coding in the lessons goes way beyond any explanation

In Lesson 4 RGB LED the loop program is not explained. I want to learn what is happening but can't make out what is going on or how to change it.

The directions imply I can set specific colors by changing the values of red, green and blue between 0-255 but the loop takes control and I have no idea how to start with simple color sets then learn how to do the more complex transitions.

Post the code. Explain what it does now, which parts you do not understand, and what you would like the code to do. Be as specific as you can.

This makes the LED fade gradually between colors. The lesson talks about simply being able to create any color by varying the values from 0-255 of the 3 colors, which would be great to try BEFORE adding all the complex fading. But there is too much going on for me to figure what commands are doing what.

// 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, LOW);

digitalWrite(BLUE, LOW);

}

// define variables

int redValue;

int greenValue;

int blueValue;

// main loop

void loop()

{

#define delayTime 10 // 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);

}

}

In what learning materials is this lesson 4 to be found?

Lessons 0, 1, 2 and 3 should have prepared you.

// The following was reversed, counting in the wrong directions

// analogWrite(RED, 255 - redValue);

// analogWrite(GREEN, 255 - greenValue);

If that is your comment, the materials did not explain that the expression

    255 - redValue

will indeed run from 255 to 0 as redValue runs from 0 to 255, and vice versa. This basically turns a fade up into a fade down… and was very likely the exact intent of the sketch writer.

So far I am not impressed with what we know about your lessons as seen by a typical user (you).

a7

When you post code, use the <CODE/> button in the message composition window, and you will see

type or paste code here

paste you code there replacing the directive type or paste code here.

Then your code will look like code

void setup() {

}

void loop() {

}

TIA

a7

1 Like

Reformatted with code tags:

// 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, LOW);
  digitalWrite(BLUE, LOW);
}
// define variables
int redValue;
int greenValue;
int blueValue;
// main loop
void loop()
{
#define delayTime 10 // 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);
  }
}

Where is this Lesson 4 code from? The commented out The following was reversed, counting in the wrong directions stuff is due to the difference between a Common Cathode versus Common Anode RGB led wiring.

1 Like

would that help ?

(if you understand what PWM does and how mixing basic red, green blue hues lead you to perceive a combined color)

// Define Pins
const byte bluePin = 3;
const byte greenPin = 5;
const byte redPin = 6;

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

  // RED
  analogWrite(redPin, 255); // full red
  analogWrite(greenPin, 0); // green off
  analogWrite(bluePin, 0); // blue off
  delay(2000);

  // ORANGE
  analogWrite(redPin, 255); // full red
  analogWrite(greenPin, 165); // medium green
  analogWrite(bluePin, 0); // blue off
  delay(2000);

  // YELLOW
  analogWrite(redPin, 255); // full red
  analogWrite(greenPin, 255); // full green
  analogWrite(bluePin, 0); // blue off
  delay(2000);

  // GREEN
  analogWrite(redPin, 0); // red off
  analogWrite(greenPin, 255); // full green
  analogWrite(bluePin, 0); // blue off
  delay(2000);

  // BLUE
  analogWrite(redPin, 0); // red off
  analogWrite(greenPin, 0); // green off
  analogWrite(bluePin, 255); // full blue
  delay(2000);

  // INDIGO
  analogWrite(redPin, 75); // low red
  analogWrite(greenPin, 0); // green off
  analogWrite(bluePin, 130); // medium blue
  delay(2000);

  // VIOLET
  analogWrite(redPin, 138); // medium red
  analogWrite(greenPin, 43); // medium green
  analogWrite(bluePin, 226); // full blue
  delay(2000);

}

void loop() {}

Oh!

That always puts me a certain mood.

a7

1 Like

Hi @jeffurr,

did you have a look at this tutorial?

https://docs.arduino.cc/tutorials/generic/secrets-of-arduino-pwm#introduction

You can stop after "Simple Pulse Width Modulation with analogWrite()" for the beginning as the rest goes into details you might be interested in later!

From this link (a zip file download is launched):
https://download.elegoo.com/?t=Neptune2_2D_2S_Software_User_Guide
This document, page 44:
Basic Starter Kit for UNO V1.0.2019.07.24.pdf (7.7 MB)

1 Like

Thanks to all the respondents.
Lesson three - use the arduino +5 and GND to light an LED. (no code)
Lesson 5 2 switches and a light.
Lesson 4 is RIDICULOUS. I can understand the OP's confusion.
Its FAR too complicated, and the comments are worse than useless.

@jeffurr - try J-M-L code from #7, hopefully you will find it works and is easy to understand.

Thank you J-M-L, what you wrote, I can understand and it accomplished what the lesson seemed to be implying was its goal. I am now am able to use it to vary the PWM intensitys to create whatever colors I want. As a beginner it is sad for me that your approach is so different from the lesson so it is challenging for me to integrate it with what they were coding. For instance you used:
const byte
to replace:
#define
I have know idea why or what the difference is. Do you know of a guide that has clearer explanations, with examples, so I can actually understand what I am doing? Thank you

Thank you for your response Johnerrington, Yes, RIDICULOUS is the perfect word for Lesson 4... and yes, I was able to accomplish what I thought were lesson 4s goal with the code for J-M-L. Just wish the lessons were actually compiled in a way that was actually understandable...

Thank ec2021, I took a look at the tutorial though I am to much of a beginner to extrapolate that description without a clear example... it makes sense in principle, but to many things have to be right to get it to work as a code. Fortunately, J-M-L gave a a nice coded example that actually worked, now I just have to figure out all the stuff he used with analogWrite()! If you know of any wellwritten step by step learning guide, I love to know about it. Saddly, that is what I thought I was buying....

I’m glad if that helped you understand better how to handle colors and what PWM does.

If you have not taken an introduction tuto C/C++ programming, I think that’s your next step otherwise it’s like trying to speak or decrypt a foreign language without having an understanding about the vocabulary nor the grammar.


To your question #define is what’s called a macro. It’s used to associate a keyword to some text.

When you compile the sketch before the actual compilation there is a pre-processing stage where all the keywords in your code are replaced textually by the text you provided. See that as you were doing a find and replace in a text editor. Then the compilation starts. It means there is no understanding by the compiler about what those keyword meant.

The programming language has variables that can hold some value. Basic values are typed (an integral number, a floating point number, if the number is always positive, a truth value, …). When you define a variable you tell the compiler about the type which helps knowing how many byte of memory are needed to represent your values and the associated range. On a UNO a byte will use one byte and the value range is 0 to 255. An int is signed, uses 2 bytes and can represent values between -32768 to 32767, etc…

You can tell the compiler that the value you assign to the variable at compile time will never change. That’s what the const keyword is saying to the compiler.

When I do
const byte ledPin = 2;
I’m giving lots of information to the compiler. It knows that it will need 1 byte for ledPin that the value will never change and is always positive. This information can help the optimizer generate a more efficient/ effective code at compile time, including deciding not to allocate the memory and replace directly the value in function calls as it knows the value won’t change. The compiler will also bark at me if I try to do something like
ledPin = 8;
Later in the code. I promised the compiler the variable would never change so I can’t do that and the compiler will remind me my promise and tell me this is likely a bug.

So in a nutshell, it’s a good practice in strongly typed languages to give as much information as possible to the compiler.

2 Likes

LOL Yes, from what I am getting from the other posters, it is pretty clear lessons 1-3 did not prepare me in a way that allowed me to "get" lesson 4. And yes, it told me I was doing it wrong when I was posting... though being a brand new beginner, im still not quite sure what you are saying... oh well, I guess that is my main problem,.. how to learn things, when there are things I am already suppose to know, that I dont know, to learn the things I am trying to learn. Thanks for your interest!

You can learn a lot from the examples in the IDE
image
I'd suggest you try this one "Fade"
when its working you can change values such as in this part

for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += **5** ){
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(**30**);

you can change the 5 to another value - eg 1; or change the
delay(30); to delay(50);

Then look at other examples in the "Basics" section.

J-M-L, I greatly appreciate you taking the time to try to explain this to me... Unfortuately, I am a brand new beginner to all this. Your analogy of trying to decrypt a foriegn Language is apt in this instance. I look forward to the day I can have an understanding of what you are trying to explain to me. As for now, it is way over my head. I will save what you wrote and revisit it periodically to see if I can gleen more meaning from it. Thanks again for your efforts to help me! I will look into an into couse in introduction tuto C/C++ programming. I bought this electronics kit because it said it was good for beginners to learn with, thinking it would guild me gradually... sad to think I need to take a course in programming to use a beginners electronics kit. oh well...

Thank you! I will check that out. Looks like it could be a good way for me to build my knowledge!

There are less an less systems where you just combine electronic components to get the project done. Most systems nowadays include some sort of "computer". The arduino environment is targeting this latter case - probably with more emphasis on programming than electronics itself as we tend to buy ready made modules offering a programming interface.

Whists learning C++ takes a bit of time, you'll see it's very rewarding when you understand what you are doing and can build your own very first project. Hang in there!