Code for RGB LED Modules

Hi,

i recently bought this KY-009 RGB LED module for Arduino with 5050RGB led on it ..

There is no current limiting resistors on KY-009 so some resistors should be added when connecting to Arduino ;

Arduino pin 9 --> 180 Ohm resistor --> Pin 'R' of KY-009 module
Arduino pin 10 --> 100 Ohm resistor --> Pin 'G' of KY-009 module
Arduino pin 11 --> 100 Ohm resistor --> Pin 'B' of KY-009 module
Arduino GND --> pin '-' of KY-009 module

To test the device, i wrote the following code ;

Starts with Green LED is on, cross feeds to Red, cross fades to Blue, cross fades to Green, then to Red-Blue, then to White then back to Green and loops ..
Rather long code but transitions are smooth and no jumps between colours when looping .. Could be an example code for label structures in a code ..

Any suggestions for shortening this code while performing exactly the same lightning pattern on LEDs ??

Thanks …

int redPin = 9;
int grnPin = 10;
int bluPin = 11;

int redVal = 0;
int grnVal = 255;
int bluVal = 0;

void setup() {
  
  pinMode(redPin, OUTPUT);
  pinMode(grnPin, OUTPUT);
  pinMode(bluPin, OUTPUT);
  }

void loop(){
  
  shiftToRed:
  analogWrite(grnPin, grnVal);
  analogWrite(redPin, redVal);
  delay(20);
  --grnVal;
  ++redVal;
  if (grnVal == 0) {goto shiftToBlue;}
  goto shiftToRed;
  
  shiftToBlue:
  analogWrite(redPin, redVal);
  analogWrite(bluPin, bluVal);
  delay(20);
  --redVal;
  ++bluVal;
  if (redVal == 0) {goto shiftToGreen;}
  goto shiftToBlue;
  
  
  shiftToGreen:
  analogWrite(bluPin, bluVal);
  analogWrite(grnPin, grnVal);
  delay(15);
  --bluVal;
  ++grnVal;
  if (bluVal == 0) {goto shiftToRedBlu;}
  goto shiftToGreen;
  
  
  shiftToRedBlu:
  analogWrite(redPin, redVal);
  analogWrite(bluPin, bluVal);
  analogWrite(grnPin, grnVal);
  delay(15);
  --grnVal;
  ++redVal;
  ++bluVal;
  if (grnVal == 0) {goto shiftToWhite;}
  goto shiftToRedBlu;
  
  
  shiftToWhite:
  analogWrite(redPin, redVal);
  analogWrite(bluPin, bluVal);
  analogWrite(grnPin, grnVal);
  delay(15);
  ++grnVal;
  if (grnVal == 255) {goto shiftBackToGreen;}
  goto shiftToWhite;
  
  shiftBackToGreen:
  analogWrite(redPin, redVal);
  analogWrite(bluPin, bluVal);
  analogWrite(grnPin, grnVal);
  delay(15);
  --redVal;
  --bluVal;
  if (redVal == 0) {goto shiftToRed;}
  goto shiftBackToGreen;
  
}

I see that your first two transitions are slower than the others (20 millisecond delay instead of 15). The 'goto' statements make the code harder to follow than it could be. Here is what it would look like written as a 'structured' program:

void loop() {

  // Green (0/1/0) to Red (1/0/0)
  do {
    analogWrite(grnPin, grnVal--);
    analogWrite(redPin, redVal++);
    delay(15);
  } while (grnVal != 0);

  // Red (1/0/0) to Blue (0/0/1)
  analogWrite(redPin, redVal--);
  analogWrite(bluPin, bluVal++);
  delay(15);
} while (redVal != 0);


// Blue (0/0/1) to Green (0/1/0)
do {
  analogWrite(bluPin, bluVal--);
  analogWrite(grnPin, grnVal++);
  delay(15);
} while (bluVal != 0);


// Green (0/1/0) to Purple (1/0/1)
do {
  analogWrite(redPin, redVal++);
  analogWrite(bluPin, bluVal++);
  analogWrite(grnPin, grnVal--);
  delay(15);
} while (grnVal != 0);

// Purple (1/0/1) to White (1/1/1)
do {
  // analogWrite(redPin, redVal);
  // analogWrite(bluPin, bluVal);
  analogWrite(grnPin, grnVal++);
  delay(15);
} while (grnVal != 255);

// White (1/1/1) to Green (0/1/0)
do {
  analogWrite(redPin, redVal--);
  analogWrite(bluPin, bluVal--);
  delay(15);
} while (redVal != 0);
}

And of course where you have many blocks of code that look very similar you can likely make those into a function.

void loop() {
  // Green (0/1/0) to Red (1/0/0)
  crossFade(+1, -1, 0);

  // Red (1/0/0) to Blue (0/0/1)
  crossFade(-1, 0, +1);

  // Blue (0/0/1) to Green (0/1/0)
  crossFade(0, +1, -1);

  // Green (0/1/0) to Purple (1/0/1)
  crossFade(+1, -1, +1);

  // Purple (1/0/1) to White (1/1/1)
  crossFade(0, +1, 0);

  // White (1/1/1) to Green (0/1/0)
  crossFade(-1, 0, -1);
}

void crossFade(int redInc, int greenInc, int blueInc) {
  for (int i = 0; i < 255; i++) {
    analogWrite(redPin, redVal);
    analogWrite(grnPin, grnVal);
    analogWrite(bluPin, bluVal);
    redVal += redInc;
    greenVal += greenInc;
    blueVal += blueInc;
    delay(15);
  }
}

Thanks a lot John, it seems that i've a huge gap to fill in, since the last chip i programmed was Motorola's famous MC6800 with its own evaluation module using assembly language only with just 72 instructions about 20 years ago ... I remember some of Assembly routines, some Fortran and some Basic Language structures thats all ..
I'll be asking many questions about languages, syntax and structures i guess ..

The 5050 SMD RGB LED is also available as a Robotshop module with resistors included (RGB LED Breakout - RobotShop). Since the spec sheet states a different forward voltage for each color, the module has used 220, 330 and 470 ohm resistors, instead of the same 100 ohm for all.