Pages: [1] 2   Go Down
Author Topic: Programming ??? Math Question  (Read 903 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
God Member
*****
Karma: 20
Posts: 835
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1184
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset 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

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46091
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Brattain Member
*****
Karma: 238
Posts: 24364
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

...and why is "color" never mentioned again?
Logged

"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.

Offline Offline
Newbie
*
Karma: 1
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Jr. Member
**
Karma: 1
Posts: 84
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Brattain Member
*****
Karma: 549
Posts: 46091
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Newbie
*
Karma: 1
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Online
Faraday Member
**
Karma: 89
Posts: 3482
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 1
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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. smiley
Logged

Worst state in America
Offline Offline
God Member
*****
Karma: 23
Posts: 665
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset 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.


Check out this link: http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm

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.

Logged

Pages: [1] 2   Go Up
Jump to: