Coding experts I seek your guidance, sadly I am horrible at coding
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.
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.
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.
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?
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.