Breaking News
Comments
Newest First | Oldest First | Threaded View
Page 1 / 3   >   >>
Max The Magnificent
User Rank
Blogger
Re: Simplest
Max The Magnificent   10/28/2013 4:46:34 PM
NO RATINGS
@jpessin: It's probably not the most efficient option computationally, but perhaps the simplest solution would be to use the Arduino language's map() function.

This just goes to show that I really need to spend some time on the Arduino website looking to see what's there. I'm currently ver yhappy with the solution proposed by Javi (Garcia) earlier in these comments, but it's great to know that this (and other) functions are available.

 

Max The Magnificent
User Rank
Blogger
Re: Distance calculation ...
Max The Magnificent   10/28/2013 4:41:18 PM
NO RATINGS
@Bastian.Schick: In the ancient times we used a modified Bresenham algorithm to fade between colours...

You make it sound as though you are talking about the last millennium .... Oh, wait a moment... LOL

Max The Magnificent
User Rank
Blogger
Re: Use fractions
Max The Magnificent   10/28/2013 4:39:53 PM
NO RATINGS
@another nickname: Your truncation problem would not be there if you stick with fractions.

True enough -- but the problem also goes away if we use the solution proposed by Javi (Garcia) in the early comments to this blog.

Max The Magnificent
User Rank
Blogger
Re: Rounding integer division
Max The Magnificent   10/28/2013 4:38:12 PM
NO RATINGS
@DU0...01: Though the Bresenham algorithm might be appropriate...

I know this is not complex -- but I can imagine the problems that woudl ensue trying to explain all of this to an absolute beginner :-)

jpessin
User Rank
Rookie
Simplest
jpessin   10/28/2013 4:26:42 PM
NO RATINGS
It's probably not the most efficient option computationally, but perhaps the simplest solution would be to use the Arduino language's map() function.  It seems to work going up and going down in color value, and ends on exactly the right value, in the right number of steps.

http://arduino.cc/en/Reference/Map

 

This gives:

void lightLEDs (int howMany,
                int howLong)
{
    for (int i = 1; i <= howMany; i++)
    {
        int tmpColor = map(i, 0, howMany, oldColor, newColor);

        // Use current tmpColor to drive LED

        delay(howLong);
    }
}

Bastian.Schick
User Rank
Rookie
Re: Bresenham algorithm
Bastian.Schick   10/27/2013 2:50:53 AM
NO RATINGS
Sorry I did double this. But yes, Bresenham is the IMHO the best choice to move in descrete steps from one point to another.

Bastian.Schick
User Rank
Rookie
Distance calculation ...
Bastian.Schick   10/27/2013 2:49:14 AM
NO RATINGS
Two things:
- The distance between 0 and 255 is (255-0)+1

- In the ancient times we used a modified Bresenham algorithm to fade between colours.


Cheers

another nickname
User Rank
Rookie
Use fractions
another nickname   10/26/2013 8:29:22 PM
NO RATINGS
Your truncation problem would not be there if you stick with fractions.

Just keep 2 integers for each number numerator and denominator) and rewrite your algorithm using 2 numbers instead of one for all calcluations and do multiplications first and divisions last. It's pretty much the same as Fixed Point arithmetics .

DU00000001
User Rank
CEO
Rounding integer division
DU00000001   10/26/2013 7:18:27 PM
NO RATINGS
Though the Bresenham algorithm might be appropriate, the initial question was how to cope with the truncating element of the integer division.

The solution is really simple:

result = (dividend + (divisor/2))/divisor   or
result = (dividend + (divisor>>1))/divisor

This might lead us to the next level of discussion: what if divisor is odd?

You simply have to decide whether you want to round up (x.5 => x+1) or down (x.5 => x). In one of the cases the above equations will read ((divisor+1)/2) resp. ((divisor+1)>>1).

There's always a last philosophical question or, in other words, the freedom of choice :)

Max The Magnificent
User Rank
Blogger
Re: Bresenham algorithm
Max The Magnificent   10/26/2013 6:01:09 PM
NO RATINGS
@pviry750: The standard way to do this efficiently is the Bresenham algorithm...

Cool beans -- I will bounce over to peruse and ponder the Wikipedia page on this -- I'm sure I will be doing a lot more of this in the future -- for example, with regard to my current robot project.

Page 1 / 3   >   >>


Flash Poll
EE Life
Frankenstein's Fix, Teardowns, Sideshows, Design Contests, Reader Content & More
Rishabh N. Mahajani, High School Senior and Future Engineer

Future Engineers: Don’t 'Trip Up' on Your College Road Trip
Rishabh N. Mahajani, High School Senior and Future Engineer
3 comments
A future engineer shares his impressions of a recent tour of top schools and offers advice on making the most of the time-honored tradition of the college road trip.

Max Maxfield

Juggling a Cornucopia of Projects
Max Maxfield
7 comments
I feel like I'm juggling a lot of hobby projects at the moment. The problem is that I can't juggle. Actually, that's not strictly true -- I can juggle ten fine china dinner plates, but ...

Larry Desjardin

Engineers Should Study Finance: 5 Reasons Why
Larry Desjardin
37 comments
I'm a big proponent of engineers learning financial basics. Why? Because engineers are making decisions all the time, in multiple ways. Having a good financial understanding guides these ...

Karen Field

July Cartoon Caption Contest: Let's Talk Some Trash
Karen Field
140 comments
Steve Jobs allegedly got his start by dumpster diving with the Computer Club at Homestead High in the early 1970s.

Top Comments of the Week
Like Us on Facebook
EE Times on Twitter
EE Times Twitter Feed

Datasheets.com Parts Search

185 million searchable parts
(please enter a part number or hit search to begin)