Go Down

Topic: Dumb little project - Esplora Bubble Level (Read 3632 times) previous topic - next topic

rmd6502

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.

Code: [Select]
#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));
  }
}

rmd6502

Oops, made a variable and forgot to use it!
Code: [Select]
#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));
  }
}

TheKitty

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

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy