New to the forum, and hoping I have this in the right location.
I would like to show my Grandson some fun code with some LED lights. He got a kick out of seeing how the computer could control individual LEDs. I would like to take this a bit further than just lighting up a pixel or two. I'm still new to this Arduino coding; so I am hoping someone here can help.
What i have is a matrix of WS2811s. The matrix is 20 wide by 25 tall.
What I would like to do is to draw a radar sweep line from the center pixel out to the edges where it will rotate around the board.
I wouldn't think that this is too difficult, but as new as I am to the Arduino I am not sure where or how to begin to do this. I did a bit of browsing to see if I could locate an example, but came up blank... Hopefully someone here can help?!? This was one of my first things I programmed when learning MSDOS Basic language. I thought it would be fun to bring it full circle (pun intended) and learn to do it with an Arduino and LEDs.
Once I get a radar sweep going, I plan on adding in a random pixel and when the sweep hits it the surrounding lights will flash & fade.
Too difficult to start out with?!?
Too difficult to start out with?
When you did it with BASIC then under the hood was the code that could draw straight lines on a screen. This is the same code you want to use on your matrix. There are several ways of doing this but one way is called the Bresenham line algorithm
But first start by writing a function that will work out the LED number for a given X and Y location. The function will depend on if you have wired the matrix as a raster or serpentine raster. Use that to plot single points. This then becomes the function that the Bresenham algorithm feeds into.
Alternatively you can just use one of these, but they will not be as fast. Line drawing algorithm
I started last night writing a function for the X and Y locations. Has not been as simple as I thought it would be.
Also just looked over that Bresenham algorithm.... Yup, probably bitten off more than I can chew here!
Think I will just start at the function for X and Y .... See if I can just get that to work and draw a circle on the matrix. If I can get that far I will consider myself lucky!
Has not been as simple as I thought it would be.
But have you got it working?
Not even close.
Really didn't get too far in it though. But I know from past experience I have a LONG LONG way to go.
Just can't seem to get the 'feel' for programming this arduino. My head keeps reverting back to basic
See if this makes it better:-
The linear raster simply starts each column where the previous one left off.
The columns are one continuous strip and the rows formed by laying strips adjacently. In this case the three wires have to be taken from the top of one column to the bottom of the next. This sort of wiring is not the best as the long runs for the signal can end up degrading it and interference can be picked up. On the plus side the addressing is simpler. That means converting the X - Y values of an LED into the LED number along the strip (buffer). Given any X - Y value the LED number is simply:-
LED number = Y + (X * number of LEDs in a column)
A much better way to wire up a rectangular display is by making a serpentine raster. This is where the top of one column is wired up to the top of the next. This means that the wiring between the sections of LED strip are short and thus more robust.
Here the conversion from X - Y values to strip values are just a little more involved but that what software is for. In the case of even numbered columns the conversion is simply what we had before, with:-
LED number = Y + (X * number of LEDs in a column)
However for odd numbered columns the conversion is :-
LED number = (X * number of LEDs in a column) + (number of LEDs in a column -1 -Y)
To determine if the X coordinate is odd or even, simply look at the least significant bit of the X value and if it is a zero then it is an even column or if it is a one it is an odd column. So all the software has to do is to examine the X - coordinate and decide what formula to use. It is simple enough to make a function that returns the LED number given the X & Y values.
Thanks, that will help.
My matrix is in a serpentine I believe:
0, 1, 2, 3
7, 6, 5, 4
8, 9, 10, 11
Your formulas here will be helpful! Now I just need to get the time to work on it!! HAHA
Life always seems to get in the way unfortunately. I'll get there though.
I would rotate it 90o counter clockwise, so that pixel zero is in the bottom left hand corner. It is only then the formula will work.
Easy enough to do. Thanks!
Let me ask you something that crossed my mind while working on a function to convert the X Y information into a pixel number...
That Bresenham line algorithm was pretty involved (as far as I could understand - may have to get my daughter involved in this one! LOL). But wouldn't there be a simpler solution?
Based on a radius of 10 pixels, and knowing where the center is, I can determine the XY coordinates of the circumference with simple math. So if I run a loop from 0 to 360 (angles) I can light the whole circle. If I take this a step further and run a loop from 1 to 10 for the radius at each of the angles, I should be able to effectively light the line for the sweep. With only a 20 x 25 matrix it isn't going to be pretty - i.e. straight; but essentially it should work right?
That Bresenham line algorithm was pretty involved
The point is that it is very computationally efficient in terms of the number of the number of multiplication / divisions that need doing.
If you don't consider that then the Line drawing algorithm link I posted is simpler to understand ( did you read that ? ) . That just has a fractional increment and increment to apply to the X&Y to get the next point on the line. It is a bit like what you said but simpler.