Created
May 25, 2015 16:22
-
-
Save jdeblese/041a695d92397144904e to your computer and use it in GitHub Desktop.
Tinymote code to run a color wheel on a neopixel ring based on accelerometer readings
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 <Adafruit_NeoPixel.h> | |
#include <TinyWireM.h> | |
#include <USI_TWI_Master.h> | |
#define LEDPIN 3 | |
#define LEDCOUNT 12 | |
#define DEV_ADDR 0x4C // MMA7660FC 7-bit address | |
/* ATtiny85 | |
* ATtiny Pin 1 = (RESET) N/U ATtiny Pin 8 = VCC (2.7-5.5V) | |
* ATtiny Pin 2 = (D3) LED ATtiny Pin 7 = SCL | |
* ATtiny Pin 3 = (D4) /INT ATtiny Pin 6 | |
* ATtiny Pin 4 = GND ATtiny Pin 5 = SDA | |
*/ | |
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDCOUNT, LEDPIN, NEO_GRB + NEO_KHZ800); | |
byte counter; | |
void setup() { | |
// Set accelerometer to active mode | |
// Set accelerometer sampling rate to 32/s | |
// Register address is auto-incrementing | |
TinyWireM.begin(); | |
TinyWireM.beginTransmission(DEV_ADDR); | |
TinyWireM.send(0x07); | |
TinyWireM.send(0x01); // Active | |
TinyWireM.send(0x02); // Sampling rate | |
TinyWireM.endTransmission(); | |
strip.begin(); | |
strip.show(); // Initialize all pixels to 'off' | |
counter = 0; | |
} | |
// Convert 6-bit to signed | |
#define to_signed(x) (x > 31 ? x - 64 : x) | |
void loop() { | |
int16_t x, y, z; | |
uint8_t i; | |
// FIXME the following only works if a repeated start is used, otherwise the register | |
// address is reset to zero. That's what we want anyway, however... | |
// Set to register 0 | |
TinyWireM.beginTransmission(DEV_ADDR); | |
TinyWireM.send(0x00); | |
TinyWireM.endTransmission(); | |
// Read X, Y and Z values | |
TinyWireM.requestFrom(DEV_ADDR,3); | |
x = TinyWireM.receive(); | |
y = TinyWireM.receive(); | |
z = TinyWireM.receive(); | |
x = to_signed(x); | |
y = to_signed(y); | |
z = to_signed(z); | |
// Set pixel to next color | |
//strip.setPixelColor(0, x & 255, y & 255, z & 255); | |
for (i = 0; i < LEDCOUNT; i++) | |
strip.setPixelColor(i, Wheel(counter+i)); | |
strip.show(); | |
// Increment counter | |
counter++; | |
// Acceleration vector magnitude | |
x = x*x + y*y + z*z; | |
if (x < 300 || x > 600) | |
counter += 64; | |
delay(50); | |
} | |
// Following code taken from Adafruit Neopixel Library example, GPL Licensed | |
// Input a value 0 to 255 to get a color value. | |
// The colours are a transition r - g - b - back to r. | |
uint32_t Wheel(byte WheelPos) { | |
WheelPos = 255 - WheelPos; | |
if(WheelPos < 85) { | |
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); | |
} else if(WheelPos < 170) { | |
WheelPos -= 85; | |
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); | |
} else { | |
WheelPos -= 170; | |
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment