Skip to content

Instantly share code, notes, and snippets.

@joshbirk
Created December 28, 2016 03:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joshbirk/392335fa4e2deddd54f853004abfb6ca to your computer and use it in GitHub Desktop.
Save joshbirk/392335fa4e2deddd54f853004abfb6ca to your computer and use it in GitHub Desktop.
Quick Electric Imp impExplorer Tilt Meter
function motion(direction) {
server.log(direction);
}
device.on("direction.sent", motion);
// Accelerometer Library
#require "LIS3DH.class.nut:1.3.0"
#require "WS2812.class.nut:2.0.2"
// Define constants
const sleepTime = 10;
const LIS3DH_ADDR = 0x32;
// Accel settings
const ACCEL_DATARATE = 100;
// High Sensitivity set, so we alway wake on a door event
const ACCEL_THRESHOLD = 0.1;
const ACCEL_DURATION = 1;
// Declare Global Variables
accel <- null;
led <- null
// Define functions
function takeReading(){
accel.getAccel(function(reading) {
if( reading.y < -0.50) {
agent.send("direction.sent", "RIGHT");
flashLed([0,0,128]);
}
if( reading.x < -0.50) {
agent.send("direction.sent", "BACK");
flashLed([0,128,0]);
}
if( reading.y > 0.50) {
agent.send("direction.sent", "LEFT");
flashLed([128,0,0]);
}
if( reading.x > 0.50) {
agent.send("direction.sent", "FORWARD");
flashLed([0,128,128]);
}
takeReading();
});
}
function flashLed(color) {
led.set(0, color).draw();
imp.sleep(0.5);
led.set(0, [0,0,0]).draw();
}
// Start of program
// Configure I2C bus for sensors
local i2c = hardware.i2c89;
i2c.configure(CLOCK_SPEED_400_KHZ);
accel = LIS3DH(i2c, LIS3DH_ADDR);
accel.init();
// set up to take readings
accel.setLowPower(true);
accel.setDataRate(ACCEL_DATARATE);
accel.enable(true);
// Configure SPI bus and powergate pin for RGB LED
local spi = hardware.spi257;
spi.configure(MSB_FIRST, 7500);
hardware.pin1.configure(DIGITAL_OUT, 1);
led <- WS2812(spi, 1);
// Take a reading
takeReading();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment