Pages: [1]   Go Down
Author Topic: Dumb little project - Esplora Bubble Level  (Read 2321 times)
0 Members and 1 Guest are viewing this topic.
New York, NY
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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));
  }
}
Logged

New York, NY
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

US
Offline Offline
Full Member
***
Karma: 4
Posts: 182
Electronics are the new Legos
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: