Psychedelic colors using RGB LEDs

Zápisník experimentátora

Hierarchy: ShiftPWM

In this article you will find a description of an algorithm that creates psychedelic color transitions using RGB LEDs. It looks harder to see how it looks, so watch the video at the end of the article, where these colors change fluently. This was the first experiment in the field and the result is a little different than I expected. I use random color settings and it has often happened that the choice of colors at both ends of the eight diodes was so close that no psychedelic transition took place. But let's start from the beginning.

Used boards

To create the effect, we will use two boards from the previous article. In the ardticle is described how to make them and how to connect them. In addition, I also used:

  • Arduino Pro Mini {linkArduino}
  • Breadboard {linkBreadboard}

Programming

The algorithm is designed to alternately calculate a random color at one end or the other and smoothly animate the transition from the previous color. Thus, every two seconds, one side is smoothly transformed into a new color and the entire algorithm is repeated. In the article I will not describe the basic ShiftPWM library settings, which you will find in the previous article.

Because we work with RGB values, it is advisable to define the structure for those purposes. We will need three pairs of such structures to implement the entire algorithm. The variable v contains the currently calculated value at both ends. The variable val contains the latest pair of RGB colors. It is a goal that the color gradually approaches. The oldval variable contains the previous pair and the color is moving away from it. The side variable takes two values and tells us on which side to calculate the new RGB value.

struct rgb {
  int r;
  int g;
  int b;
};

rgb v[2];
rgb val[2];
rgb oldval[2];
int side=0;

In the loop function, we perform the following calculations:

  • We calculate the RGB value on one side or the other.
  • In the cycle from 0-100, we calculate the new extreme values first and then recalculate them to the transition between them. Since we have 8 diodes, the second cycle is from 0-8. We wait 20 ms, giving us 2 seconds per pass for 100 cycles.
  • Then we write the last calculated value into the oldval.
  • And switch the page to make the new calculation on this side.
void loop()
{  
  if(side==0) {
    val[0].r=random(255);
    val[0].g=random(255);
    val[0].b=random(255);
  } else {
    val[1].r=random(255);
    val[1].g=random(255);
    val[1].b=random(255);
  }

  for(int j=0;j<100;j++) {
    v[0].r=map(j,0,99,oldval[0].r,val[0].r);
    v[0].g=map(j,0,99,oldval[0].g,val[0].g);
    v[0].b=map(j,0,99,oldval[0].b,val[0].b);

    v[1].r=map(j,0,99,oldval[1].r,val[1].r);
    v[1].g=map(j,0,99,oldval[1].g,val[1].g);
    v[1].b=map(j,0,99,oldval[1].b,val[1].b);
    
    for(int i=0;i<8;i++)
      SetRGB(i,
             map(i,0,7,v[0].r,v[1].r),
             map(i,0,7,v[0].g,v[1].g),
             map(i,0,7,v[0].b,v[1].b));
    delay(20);
  }

  oldval[0]=val[0];
  oldval[1]=val[1];

  if(side==0) side=1;
  else side=0;
}

The weakness of the algorithm is that random values often do not produce sufficiently contrasting colors. If the RGB values are about the same on both sides, the human eye no longer sees any animation, and the power of the effect is lost. If you would like to improve the algorithm, I would recommend having an array of manually defined RGB values and selecting only one of them through the index. This will ensure that there will still be sufficient contrast between the two edges.

Source code

The source code is located on GitHub.

Video

Additional video is located on YouTube.



Video


01.07.2019


Menu