Good evening, I have a quick question about my code,

Good evening, I have a quick question about my code, i’m really new to this still and a lot of it is way beyond me, but how would i go about anti-aliasing this code so the LEDs arent just on OR off. The code i have works well, it would just look alot nicer if the LEDs faded in and out rather than being all or nothing. I’ve referred to this example: https://plus.google.com/112916219338292742137/posts/2VYNQgD38Pw

but I dont even know where to start when it comes to integrating this into my code so it does something similar…

Here’s what i have:
#include<Wire.h>
const int MPU=0x68; // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp;

#include <FastLED.h> // FastLED library

#define NUM_LEDS 10 // Number of LED’s
#define COLOR_ORDER GRB // Change the order as necessary
#define LED_TYPE WS2811 // What kind of strip are you using?
#define BRIGHTNESS 255 // How bright do we want to go

CRGB Xleds[NUM_LEDS]; // Initialize our array
CRGB Yleds[NUM_LEDS];

void setup(){
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
Serial.begin(9600);

LEDS.addLeds<LED_TYPE, 13, COLOR_ORDER>(Xleds, NUM_LEDS).setCorrection(TypicalLEDStrip);
LEDS.addLeds<LED_TYPE, 12, COLOR_ORDER>(Yleds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
}
void loop(){
Wire.beginTransmission(MPU);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU,8,true); // request a total of 14 registers
AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Tmp=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
Serial.print("AcX = “); Serial.print(AcX);
Serial.print(” | AcY = “); Serial.print(AcY);
Serial.print(” | AcZ = “); Serial.print(AcZ);
Serial.print(” | Tmp = "); Serial.print(Tmp/340.00+36.53); //equation for temperature in degrees C from datasheet

// Figure out how many leds would be lit, and in what direction
int XLit = abs(AcX) / (25000/(NUM_LEDS/2));
int YLit = abs(AcY) / (25000/(NUM_LEDS/2));

// sanity check, just in case we get larger values than we’re expecting!
if(XLit > (NUM_LEDS/2)) {
XLit = NUM_LEDS/2;
}

// empty out the existing leds
fill_solid(Xleds,NUM_LEDS,CRGB::Black);

if(AcX < 0) {
// we want to light up numLit leds, starting from the midpoint, going down
for(int xi = 1; xi <= XLit; xi++) {
Xleds[(NUM_LEDS/2)-xi] = CRGB::Green;
}
} else {
// we want to light up numLit leds, starting from the midpoint, going forward
for(int xi = 0; xi < XLit; xi++) {
Xleds[(NUM_LEDS/2)+xi] = CRGB::Yellow;
}
}

// sanity check, just in case we get larger values than we’re expecting!
if(YLit > (NUM_LEDS/2)) {
YLit = NUM_LEDS/2;
}

// empty out the existing leds
fill_solid(Yleds,NUM_LEDS,CRGB::Black);

if(AcY < 0) {
// we want to light up numLit leds, starting from the midpoint, going down
for(int yi = 1; yi <= YLit; yi++) {
Yleds[(NUM_LEDS/2)-yi] = CRGB::White;
}
} else {
// we want to light up numLit leds, starting from the midpoint, going forward
for(int yi = 0; yi < YLit; yi++) {
Yleds[(NUM_LEDS/2)+yi] = CRGB::Red;
}
}

LEDS.show();
}

Too complex for my old brain. I just use sine waves and let pi do the heavy lifting.

Could that work for what i’m doing?

Well, I’ve got a pile o’ stuff at https://github.com/atuline/FastLED-Demos.

Feel free to look around and see if anything suits your taste. My emphasis is on loop friendly and simple algorithms. You may be interested in my one_sin and two_sin demo’s.

Instead of filling the leds with a solid black, try doing this after calling the show:

for(int i =0; i < NUM_LEDS; i++){
leds[i].nscale8(200);
}

Each loop through, the library will slowly scale the brightness down to 0. I’ve found this to be my trick at anti-alising single dot animations. Your code will require a bit of tweaking as you have multiple arrays.

Wonderful, I will look into them tomorrow after work. Thank you! I appreciate it!

I will try that tomorrow too! :slight_smile: thank you. I’ll post what I have right now under “show off your work.”

Take a look at Starfield.cpp here for a simple implementation of 1D antialiasing: https://bitbucket.org/ratkins/technocolourdreamcoat2014

@Jon_Burroughs I tried your suggestion, it didnt seem to work, but its highly likely that i’m doing something wrong…
here’s what my code looks like now:

// Figure out how many leds would be lit, and in what direction
int XLit = abs(AcX) / (25000/(NUM_LEDS/2));
int YLit = abs(AcY) / (25000/(NUM_LEDS/2));

// sanity check, just in case we get larger values than we’re expecting!
if(XLit > (NUM_LEDS/2)) {
XLit = NUM_LEDS/2;
}

// empty out the existing leds
fill_solid(Xleds,NUM_LEDS,CRGB::Black);

if(AcX < 0) {
// we want to light up numLit leds, starting from the midpoint, going down
for(int xi = 1; xi <= XLit; xi++) {
Xleds[(NUM_LEDS/2)-xi] = CRGB::Green;
}
} else {
// we want to light up numLit leds, starting from the midpoint, going forward
for(int xi = 0; xi < XLit; xi++) {
Xleds[(NUM_LEDS/2)+xi] = CRGB::Yellow;
}
}

// sanity check, just in case we get larger values than we’re expecting!
if(YLit > (NUM_LEDS/2)) {
YLit = NUM_LEDS/2;
}

// empty out the existing leds
fill_solid(Yleds,NUM_LEDS,CRGB::Black);

if(AcY < 0) {
// we want to light up numLit leds, starting from the midpoint, going down
for(int yi = 1; yi <= YLit; yi++) {
Yleds[(NUM_LEDS/2)-yi] = CRGB::White;
}
} else {
// we want to light up numLit leds, starting from the midpoint, going forward
for(int yi = 0; yi < YLit; yi++) {
Yleds[(NUM_LEDS/2)+yi] = CRGB::Red;
}
}

LEDS.show();
for(int xi =0; xi < NUM_LEDS; xi++){
Xleds[xi].nscale8(200);
}
for(int yi =0; yi < NUM_LEDS; yi++){
Yleds[yi].nscale8(200);
}

Get rid of these code blocks, the nscale8 is meant to replace having to call everything black between frames:

// empty out the existing leds
fill_solid(Xleds,NUM_LEDS,CRGB::Black);

// empty out the existing leds
fill_solid(Yleds,NUM_LEDS,CRGB::Black);

And, the value of nscale8(value), is what you should experiment with to change the amount of fade. If you use nscal8_video(), the leds never go all the way dark, it’s a cool feature to implement just to see for yourself. Good luck.

The issue i’m having now is that the LEDs are still either on or off. i’m trying to code it so that if (for example) the value of AcX is halfway between illuminating LEDs 1 and 2, Led 2 will light up half as bright as led 1. I’m trying to adapt one of the example sketches from FastLED to use a sine wave to fade the LEDs in and out with the changing values of AcX and AcY.

i see you put the number of led to light into an integer.
If you do the same calculation a second time but now put it into a floating point var. and then subtract the 2 from each other.

you should end up with a number somewhere between 0 en 1.

So all you need to do now is multiply that number by 255 to get the brightness value of you last led ?