Hey everybody,
I’m working on a new project and I’m using one of Adafruit’s circuit playground dev boards. Circuit Playground Classic : ID 3000 : $19.95 : Adafruit Industries, Unique & fun DIY electronics and kits It’s cheap, got 10 neopixels and a bunch of sensors. Really nice to play with and work great with Fastled.
The plan is to use the build in accelerometer to change to colours of the led according to the xy angle. This is fairly easy. See this code:
#include “Adafruit_CircuitPlayground.h”
#include “FastLED.h”
#define NUM_LEDS 10
#define DATA_PIN 17
#define BRIGHTNESS 64
#define FRAMES_PER_SECOND 15
CRGB leds[NUM_LEDS];
float x, y, z, nx, ny, angle;
uint8_t hue = 70;
uint8_t sat = 255;
uint8_t val = 255;
void setup() {
CircuitPlayground.begin();
CircuitPlayground.clearPixels();
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
Serial.begin(9600);
}
void loop(){
// create color from angle
hue = map(getAngle(), 0, 359, 0, 255);
// change all leds to color
for( int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(hue, 255, 255);
}
FastLED.show();
FastLED.delay(1000/FRAMES_PER_SECOND);
}
// Get values from accelerometer and calculate the angle in 2D
int getAngle(){
x = CircuitPlayground.motionX();
y = CircuitPlayground.motionY();
z = CircuitPlayground.motionZ(); // We ignore the z axis
nx = x / 10.0;
ny = y / 10.0;
angle = atan((ny/nx)) * 180 / 3.14; // Figure out the angle of the accelerometer
if(angle > 0.0) { // Adjust based on arctangent function (in degrees)
if(nx < 0.0)
angle += 180;
}
else {
if(ny > 0.0)
angle += 180;
else
angle += 360;
}
if(angle == 360.0) // a 360 degree angle is the same as a zero degree angle
angle = 0;
return angle;
}
The next step would be to fade the color slowly according the angle of the board. This is trickier. Is there a function thats fades a pixel to a given color? Example the current hue = 100 but we want to go to 255 using the shortest route.
Does anybody has done this before or has a function to share?