RGB Led to dim from 0-255-0, first R, then B, then G, but blue goes instead og G

Hey!
I just got my first arduino, and decided to try and make a RGB-led to dim from zero to full to zero first in red, then in blue and then in green. It works for red and blue, but then, instead of green, the blue led goes twice! I appreciate an answer. It is probably really basic, but I cant figure it out

Here is the code:

int red=5; //pin for red led is 5
int green=6; //pin for green led is 6
int blue=9; //pin for blue led is 9
int value=0; //the output value to declare how strong the led will glow (0-255)
int dim=5; //the amount the led will change in strenght each loop
int mycounter=0; //value to keep track of the amount of cycles, so that the arduino knows when to switch to the new color
int mycounterchange=1; //the amount the counter will change for each cycle
void setup() {
  // put your setup code here, to run once:
pinMode(red,OUTPUT); // red light is an output
pinMode(green,OUTPUT); //green light is an output
pinMode(blue,OUTPUT); //blue light is an output
Serial.begin(9600); //seral communication to see if the code is doing what i intended
}

void loop() {
  // put your main code here, to run repeatedly:
Serial.print(value); // printing in serial monitor
Serial.print("   "); // separate led strenght value from counter
Serial.println(mycounter); //printing the counter

if(mycounter<=105){ //105 loops seem to be what is needed to dim from 0 to 255 and down to 0
  analogWrite(green,LOW); //i only want the red light to glow first
  analogWrite(blue,LOW); // ditto
  analogWrite(red,value); // ditto
  value=value+dim; //to make the dim effect, i will add the dim amount to the strenght value from the last loop
  if(value>255){ // to dim down instead of continuing up, the dim amounbt goes negative after the LED output reaches 255
    dim=-dim;
  }
  if(value<0){ //when getting down to 0 output, it starts going up agian
    dim=-dim;
  }
  
}
else if(105<mycounter<=210){ //so for the second led dim from 0 to 255 and back i want the blue light to go.
  analogWrite(green,LOW);
  analogWrite(blue, value);
  analogWrite(red,LOW);

  value=value+dim;
  if(value>255){
    dim=-dim;
  }
  if(value<0){
    dim=-dim;
  }
 
}
else if(210<value<=315){ // for the third dim cycle i want the green to go.
  analogWrite(green,value);
  analogWrite(blue,LOW);
  analogWrite(red,LOW);
  
value=value+dim;
  if(value>255){
    dim=-dim;
  }
  if(value<0){
    dim=-dim;
  }
}
mycounter=mycounter+mycounterchange; //adding one to the counter at the end of each cycle
if(mycounter>315){
  mycounterchange=-mycounterchange; //after the 3 cycles, it will go backwards
}
if(mycounter<0){
  mycounterchange=-mycounterchange; //when reaching 0, it will start over agian
}

delay(30);
}

Counting loops is not what you want to do when fading LED's. You know that there are 255 steps from off to on and if you want your LED to go from dark to bright in one second, then you can calculate the actual brigtness with "CurrentTime - TimeStarted / 1 second * 255". Consider this:

unsigned long start;

void setup() {
  start = millis();
}

void loop() {
  int value = round( (float)(millis() - start) / 1000.0f * 255 );
  if (value < 0) value = 0;
  else if (value > 255) value = 255;
  analogWrite(ledPin, value);
}

This will actually convert a period of time into a percentage of brightness. This is how you normally would fade LED's.

Okei thanks a lot! I assumed there might be a better way :wink:
But anyways, can you see how i could make it work with my method?
Would like to understand why it wont work :slight_smile:

Regards Thor

You don't need to count loops to fade an RGB LED. Instead, you can use for loops. First, you need to create an array of your output pins. Then you can use for loops to cycle through those. It is important to use PWM compatible GPIO pins when you chose outputs.

// Set ledPin[0] as red
// Set ledPin[1] as green
// Set ledPin[2] as blue
const byte ledPins[3] = {9, 10, 11}; // GPIO pin 9, 10 and 11 is all PWM compitable

void setup() {
  
  // Use your outputs in ledPins as output
  for(int i = 0; i <= 2; i++){
    pinMode(ledPins[i], OUTPUT);
  }
}

void loop() {
  
  // Fade to red
  for(int i = 0; i <= 255; i++){
    analogWrite(ledPins[0], i);
    delay(5);
  }

  //Fade from red to green
  for(int i = 0; i <= 255; i++){
    analogWrite(ledPins[0], 255 - i);
    analogWrite(ledPins[1], i);
    delay(5);
  }

  //Fade from green to blue
  for(int i = 0; i <= 255; i++){
    analogWrite(ledPins[1], 255 - i);
    analogWrite(ledPins[2], i);
    delay(5);
  }

  //Fade away
  for(int i = 0; i <= 255; i++){
    analogWrite(ledPins[2], 255 - i);
    delay(5);
  }
}

Please avoid any use of delay's if possible :slight_smile: