Skip to content

Instantly share code, notes, and snippets.

@osteele
Created March 2, 2022 04:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save osteele/f0a11d2f6069acf48982532a67e75991 to your computer and use it in GitHub Desktop.
Save osteele/f0a11d2f6069acf48982532a67e75991 to your computer and use it in GitHub Desktop.
Pixelblaze pattern: a block of lights chases around a circle while the touch sensor is touched
// While the capacitive sensor is touched, a block of light chases around the
// LED strip.
timer = 0 // ms since last position update
period = 100 // ms between position updates
hue = 0
export var center = 0 // the center of the light block
export function beforeRender(delta) {
isTouching = touchRead(T4) > .6
timer += delta
if (timer > period) {
timer -= period
// update the position if the sensor is touched
if (isTouching) {
center = (center + 1) % pixelCount
hue = (hue + .01) % 1
}
}
}
export function render(index) {
// Compute the distance from the current pixel to the center, considering the
// strip as a circle (so that the last pixel is next to the first one).
//
// `abs(index - center)` is the distance from the current pixel (`index`) to
// the center of the block, staying inside the LED strip. `abs(index -
// pixelCount - center)` is the distance going off one end and back onto the
// other. `min(…, …)` is whichever is smaller.
distance = min(abs(index - center), abs(index - pixelCount - center))
hsv(hue, 1, distance < 3) // distance < 3 is 1 when distance < 3, else 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment