Hey everybody, I'm working on a new project and I'm using one of Adafruit's

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?

Check the Colorutils.h file. That mentions how to create gradients and the travel path (shortest_hues): https://github.com/FastLED/FastLED/blob/master/colorutils.h
https://github.com/FastLED/FastLED/blob/master/colorutils.h

Thanks fill_gradient makes nice, read awesome gradients. Played for over an hour with these… but that’s not wanted here. Just want to slowly (over time) color one led from the currentcolor to the angle of the sensor.

Got it working, just calculated the two angles and then take the shortest route :wink:

#include “Adafruit_CircuitPlayground.h”
#include “FastLED.h”

#define NUM_LEDS 10
#define DATA_PIN 17
#define BRIGHTNESS 64
#define FRAMES_PER_SECOND 30

CRGB leds[NUM_LEDS];

float x, y, z, nx, ny, angle;

uint8_t anglebetween;
uint8_t hue = 64;
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(){

angle = map(getAngle(), 0, 359, 0, 255);
anglebetween = (hue - angle);

if(abs(x) > 1 || abs(y) > 1){

if(anglebetween > 0 && anglebetween < 128 ){
  hue -= 1;
  //Serial.println("left");
} else {
hue += 1;
  //Serial.println("right");
}

// change all leds to color
for( int i = 0; i < NUM_LEDS; i++) {
  leds[i] = CHSV(hue, sat, val);
}

//current hue led
//leds[map(hue, 0, 255, 0, 9)] = CHSV(0, 0, 255);

//current angle led
//leds[map(angle, 0, 255, 0, 9)] = CHSV(angle, 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;
}