Go Down

### Topic: Programming ??? Math Question (Read 2115 times)previous topic - next topic

#### galantida

##### Feb 28, 2013, 03:59 amLast Edit: Feb 28, 2013, 04:05 am by galantida Reason: 1
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);
}
}

#### gardner

#1
##### Feb 28, 2013, 07:08 am
Okay. But what *does* happen?  What exactly goes wrong?

#### marco_c

#2
##### Feb 28, 2013, 07:54 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.
Arduino Libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com
Arduino++ blog https://arduinoplusplus.wordpress.com

#### PaulS

#3
##### Feb 28, 2013, 11:27 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?

#### Nick Gammon

#4
##### Feb 28, 2013, 11:29 am

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.
Please post technical questions on the forum, not by personal message. Thanks!

http://www.gammon.com.au/electronics

#### AWOL

#5
##### Feb 28, 2013, 11:30 am
...and why is "color" never mentioned again?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### galantida

#6
##### Feb 28, 2013, 05:46 pm
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

#### Chaul

#7
##### Feb 28, 2013, 08:24 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..

#### Nick Gammon

#8
##### Feb 28, 2013, 08:42 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?

Code: [Select]
`// set all the points on a line to a colorvoid 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 setupvoid loop () { }`

Output:

Code: [Select]
`4, 1, 44, 0, 44, 0, 44, 0, 44, -1, 44, -2, 44, -2, 44, -3, 4`
Please post technical questions on the forum, not by personal message. Thanks!

http://www.gammon.com.au/electronics

#### Nick Gammon

#9
##### Feb 28, 2013, 08:45 pm
If we change "calculate the length" like this:

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

We get:

Code: [Select]
`4, 1, 44, 1, 44, 2, 44, 2, 44, 3, 44, 4, 44, 4, 44, 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.
Please post technical questions on the forum, not by personal message. Thanks!

http://www.gammon.com.au/electronics

#### PaulS

#10
##### Mar 01, 2013, 12:27 am
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.

#### galantida

#11
##### Mar 04, 2013, 03:41 pm
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

#### UKHeliBob

#12
##### Mar 04, 2013, 05:17 pm
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.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

#### galantida

#13
##### Mar 04, 2013, 05:29 pm
@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.

#### Krupski

#14
##### Mar 04, 2013, 05:55 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.

That'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.

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

Go Up

Please enter a valid email to subscribe