Offline
Newbie
Karma: 1
Posts: 13
|
 |
« on: February 27, 2013, 09:59:03 pm » |
Hello All,
This is my first post and I am happy to be using Arduino!!!
I recently built an 8x8x8 rgb led cube. Well almost done, I have two 8x8 towers going and I am having some fun turning independent LEDs on and off. Now I am trying to write some more advanced graphic routines to turn on more then single LEDs. For example lines and circles. I just can't seem to get the line rountine to work. Any help would be appreciated.
// set all the points on a line to a color void Line(float xa, float ya, float za, float xb, float yb, float zb, int color) { float x,y,z; // calculate the legnth float xl = xa-xb; float yl = ya-yb; float zl = za-zb; for (float t=0; t<8.0f; t++) { x = xa + ((xl/8.0f) * t); y = ya + ((yl/8.0f) * t); z = za + ((zl/8.0f) * t); Point(x,y,z,6); } }
|
|
|
|
« Last Edit: February 27, 2013, 10:05:09 pm by galantida »
|
Logged
|
|
|
|
|
Ontario
Offline
God Member
Karma: 16
Posts: 736
|
 |
« Reply #1 on: February 28, 2013, 01:08:58 am » |
Okay. But what *does* happen? What exactly goes wrong?
|
|
|
|
|
Logged
|
|
|
|
|
Sydney
Offline
God Member
Karma: 15
Posts: 743
Big things come in large packages
|
 |
« Reply #2 on: February 28, 2013, 01:54:55 am » |
I would multiply xl by 8 and then divide by t. You would be dealing with bigger numbers and less rounding errors.
As mentioned you need to be specific with what is not working for you.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 336
Posts: 36476
Seattle, WA USA
|
 |
« Reply #3 on: February 28, 2013, 05:27:02 am » |
You want to be able to set the LEDs along a line from (1.2, 5.6, 92.7) to (-184,5, 993.02, -15.392) to a color? Does that really make sense? Your LEDs are at integer positions, are they not? There are some bounds on the reasonable extent of the values, are there not?
What does the Point() function do, Mr. Snippet?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Online
Shannon Member
Karma: 226
Posts: 14118
Lua rocks!
|
 |
« Reply #4 on: February 28, 2013, 05:29:46 am » |
This is my first post and I am happy to be using Arduino!!!
http://arduino.cc/forum/index.php/topic,148850.0.htmlPoint 7 plus the other ones.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 143
Posts: 19379
I don't think you connected the grounds, Dave.
|
 |
« Reply #5 on: February 28, 2013, 05:30:54 am » |
...and why is "color" never mentioned again?
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 1
Posts: 13
|
 |
« Reply #6 on: February 28, 2013, 11:46:14 am » |
Thanks for all the replies and sorry for the missing information. I have working with this cube so long that what I think is basic is really not.
The Point function lights the LED at x,y,z at a specified color. My cubes originating point is one of its corners so you would pass any number from zero to seven in the x,y,z. My Cube is also capable of 6 colors so that is also zero to seven. The reason color variable is never used again was I hard coded a six in the point function for color when I was testing.
What the code does as is... I pass the coordinates to draw a line from 4,1,4 to 4,6,4 at color 6. [e.g. Line(4,1,4,4,6,4,6) ]
I would think this would light the six LEDs between those two points
4,1,4 4,2,4 4,3,4 4,4,4 4,5,4 4,6,4
what it does is light 4,1,4 and 4,1,4.
@PaulS: Are the numbers you list really the ones you came up with. When I walk through the code I does get decimal values that need to be rounded but none are as high as 92? I might be miss calculating?
Thanks for all the replies Dave
|
|
|
|
|
Logged
|
|
|
|
|
Finland
Offline
Jr. Member
Karma: 1
Posts: 84
|
 |
« Reply #7 on: February 28, 2013, 02:24:03 pm » |
Now that I think about, I've never actually tried to do an increment operation on a floating point number. I would have tried something like t+=1.0f myself not knowing would the post increment operation also work. Then again, you could implement a line drawing algorithm using integers for efficiency, but I suppose I'm only nitpicking here.. Also, ya-yb is a negative, is that what you intended in your design? Maybe that's the key problem here..
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Online
Shannon Member
Karma: 226
Posts: 14118
Lua rocks!
|
 |
« Reply #8 on: February 28, 2013, 02:42:48 pm » |
I pass the coordinates to draw a line from 4,1,4 to 4,6,4 at color 6. [e.g. Line(4,1,4,4,6,4,6) ]
I would think this would light the six LEDs between those two points
4,1,4 4,2,4 4,3,4 4,4,4 4,5,4 4,6,4
what it does is light 4,1,4 and 4,1,4.
Well, let's test it shall we? // set all the points on a line to a color void Line(float xa, float ya, float za, float xb, float yb, float zb, int color) { float x,y,z; // calculate the legnth float xl = xa-xb; float yl = ya-yb; float zl = za-zb; for (float t=0; t<8.0f; t++) { x = xa + ((xl/8.0f) * t); y = ya + ((yl/8.0f) * t); z = za + ((zl/8.0f) * t); Point(x,y,z,6); } } void Point (int x, int y, int z, int color) { Serial.print (x); Serial.print (", "); Serial.print (y); Serial.print (", "); Serial.print (z); Serial.println (); } void setup () { Serial.begin (115200); Serial.println (); Line(4,1,4,4,6,4,6); } // end of setup
void loop () { }
Output: 4, 1, 4 4, 0, 4 4, 0, 4 4, 0, 4 4, -1, 4 4, -2, 4 4, -2, 4 4, -3, 4
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Online
Shannon Member
Karma: 226
Posts: 14118
Lua rocks!
|
 |
« Reply #9 on: February 28, 2013, 02:45:24 pm » |
If we change "calculate the length" like this: // calculate the legnth float xl = xb - xa; float yl = yb - ya; float zl = zb - za;
We get: 4, 1, 4 4, 1, 4 4, 2, 4 4, 2, 4 4, 3, 4 4, 4, 4 4, 4, 4 4, 5, 4
Do you see how adding debugging displays helps? You don't need to speculate, add some Serial.print and see for yourself what is happening.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 336
Posts: 36476
Seattle, WA USA
|
 |
« Reply #10 on: February 28, 2013, 06:27:42 pm » |
@PaulS: Are the numbers you list really the ones you came up with. When I walk through the code I does get decimal values that need to be rounded but none are as high as 92? I might be miss calculating? No, I was trying to make two points. First, I don't see any sanity checking of the input data. That's usually a good idea. Second, I don't understand why the inputs are floats. Don't the line ends correspond to positions where the LEDs are located in the matrix? Aren't the LEDs uniformly spaced? That is, at 0, 1, 2, etc., rather than 0.1, 1.3, 2.7, etc.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 1
Posts: 13
|
 |
« Reply #11 on: March 04, 2013, 09:41:42 am » |
Hello thanks again or the replies.
I am an experienced developer but I am new to Arduino style programming. I see now from the replies that I really have to look in to better ways to debug. I assumed that since the code runs on the Arduino I could not use the debug area at the bottom of the development environment. I'll look in to Serial.Print as suggested.
Also there were some questions about why I used floats. Depending on the angle of the line I my be incrementing it by very small values each cycle. For example a line that starts on one row and ends on the next will increment about 0.2 each time. The .2 is not really important but if you throw it out you will never get the next row.
I could have limited my use of floats to the number I am incrementing but I have found in some languages that if you mix in even one integer the answer could be rounded to integer. Not sure how Arduino does it but I was trying to get the right answer before I started improving the code. I hope to get the hang of these Arduino C++ nuances.
Thanks again everyone!! Dave G
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 55
Posts: 1601
May all of your blinks be without delay
|
 |
« Reply #12 on: March 04, 2013, 11:17:52 am » |
Are you using the Arduino IDE ? If so, then Serial.print sends it output to the IDE's Serial monitor or another serial device listening on the COM port that the USB interface creates.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 1
Posts: 13
|
 |
« Reply #13 on: March 04, 2013, 11:29:27 am » |
@UKHeliBob: I am use the IDE. For some reason I didn't think it would be that easy. I thought I would have to write sending and receiving code. I'll give it a shot when I get home tonight. The more I learn about my Arduino the more I am pleasantly surprised. 
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 15
Posts: 463
|
 |
« Reply #14 on: March 04, 2013, 11:55:02 am » |
Hello All,
This is my first post and I am happy to be using Arduino!!!
I recently built an 8x8x8 rgb led cube. Well almost done, I have two 8x8 towers going and I am having some fun turning independent LEDs on and off. Now I am trying to write some more advanced graphic routines to turn on more then single LEDs. For example lines and circles. I just can't seem to get the line rountine to work. Any help would be appreciated.
Check out this link: http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithmThat's Bresenham's line draw algorithm. It's used in any graphics code that draws a line from one point to another. Working with a cube, you have a THREE dimensional space to work with. You may wish to divide the cube into two parallel square planes, one located at the line start and the other at the line end. Then distance between the planes is the line length and the X/Y coordinates in each plane locate the start and end point. Then, use Bresenham to calculate all the points in between.
|
|
|
|
|
Logged
|
|
|
|
|
|