Red Blink on braking

Coding experts I seek your guidance, sadly I am horrible at coding :frowning:

I have the pixelblaze as some of you have seen on an electric scooter. I want to say I am the first and only one in the world! yay for us!

Here is what I would love to do if possible. What line of code can I insert into ANY pattern, that when I reduce my speed it will make the LEDs flash red? I have the sensor expander with the g sensor etc.

thanks

Hey Wilder -

If you’re relying on this for safety, please see the suggested improvements at the end.

This is the simplest example I could think up. Here’s the output:

And the code:

export var accelerometer

export function beforeRender(delta) {
  blink = square(time(.003), 0.5)
  // You'll need to tune this and use the right ax[ie]s [0..2]
  braking = accelerometer[1] < -0.01 
}

export function render(index) {
  if (braking) hsv(0, 1, blink)    // Braking
  else hsv(index/pixelCount, 1, 1) // Whatever your default pattern is
}

For anyone who doesn’t have the sensor expansion board, you can simulate the accelerometer using a slider control in the editor like this:

// Assume negative values indicate braking
export var acceleration = 0.1

export function sliderBraking(slider) {
  acceleration = slider - 0.5 // -0.5 to 0.5
}

export function beforeRender(delta) {
  blink = square(time(.003), 0.5)
}

export function render(index) {
  if (acceleration < 0) hsv(0, 1, blink)  // braking
  else hsv(index/pixelCount, 1, 1)  // Whatever your default pattern is
}

Here are some enhancements you should consider if this is a signaling mechanism used in real-world scootering among driving things that can kill you. Safety third and stuff, but:

  • Introduce hysteresis - a threshold over which there needs to be braking or the release of braking before the mode switches.
  • Consider making a minimum duration that braking blink mode will turn on for, so that very hard sudden braking that’s released quickly still allows reaction time from other drivers
  • Use the integral of time * acceleration values to trigger the mode change with the hysteresis. I know that might sound complicated because “integral” is calculus, but it’s just the sum of a rolling array of the measured values, each one multiplied by delta. That way, sustained moderate braking AND sudden sharp braking both trigger the minimum duration brake signal.
  • If you customize the blinking to be any slower, consider resetting the blink timer on each entry into the braking sate so you always start with a consistent visual signal.

Sorry for any preachiness on safety, it’s just that most of my patterns don’t have consequences.

2 Likes

The first code you posted, where do I copy and paste it to? To the beginning of the code or to the end?

Unfortunately it’s not a simple as being able to just copy-paste it in somewhere.

The first part, ‘export var accelerometer’ needs to be outside of functions and up top.

The rest of the code lives inside the two functions that almost all Pixelblaze patterns have - find those in your existing patterns and copy-paste the contents in. You’ll need to think through the one in render() so you call either hsv() for the blinking mode OR whatever your default patterns are.

Aysh! this is so dificult :cry:

I wish I understood!

I also noticed in your example the pixelblaze actually rotates, since my pixelblaze is mounted on the stem of the scooter, moving the pixelblaze in that same fashion is NOT an option.

How about a 12v or a 5v line input? is that possible?

For others who might come across this thread in the future:

We got it all sorted via Skype. We used the live variable watcher to identify which axis in accelerometer to use, given the existing mounting.

2 Likes

Jeff! you are a life saver! thank thank thank you so much for the hours spent helping me grasp the concept a lot better! I now feel so much more confident, thanks to you.

1 Like