PWM N00b question

Hi folks,

I am pretty new to Arduino and very new to the site. I have multiple questions but I will reserve this post for a specific challenge that I am having with. I am currently experimenting with the code for PWM exercise from Massimo Banzi’s book “Getting Started with Arduino”. Here is a link to the code that I am trying to modify:

What I am trying to do, is modify the code such that I can have 3 LEDs fade in and out such that the second LED fades in when the first one’s brightness reaches 50%, and the third LED starts to fade in when the second LED reaches 50% brightness. According to the book, analogWrite(x, 128) will set an LED to approximately 50% of its brightness since it accepts a value between 0-255.

Based on this I was thinking that I could use the following logic in my code:

#define LED1 9
#define LED 2 10
#define LED3 11

int i = 0; //brightness counter for LED 1
int j = 0; //brightness counter for LED 2
int k = 0; //brightness counter for LED 3

void setup(){

pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);

void loop(){

for (i = 0; i < 255; i++){
digitalWrite(LED1, i); //fade in LED 3

…now here is where I try to change things such that LED2 starts when LED 1 gets to 50%. I use the same method for LED 3 so I don’t think there is any need for me to paste the entire sketch here:

for(i = 128; i < 255; j++){ //here I am trying to tell j to start incrementing once i reaches 128 (50%)
digitalWrite(LED2, j);

The result isn’t what I expected. Instead of fading up to full brightness LED 2 seems to loop at a halfway point and the code never appears to get to LED3. I know that I may need to state somewhere that j should continue to increment until its own counter reaches 255, but I am not sure where to put it. On the other hand I may be totally off track here.

In any case my goal is to learn by experimenting with the code in the book in an effort to increase my understanding. What I was hoping to see was the LEDs fading in and out and the combination of a red blue and green LEDs creating different colors.

As I said, I am new at all this so any guidance or recommendations are greatly appreciated.



Can you post the actual code you're using? Use the # icon to give a code box

Awol, thanks for the response see the actual code that I am running at this point below:

#define LED  9     //pin 9 will be used for LED
#define LED2 10  //pin 10 will be used for LED
#define LED3 11  //pin 11 will be used for LED

int i = 0; //variable to count up and down for each LED
int j = 0;
int k = 0;

void setup(){
  pinMode (LED, OUTPUT); //LEDs configured as OUTPUTs
  pinMode (LED2, OUTPUT);
  pinMode (LED3, OUTPUT);

void loop(){
  for (i = 0; i < 255; i++) {      // loop from 0 to 254 (fade in)
  analogWrite (LED, i);           //set LED brightness
for (i = 128; i < 255; j++){    //once i has reached 50% j should start incrementing
analogWrite (LED2, j);         //set LED brightness

for (i = 255; i > 0; i--)             {//loop from 255 to 1 (fade out)
analogWrite (LED, i);              //set LED brightness
delay(10);                             //wait 10ms
for (j = 255; j < 0; j++){
analogWrite (LED2, j);
for (j = 128; j < 255; k++){
analogWrite (LED3, k);
for (k = 0; k < 255; k++){
  analogWrite (LED3, k);
for (k = 255; k > 0; k--){
analogWrite (LED3, k);

Take a careful look at your second for loop.

pinMode isn't necessary for pin for analogWrite

Are you referring to the following line?: for (i = 128; i < 255; j++)

What I am trying to say here is that j should start to increment once i = 128; However now that I am looking closely at this, I realize that LED2 would only ever get to 50% brightness since its incrementation is restricted to i being between 128 and 255. So the question is, how do I get j to start incrementing at the correct time and at the same time allow it to reach its maximal potential brightness?

Is this the same problem that you see?

Is this something that I can resolve within my current coding skill set as a beginner? I appreciate you helping me to figure this out on my own.

I don't have your hardware, and I don't have your specification, so I don't see the problem, I just see an unusual programming construct.

void loop(){
  for (i = 0; i < 255; i++) {      // loop from 0 to 254 (fade in)
  analogWrite (LED, i);           //set LED brightness

This loop should work fine.

for (i = 128; i < 255; j++){    //once i has reached 50% j should start incrementing
analogWrite (LED2, j);         //set LED brightness

This loop isn’t going to start until after the previous one finishes. It will set i =128 and then keep incrementing j until i is no longer less than 255. That’s never going to happen because this loop never increments i, so it will always be less than 255.

I think what you want is all of these things inside a single for loop.

Thank you both for your responses. I managed to get the code to behave the way I wanted without all the silly weird construct stuff. I all I needed to do was work with the delay(); function and the order in which I had the LEDs fade in and fade out.

By having LED2 fade in before LED1 fades out I get the effect of the colors blending as desired. I guess it's a common newb mistake to make things harder than they need to be :blush: ..Thanks for your time folks much appreciated

This is a whole different way to do this, but I did this a long time ago.
I also like this because you can do MANY LEDs in one loop.
Say you want to start with…
LED0 at 0, going up
LED1 at 128, going up
LED2 at 255 going down

Keep the “ups” going higher until they reach 255, then reverse direction. Keep the “downs” going lower until they reach zero, then reverse direction, so initialize with something like…

led[0] = 0;     
led[1] = 128;
led[2] = 255;
direction[0] = 1;
direction[1] = 1;
direction[2] = -1;

now you can add or subtract and OUTPUT all in one loop
(AND for any number of LEDs!)

firstPWM_Pin = 2;
loop forever
  for(i = 0; i < 3; ++i)
    led[i] += direction[i];                      // Add or subtract one
    analogWrite(firstPWM_Pin + i, led[i]; // OUTPUT the appropriate voltage
    if((led[i] > 254) || (led[i] < 1))        // Are we at the end of the range?
      direction[i] = -direction[i];            // If so, change direction