Hey everybody! I am working on an Arduino project and I am trying to

Hey everybody!
I am working on an Arduino project and I am trying to figure out a way to store the past 5 values from the LED’s in a little neopixel stick(8 LED’s total). Once I have stored the values I want to average them out and use the new averaged value for the color of the LED’s.

I tried creating an array and looping through the CRGB element and storing the values that way but I kept getting a ton of errors about not being able to set certain kinds of variables. Is there some way of accessing the past values in an easier manner so that I can store them?

I’m also wondering how I would go about comparing the values. The documentation for the FastLED library seemed to be saying that they are all stored as individual bits one for each r, g, and b of each LED. I am setting the colors with the CHSV function and was wondering if there was a way to compare them using CHSV as well.

I’m still fairly new to Arduino so any helpful direction would be much appreciated.

Thanks!

Hi Dustin!
Once you overwrite the value of a variable with a new value, there’s no way to retrieve the old value. SO if you want to access the ‘old’ values, you’ll have to keep the ‘old’ values in a separate set of variables.

So in addition to the “leds” array, you might want to keep the previous values in a second array called “prevLeds”. That would let you keep one set of prior values. You could extend that as many layers as you wish, presuming that you don’t run out of RAM on the Arduino. You could also make a two-dimensional array, if that’s something you’re comfortable with.

And yes, you can compare CHSV values the same way you can compare CRGB values, just using ==.

Hey Mark,
Thanks for the input! I am comfortable working with multidimensional arrays. The thing I’m getting caught up on is what type of array prevLeds needs to be(would I make it a CRGB or just a two dimensional array) and how would I go about checking the values to store them(can I just use led[i] to get the value of each led).

I’ve tried something like this(abbreviated):
//at top of file
int prevLeds[8][5];
CRBG leds[8];

//in function
for(i=0; i>8; i++){
for(y=0; y>5; y++){
prevLeds[i][y] = leds[i];
}
}

but I kept getting errors about trying to set the wrong kind of int.

I’ve tried doing some Serial.print statements to get an idea of what I’m working with but I’m not having much luck. I am a web developer by day and I’m used to having a nice easy debug process so it’s been an interesting challenge working with the arduino!

If it would help I can post my code to pastebin so you can see it. Thanks!

You’re on the right track. Declare the 2-d as an array of CRGBs, and just put the values in there. The CRGB data type lets you just assign one to another.

The thing it doesn’t let you do in one step is Print a CRGB, but for that you can just print the r g and b fields separately.

If you are just interested in a moving average over time of the values, you can do it without storing all the previous values using this technique http://www.bennadel.com/blog/1627-create-a-running-average-without-storing-individual-values.htm

Russell - that approach let’s you incrementally compute an average of all past values. But it’s not a moving average. For a conventional “moving average” you need to subtract the values falling off the end (like 5 down) as well as add the new value.

One could compute an exponentially decaying average somewhat similarly, tho.