Hello, I have a 5x5 grid of LEDs using a WS2812b strip. I need to check if 4 consecutive LEDs are either all blue or all red and if they are I need to change the color of those 4 LEDs to green.
It basically connects four on a 5x5 grid, so the 4 consecutive LEDs can be in a row, a column, or a diagonal.
I figured out that there are 28 possibilities and of course I can just write an if statement for each possibility, but I know there is a better way to do it, I just don't know how. I would appreciate it if someone led me in the right direction. Thanks!
For 5 LEDs there exists a solution: magic square. But this won't work for 4 LEDs in a 5x5 matrix
If you have a an array of rows and cols, you can try to find a first LED on. Then check in all 8 directions for another LED on, and if found continue checking in that (and opposite) direction.
Hello, I have a 5x5 grid of LEDs using a WS2812b strip. I need to check if 4 consecutive LEDs are either all blue or all red
Won't you know that because your program has already instructed the LEDs to take a particular colour?
The LED colors exist in a 5-wide array. Set consecutive counts (conCnt) = 0. Store first element color (elmtClr) and increment conCnt. Test second element. If same as first, increment conCnt - else set elmtClr to new color and set conCnt back to one. Test third element. If not same as elmtClr this row cannot contain four consecutive same colors. If is same test to end of row. If any new element color does not match elmtClr no match is possible. If conCnt reaches four there's a match. Use the index of the first in the string to step through the color change. Lather, rinse, repeat for succeeding rows.
Do something similar for the columns and diagonals.