Pages: [1] 2   Go Down
 Author Topic: Programming ??? Math Question  (Read 474 times) 0 Members and 1 Guest are viewing this topic.
Offline
Newbie
Karma: 1
Posts: 13
 « on: February 27, 2013, 09:59:03 pm » Bigger Smaller Reset

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: 15
Posts: 717
 « Reply #1 on: February 28, 2013, 01:08:58 am » Bigger Smaller Reset

Okay. But what *does* happen?  What exactly goes wrong?
 Logged

Sydney
Offline
God Member
Karma: 14
Posts: 716
Big things come in large packages
 « Reply #2 on: February 28, 2013, 01:54:55 am » Bigger Smaller Reset

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: 316
Posts: 35535
Seattle, WA USA
 « Reply #3 on: February 28, 2013, 05:27:02 am » Bigger Smaller Reset

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
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
 « Reply #4 on: February 28, 2013, 05:29:46 am » Bigger Smaller Reset

This is my first post and I am happy to be using Arduino!!!

http://arduino.cc/forum/index.php/topic,148850.0.html

Point 7 plus the other ones.
 Logged

Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
 « Reply #5 on: February 28, 2013, 05:30:54 am » Bigger Smaller Reset

...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 » Bigger Smaller Reset

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 » Bigger Smaller Reset

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
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
 « Reply #8 on: February 28, 2013, 02:42:48 pm » Bigger Smaller Reset

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?

Code:
// 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:

Code:
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
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
 « Reply #9 on: February 28, 2013, 02:45:24 pm » Bigger Smaller Reset

If we change "calculate the length" like this:

Code:
// calculate the legnth
float xl = xb - xa;
float yl = yb - ya;
float zl = zb - za;

We get:

Code:
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: 316
Posts: 35535
Seattle, WA USA
 « Reply #10 on: February 28, 2013, 06:27:42 pm » Bigger Smaller Reset

Quote
@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 » Bigger Smaller Reset

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)
Online
Edison Member
Karma: 48
Posts: 1428
 « Reply #12 on: March 04, 2013, 11:17:52 am » Bigger Smaller Reset

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 » Bigger Smaller Reset

@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 » Bigger Smaller Reset

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.