Dumb little project - Esplora Bubble Level

Something I threw together to demo the accelerometer and LED. Hold down switch_1 to calibrate, LEDs off (flickering a bit) indicate you're back where you calibrated.

Public Domain, do what you will with it.

#include <Esplora.h>

const uint16_t SAMPLES=64;

int calx=0, caly=0, calz=0;
long sums[3] = {0};
int readings[64][3] = {0};
uint8_t index = 0;

void setup() {
  Serial.begin(115200);
}

void loop() {
  int rx, ry, rz;
  rx=Esplora.readAccelerometer(X_AXIS);
  ry=Esplora.readAccelerometer(Y_AXIS);
  rz=Esplora.readAccelerometer(Z_AXIS);
  
  if (Esplora.readButton(SWITCH_DOWN) == LOW) {
    calx=rx, caly=ry, calz=rz;
  } else {
    rx -= calx, ry -= caly, rz -= calz;
    for (uint8_t i=0; i < 3; ++i) {
      sums[i] -= readings[index][i];
    }
    sums[0] += rx, sums[1] += ry, sums[2] += rz;
    readings[index][0] = rx;
    readings[index][1] = ry;
    readings[index][2] = rz;
    index = (index + 1) % 64;
    rx = sums[0]/64, ry = sums[1]/64, rz = sums[2]/64;
    
    Esplora.writeRGB(abs(rx), abs(ry), abs(rz));
  }
}

Oops, made a variable and forgot to use it!

#include <Esplora.h>

const uint16_t SAMPLES=64;

int calx=0, caly=0, calz=0;
long sums[3] = {0};
int readings[SAMPLES][3] = {0};
uint8_t index = 0;

void setup() {
  Serial.begin(115200);
}

void loop() {
  int rx, ry, rz;
  rx=Esplora.readAccelerometer(X_AXIS);
  ry=Esplora.readAccelerometer(Y_AXIS);
  rz=Esplora.readAccelerometer(Z_AXIS);
  
  if (Esplora.readButton(SWITCH_DOWN) == LOW) {
    calx=rx, caly=ry, calz=rz;
  } else {
    rx -= calx, ry -= caly, rz -= calz;
    for (uint8_t i=0; i < 3; ++i) {
      sums[i] -= readings[index][i];
    }
    sums[0] += rx, sums[1] += ry, sums[2] += rz;
    readings[index][0] = rx;
    readings[index][1] = ry;
    readings[index][2] = rz;
    index = (index + 1) % SAMPLES;
    rx = sums[0]/SAMPLES, ry = sums[1]/SAMPLES, rz = sums[2]/SAMPLES;
    
    Esplora.writeRGB(abs(rx), abs(ry), abs(rz));
  }
}

Awesome!! This could also be ported to use the LCD display to great effect. Good work!