Array "[Device] ERROR: indexing array with integer"

I am creating a fade function for transitioning a NeoPixel from one colour to another. I keep getting this error when trying to update the array.

“ERROR: indexing array with integer”

Here is the function code:
function fade(colour1, colour2) { // smoothly fade from colour1 to colour2 with a fixed number of steps steps <- 16.0; // fade in 16 steps inc <- [0,0,0,0]; inc[0] = (colour2[0] - colour1[0])/steps; inc[1] = (colour2[1] - colour1[1])/steps; inc[2] = (colour2[2] - colour1[2])/steps; inc[3] = (colour2[3] - colour1[3])/steps; server.log("Gradient: " + colour1[0] + "," + colour1[1] + "," + colour1[2] + "," + colour1[3] + " -- fading to --> " + colour2[0] + "," + colour2[1] + "," + colour2[2] + "," + colour2[3]); server.log("RGBW Increments: " + inc[0] + ", " + inc[1] + ", " + inc[2] + ", " + inc[3]); for (local gradient = 1; gradient <= steps; gradient++) { for (local component = 0; component <= 3; component++) { server.log("Old component: " + colour[component]); server.log("New component: " + round((colour[component] + inc[component]),0)); colour[component] <- (round((colour[component] + inc[component]),0)).tointeger; } server.log("Fade: " + colour[0] + "," + colour[1] + "," + colour[2] + "," + colour[3]); } colour <- colour2; }

The function is passed 2 arrays which hold the RGBW colour values. This is the line which gives the error:
colour[component] <- (round((colour[component] + inc[component]),0)).tointeger;
If I comment out this one line, the code runs as expected (except for actually incrementing the colour to the next gradient step).

I’m building this up in steps and realize that I’ll have to add checking to ensure that the colour values do not exceed the range 0 - 255 and that the increment doesn’t step past the final colour (not that that is a big issue). But at this point, I’m just validating the code by outputting to the server log.

Is “colour” declared outside of this function? It appears to be only mentioned after you index it.

Yes. It is defined at the beginning of the program as an array:

colour <- [80,5,0,0]; // Initial colour

I’ve cleaned up the code a bit, but it still is giving me the same error. I removed the server.log output lines to clean it up, but if I comment out either of the problem lines below, it runs without error.

`
function fade(colour1, colour2) {
// smoothly fade from colour1 to colour2 with a fixed number of steps
steps <- 16.0; // fade in 16 steps
inc <- [0,0,0,0];
inc[0] = (colour2[0] - colour1[0]) / steps;
inc[1] = (colour2[1] - colour1[1]) / steps;
inc[2] = (colour2[2] - colour1[2]) / steps;
inc[3] = (colour2[3] - colour1[3]) / steps;

for (local gradient = 1; gradient <= steps; gradient++) {
    for (local component = 0; component <= 3; component++) {
        newComponent <- (round((colour1[component] + inc[component] * gradient),0)).tointeger();
        if (inc[component] >= 0) {
            if (newComponent > colour2[component]) {
                colour[component] <- colour2[component];
            } else {
                colour[component] <- newComponent; // this is the problem line
            }
        } else {
            if (newComponent < colour2[component]) {
                colour[component] <- colour2[component];
            } else {
                colour[component] <- newComponent; // this is the problem line
            }   
        }
    }
}
colour <- colour2;

}
`

If colour is already defined as an array,
eg colour <- [0,0,0,0], then you shouldn’t use a new slot operator (<-) with it. Just use colour[component] = newComponent;

I don see anything wrong with the code above, but got rid of the error by changing the code slightly to this (main change was the variable declarations):

function fade(colour1, colour2) { // smoothly fade from colour1 to colour2 with a fixed number of steps local steps = 16.0; // fade in 16 steps local newColour = [0,0,0,0]; local newComponent = 0; local inc = [0,0,0,0]; inc[0] = (colour2[0] - colour1[0]) / steps; inc[1] = (colour2[1] - colour1[1]) / steps; inc[2] = (colour2[2] - colour1[2]) / steps; inc[3] = (colour2[3] - colour1[3]) / steps; for (local gradient = 1; gradient <= steps; gradient++) { for (local component = 0; component <= 3; component++) { newComponent = (round((colour1[component] + (inc[component] * gradient)),0)).tointeger(); newColour[component] = newComponent; if (inc[component] >= 0) { if (newComponent > colour2[component]) { colour[component] = colour2[component]; } else { colour[component] = newComponent; } } else { if (newComponent < colour2[component]) { colour[component] = colour2[component]; } else { colour[component] = newComponent; } } } server.log("setNeoPixelStrip(" + newColour[0] + "," + newColour[1] + "," + newColour[2] + "," + newColour[3] + ");"); } colour = colour2; }

if I call the function with the values:
colour1 = 80,5,0,0
colour2 = 3,1,0,1
It calculates that the increments for the gradient steps as:
RGBW Increments: -4.81250, -0.25, 0, 0.0625

Here is the log that it outputs:

setNeoPixelStrip(75,5,0,0); setNeoPixelStrip(65,5,0,0); setNeoPixelStrip(51,4,0,0); setNeoPixelStrip(32,3,0,0); setNeoPixelStrip(8,2,0,0); setNeoPixelStrip(-21,1,0,0); setNeoPixelStrip(-31,-1,0,0); setNeoPixelStrip(-35,-1,0,1); setNeoPixelStrip(-40,-1,0,2); setNeoPixelStrip(-45,-1,0,2); setNeoPixelStrip(-50,-2,0,2); setNeoPixelStrip(-55,-2,0,2); setNeoPixelStrip(-60,-2,0,2); setNeoPixelStrip(-64,-2,0,2); setNeoPixelStrip(-69,-3,0,2); setNeoPixelStrip(-74,-3,0,2);

The strange thing is that the components jump in a geometric rate until they become negative (which is when the comparison discards them and sets them equal to colour2).

Since I never change the value of colour1, shouldn’t it stay constant throughout the function? It would appear that it is being updated somehow during the loop. My intent with the following calculation was:

newComponent = (round((colour1[component] + (inc[component] * gradient)),0)).tointeger();

newComponent = 80 + increment1
newComponent = 80 + increment
2
newComponent = 80 + increment*3
etc

But it is behaving more like:
newComponent = 80 + increment1
newComponent = newComponent + increment
2
newComponent = newComponent + increment*3
etc