Accelerometer for Switching Patterns

Hello:

I am a novice. How would I begin to structure a PB project that uses the accelerometer to switch between 4 patterns in an array?

Thanks,

Hey! Good to see you again.

Here’s the steps I’d go through to do this:

  1. Review how to have multiple sub-patterns in a single pattern. The simplest examples I can remember are:
    a. Shimmer Crossfade 2D (currently on the middle of Page 7)
    b. “Example: modes and waveforms” pattern (ships with Pixelblaze, also here)
  2. Review how to get values from the accelerometer (scroll to the Accelerometer heading on the Sensor Expansion Board page)
  3. Have beforeRender() check the current accelerometer values in accelerometer[0..2] and select which mode or sub-pattern you want to use.
1 Like

Thanks. Good to see you again as well.

My plan is to:
1: Scale the accelerometer values so that they are close to 1
2: Take the absolute value
3: Change to the next pattern in the array if the accelerator value is > 1
4: Reset the counter after reaching the last pattern in the array

Am I missing anything?

That sounds good. Could work quite well.

Note the example code on that Sensor Board page, “Shake to switch patterns” - it might be very close to what you’re looking for. In beforeRender():

  debounce = clamp(debounce + delta, 0, 2000) // Prevent overflow
  
  // Cycle mode if sensor board is shaken, no more than 1x / sec
  if (debounce > 1000 && totalAcceleration > 0.03) {
    mode = (mode + 1) % 3
    debounce = 0
  }

It uses the remainder operator (%) to roll back to the first pattern after making its way through them all. That’s an alternative to counting mode changes and resetting the counter.

Second is the debouncing. If you don’t debounce, IE, wait a bit after each detected change in acceleration before allowing it to change again, it will cycle patterns so quickly that it will feel random to the user. This accumulates delta milliseconds and doesn’t allow another mode change unless at least a second has passed since the last mode change.

I found the Shake to Switch Modes code on the [Sensor Expansion Board] page). It didn’t occur to me to check there. Cheers!

// Shake to switch modes

export var accelerometer  // Enable the accelerometer
mode = debounce = 0

export function beforeRender(delta) {
  // 3D vector sum of x, y, and z acceleration
  totalAcceleration = sqrt(
    accelerometer[0] * accelerometer[0] +
    accelerometer[1] * accelerometer[1] +
    accelerometer[2] * accelerometer[2]
  )
  
  debounce = clamp(debounce + delta, 0, 2000) // Prevent overflow
  
  // Cycle mode if sensor board is shaken, no more than 1x / sec
  if (debounce > 1000 && totalAcceleration > 0.03) {
    mode = (mode + 1) % 3
    debounce = 0
  }
}

h = s = v = 1
modes = array(3)
modes[0] = (index) => { h = 1 - index / pixelCount / 4; v = 1 }
modes[1] = (index) => { h = 0.5; v = index % 2 }
modes[2] = (index) => { h = wave((index+wave(time(0.04))*5)/pixelCount)*6; v = 0.1+random(wave(time(0.03))) }

export function render(index) {
  modes[mode](index)
  hsv(h, s, v)
}

@ Jeff. Thanks for the debounce explanation. I was struggling to understand its function.

1 Like