3

I'm continuously storing outputs in a CSV file. I'm able to read this data and trigger a change in lighting with the code below. The transition in lighting is sudden but, ideally, I'd like to attempt a smooth transition from one color to another in a non-linear way using only a RPi3 and neopixels/ws2812b.

df = pd.read_csv("output.csv")
second_last = df['modes'].iloc[-2]
last = df['modes'].iloc[-1]

if second_last == last and last == "one" :
    pixels.fill((255,0,0))
if second_last == last and last == "two" :
    pixels.fill((0,255,0))
if second_last == last and last == "three" :
    pixels.fill((0,0,255))

I've found that a smoothstep function may be used for this but am unsure how to make practical use of it.

How can RGB values be made to follow a curve where the initial color is 0 and the color it transitions to is 1 if, for example, the following function is used? Is this a viable approach?

y = 0.5 * (1 + sin((x*pi)-(pi/2)))

1 Answers1

2

Disclaimer: I'm an image/video specialist and I haven't used Raspberry Pi for lighting. I guess that right now, your lighting goes from all red to all green to all blue, changing very suddenly. I describe a way to make that change more gradual, but it's probably inefficient and there might be an easier way that is specific to the platform.

You need to define a time (say t) to transition over. Also, you should understand that the colour is given by pixels.fill((255,0,0)). Those three values (255,0,0) represent (R,G,B)*. If you want intermediate colours, you need to have a combination of these. So, for example, if you want to transition from Red to Blue, you need a sequence of those with some intermediate values. Linearly, this could go something like:

max=255
step = 255//t
for i in range(0,t):
  red = 255 - (t*step)
  green = 0
  blue = 255 - red
  RGBtriplet = (red, green, blue)

It's then a matter of feeding RGBtriplet to pixels.fill(). Alternatively, you could use your function:

   blue = 255 * (0.5 * (1 + sin((t*pi)-(pi/2))))
   green = 0
   red = 255 - blue

That should give you a nonlinear colour transition where blue goes from 0 to 255 and red goes from 255 to 0 (red to blue).

*Or possibly (B,G,R).

Pam
  • 1,146
  • 1
  • 14
  • 18