Created
May 2, 2018 13:27
-
-
Save bmidgley/77f898c3219eaf75122283de75d2d3cf to your computer and use it in GitHub Desktop.
Read the accelerometer and use addressible lights on an attiny85
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <TinyWireM.h> | |
#include <FastLED.h> | |
const int MPU_addr=0x68; // I2C address of the MPU-6050 | |
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ; | |
boolean was_turned = false; | |
#define NUM_LEDS 15 | |
#define LED_PIN 1 | |
#define BRIGHTNESS 64 | |
#define LED_TYPE WS2812 | |
#define COLOR_ORDER GRB | |
CRGB leds[NUM_LEDS]; | |
#define UPDATES_PER_SECOND 100 | |
void mpu_init() { | |
TinyWireM.beginTransmission(MPU_addr); | |
TinyWireM.send(0x6B); // PWR_MGMT_1 register | |
TinyWireM.send(0); // set to zero (wakes up the MPU-6050) | |
TinyWireM.endTransmission(); | |
} | |
void mpu_read() { | |
TinyWireM.beginTransmission(MPU_addr); | |
TinyWireM.send(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) | |
TinyWireM.endTransmission(); | |
TinyWireM.requestFrom(MPU_addr, 14); // request a total of 14 registers | |
AcX=TinyWireM.receive()<<8|TinyWireM.receive(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L) | |
AcY=TinyWireM.receive()<<8|TinyWireM.receive(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L) | |
AcZ=TinyWireM.receive()<<8|TinyWireM.receive(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L) | |
Tmp=TinyWireM.receive()<<8|TinyWireM.receive(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L) | |
GyX=TinyWireM.receive()<<8|TinyWireM.receive(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L) | |
GyY=TinyWireM.receive()<<8|TinyWireM.receive(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L) | |
GyZ=TinyWireM.receive()<<8|TinyWireM.receive(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L) | |
} | |
CRGBPalette16 currentPalette; | |
TBlendType currentBlending; | |
extern CRGBPalette16 myRedWhiteBluePalette; | |
extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM; | |
void setup() { | |
delay(3000); | |
TinyWireM.begin(); | |
pinMode(1, OUTPUT); //LED on Model A | |
mpu_init(); | |
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip ); | |
FastLED.setBrightness( BRIGHTNESS ); | |
currentPalette = RainbowColors_p; | |
currentBlending = LINEARBLEND; | |
} | |
void loop() { | |
static uint8_t startIndex = 0; | |
mpu_read(); | |
boolean is_turned = AcX > 0; | |
if(is_turned != was_turned) { | |
was_turned = is_turned; | |
if(is_turned) { | |
digitalWrite(1, HIGH); | |
} else { | |
digitalWrite(1, LOW); | |
} | |
} | |
ChangePalettePeriodically(); | |
startIndex = startIndex + 1; /* motion speed */ | |
FillLEDsFromPaletteColors( startIndex); | |
FastLED.show(); | |
FastLED.delay(1000 / UPDATES_PER_SECOND); | |
} | |
void FillLEDsFromPaletteColors( uint8_t colorIndex) | |
{ | |
uint8_t brightness = 255; | |
for( int i = 0; i < NUM_LEDS; i++) { | |
leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending); | |
colorIndex += 3; | |
} | |
} | |
void ChangePalettePeriodically() | |
{ | |
uint8_t secondHand = (millis() / 1000) % 60; | |
static uint8_t lastSecond = 99; | |
if( lastSecond != secondHand) { | |
lastSecond = secondHand; | |
if( secondHand == 0) { currentPalette = RainbowColors_p; currentBlending = LINEARBLEND; } | |
if( secondHand == 10) { currentPalette = RainbowStripeColors_p; currentBlending = NOBLEND; } | |
if( secondHand == 15) { currentPalette = RainbowStripeColors_p; currentBlending = LINEARBLEND; } | |
// if( secondHand == 20) { SetupPurpleAndGreenPalette(); currentBlending = LINEARBLEND; } | |
// if( secondHand == 25) { SetupTotallyRandomPalette(); currentBlending = LINEARBLEND; } | |
// if( secondHand == 30) { SetupBlackAndWhiteStripedPalette(); currentBlending = NOBLEND; } | |
// if( secondHand == 35) { SetupBlackAndWhiteStripedPalette(); currentBlending = LINEARBLEND; } | |
if( secondHand == 40) { currentPalette = CloudColors_p; currentBlending = LINEARBLEND; } | |
if( secondHand == 45) { currentPalette = PartyColors_p; currentBlending = LINEARBLEND; } | |
if( secondHand == 50) { currentPalette = myRedWhiteBluePalette_p; currentBlending = NOBLEND; } | |
if( secondHand == 55) { currentPalette = myRedWhiteBluePalette_p; currentBlending = LINEARBLEND; } | |
} | |
} | |
// | |
//void SetupTotallyRandomPalette() | |
//{ | |
// for( int i = 0; i < 16; i++) { | |
// currentPalette[i] = CHSV( random8(), 255, random8()); | |
// } | |
//} | |
// | |
//void SetupBlackAndWhiteStripedPalette() | |
//{ | |
// // 'black out' all 16 palette entries... | |
// fill_solid( currentPalette, 16, CRGB::Black); | |
// // and set every fourth one to white. | |
// currentPalette[0] = CRGB::White; | |
// currentPalette[4] = CRGB::White; | |
// currentPalette[8] = CRGB::White; | |
// currentPalette[12] = CRGB::White; | |
// | |
//} | |
// | |
//// This function sets up a palette of purple and green stripes. | |
//void SetupPurpleAndGreenPalette() | |
//{ | |
// CRGB purple = CHSV( HUE_PURPLE, 255, 255); | |
// CRGB green = CHSV( HUE_GREEN, 255, 255); | |
// CRGB black = CRGB::Black; | |
// | |
// currentPalette = CRGBPalette16( | |
// green, green, black, black, | |
// purple, purple, black, black, | |
// green, green, black, black, | |
// purple, purple, black, black ); | |
//} | |
// This example shows how to set up a static color palette | |
// which is stored in PROGMEM (flash), which is almost always more | |
// plentiful than RAM. A static PROGMEM palette like this | |
// takes up 64 bytes of flash. | |
const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM = | |
{ | |
CRGB::Red, | |
CRGB::Gray, // 'white' is too bright compared to red and blue | |
CRGB::Blue, | |
CRGB::Black, | |
CRGB::Red, | |
CRGB::Gray, | |
CRGB::Blue, | |
CRGB::Black, | |
CRGB::Red, | |
CRGB::Red, | |
CRGB::Gray, | |
CRGB::Gray, | |
CRGB::Blue, | |
CRGB::Blue, | |
CRGB::Black, | |
CRGB::Black | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment