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.