Skip to content

Instantly share code, notes, and snippets.

@joshbirk
Last active December 28, 2016 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshbirk/21a125da74ab9f5d7ed435870a043a54 to your computer and use it in GitHub Desktop.
Save joshbirk/21a125da74ab9f5d7ed435870a043a54 to your computer and use it in GitHub Desktop.
Tilt meter with Electric Imp impExplorer to control LIFX lights
current_bright <- 0.5;
current_scene <- 2;
const low_scene = 0;
const high_scene = 3;
const lowest_bright = 0.0;
const highest_bright = 1.0;
const bearer = "YOURBEARERIDS";
related_scenes <- [IFNEEDED+YOURSCENEIDSHERE];
function motion(data) {
if(data.dir == "TAP") {
server.log(data.count);
//do something cool here (I did a custom animation)
}
if(data.dir == "FORWARD") {
local request = http.put("https://api.lifx.com/v1/lights/group_id:YOURGROUPID/state",
{
"Authorization":"Bearer "+bearer,
"content-type": "application/json"
},http.jsonencode({"power": "on"})
);
local response = request.sendsync();
}
if(data.dir == "BACK") {
local request = http.put("https://api.lifx.com/v1/lights/group_id:YOURGROUPID/state",
{
"Authorization":"Bearer "+bearer,
"content-type": "application/json"
},http.jsonencode({"power": "off"})
);
local response = request.sendsync();
}
if(data.dir == "LEFT") {
current_bright -= 0.1;
if(current_bright < lowest_bright) { current_bright = lowest_bright; }
local request = http.put("https://api.lifx.com/v1/lights/group_id:YOURGROUPID/state",
{
"Authorization":"Bearer "+bearer,
"content-type": "application/json"
},http.jsonencode({"brightness": current_bright})
);
local response = request.sendsync();
}
if(data.dir == "RIGHT") {
current_bright += 0.1;
if(current_bright > highest_bright) { current_bright = highest_bright; }
local request = http.put("https://api.lifx.com/v1/lights/group_id:YOURGROUPID/state",
{
"Authorization":"Bearer "+bearer,
"content-type": "application/json"
},http.jsonencode({"brightness": current_bright})
);
local response = request.sendsync();
}
}
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;
const ACCEL_THRESHOLD = 0.1;
const ACCEL_DURATION = 1;
const LOW_THRESH = -0.85;
const HIGH_THRESH = 0.85;
//tap_count
tap_count <- 0;
// Declare Global Variables
accel <- null;
led <- null
// Define functions
function takeReading(){
accel.getAccel(function(reading) {
if( reading.z > 1.25) {
agent.send("direction.sent", {"dir": "TAP", "count": tap_count});
flashLed([25,25,25]);
tap_count++;
}
else if( reading.y < LOW_THRESH) {
agent.send("direction.sent", {"dir": "RIGHT", "count": tap_count});
flashLed([0,128,128]);
tap_count = 0;
}
else if( reading.x < LOW_THRESH) {
agent.send("direction.sent", {"dir": "BACK", "count": tap_count});
flashLed([128,0,0]);
tap_count = 0;
}
else if( reading.y > HIGH_THRESH) {
agent.send("direction.sent", {"dir": "LEFT", "count": tap_count});
flashLed([128,128,0]);
tap_count = 0;
}
else if( reading.x > HIGH_THRESH) {
agent.send("direction.sent", {"dir": "FORWARD", "count": tap_count});
flashLed([0,128,0]);
tap_count = 0;
}
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