Sequential Turn light pattern

Hi. I would like to make a sequential turn light on the car with PixelBlaze. Do you have or make the pattern for sequential turn slight?

Try modifying the KITT pattern, removing the reverse direction code. Here’s the original from https://electromage.com/patterns/

leader = 0
direction = 1
pixels = array(pixelCount)

speed = pixelCount / 800
fade = .0007
export function beforeRender(delta) {
  leader += direction * delta * speed
  if (leader >= pixelCount) {
    direction = -direction
    leader = pixelCount -1
  }
  
  if (leader < 0) {
    direction = -direction
    leader = 0
  }
  pixels[floor(leader)] = 1
  for (i = 0; i < pixelCount; i++) {
    pixels[i] -= delta * fade
    pixels[i] = max(0, pixels[i])
  }
}

export function render(index) {
  v = pixels[index]
  v = v*v*v
  hsv(0, 1, v)
}

Thank you for your advice. But, still do not know how to do.
delete line 2 or modify line 2 number, can not get sequential turn light pattern.
And do not know how to change the color blue to amber (turn light yellow). Sorry I am a beginner.

This part of the code

if (leader >= pixelCount) {
    direction = -direction
    leader = pixelCount -1
  }
  
  if (leader < 0) {
    direction = -direction
    leader = 0
  }

is what causes the “leader” pixel to bounce back and forth. It checks that it hit and end, and changes direction. Instead of changing direction, you want it to stop moving. The direction variable alternates between 1 and -1, but if it was 0 the leader would stop moving. You also probably don’t want fade, but instead want some way to turn everything off.

First, when the leader hits the end it should stop moving by setting direction to 0. Replace both checks for leader with this:

  if (leader >= pixelCount) {
    direction = 0;
  }

Next, remove this fade, and replace it with a “dark” function.
Remove this:

  for (i = 0; i < pixelCount; i++) {
    pixels[i] -= delta * fade
    pixels[i] = max(0, pixels[i])
  }

and instead make a dark function that turns everything off:

function dark() {
  for (i = 0; i < pixelCount; i++) {
    pixels[i] = 0
  }
  direction = 0
}

With only these changes, the leader index is still set when direction is 0 but we don’t want that. Change this line:

pixels[floor(leader)] = 1

With a check:

if (direction != 0)
  pixels[floor(leader)] = 1

The next thing I think you’ll want is a way to start this, so that your pattern can start with direction = 0 and only starts the animation based on some input (like a GPIO):

function start() {
  if (direction != 0)
   return
  direction = 1
  leader = 0
}

So all together, something like this:

//modified version of KITT to make a turn light pattern

var leader = 0
var direction = 0
var pixels = array(pixelCount)

export function beforeRender(delta) {
  leader += direction * .5
  if (leader >= pixelCount) {
    direction = 0
  }
  
  if (direction != 0)  
    pixels[floor(leader)] = 1
}

function start() {
  if (direction != 0)
   return
  direction = 1
  leader = 0
}

function dark() {
  for (i = 0; i < pixelCount; i++) {
    pixels[i] = 0
  }
  direction = 0
}

export function render(index) {
  v = pixels[index]
  v = v*v*v
  hsv(0, 1, v)
}

Note that the above won’t show anything without additional code. You need to call start() for it to get going. If you just want to see what it looks like, you can call start in the main body, or in beforeRender.

A note on color, if KITT pattern is showing blue, then the color order setting is not right.
Run this pattern:

export function render(index) {
  hsv( index < pixelCount/2 ? 0 : 1/3, 1, .2)
}

which will make the first half red, and the second half green. Adjust color order setting until it matches.

Imagine the first argument to hsv() is from a wheel like this:

colorwheel

So an amber color will sit somewhere between 0 (red) and .333 (green). It doesn’t take much, maybe something around 0.03 for an orange, and 0.1 for something that is closer to yellow.

In addition to hue, there is also saturation that can be used to create mixes with some white in them. When all 3 parameters work together, you get something like this:


(HSV_color_solid_cylinder.png: SharkDderivative
CC BY-SA 3.0 https://commons.wikimedia.org/w/index.php?curid=9801673)

1 Like

Following. I am thinking in do the same.

Love this! Might try it later on my car.