AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
iw2nzm
PostPosted: Jun 17, 2009 - 08:05 PM
Hangaround


Joined: Oct 29, 2007
Posts: 134
Location: Como, Italy

Hello,

I'm playing with an RGB led. I use a Tiny85 with 3 hw PWM. I created a struct that defines the current state of the led (R, G, B values). Then I can set the next color and the time it needs to reach that. The routine change the color softly with a linear interpolation.

Two questions:

1) I know it's better to use a linear control of brightness in color mixing application. Anyway, I'd like to try an exponential control. I can map the whole exp curve in flash as an array[0..255] but I'm wondering if there is a better way to achieve that.

2) To random the color I random each channel, but it's not very nice. There are a lot of bright colors and few pure ones. Do you know any smart way to get a cute effect?

Thanks
Marco
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
glitch
PostPosted: Jun 17, 2009 - 08:13 PM
Raving lunatic


Joined: Jan 12, 2002
Posts: 7004
Location: Canada

if by "pure" colour you mean colours that are 100% intensity of any combination of the 3 primaries, including a single primary. Then simply generate 3 random bits, each bit represents one of the primaries. Then generate a random time. Now run the fade from the current colour, to the new "pure" colour over the specified time. And then repeat.

If you want more complex "pure" colours, create a palette of these colours, and then randomly select the entry, and again simply fade to it, and repeat.

As for the exponential mixing, you'll need at least 12 bits of output, for the 8 bit input. Look-up table works well for this. Though if you have plenty of time between lookups, simply calculate it by brute force.
 
 View user's profile Send private message  
Reply with quote Back to top
jayjay1974
PostPosted: Jun 17, 2009 - 09:57 PM
Raving lunatic


Joined: Oct 30, 2002
Posts: 3922
Location: The Netherlands

I fairly recently made a 'color wheel' with an RGB LED, and linear intensity control does not work properly. The difference in perceived brightness from 40 to 50% is much bigger then from 80-90%; so you need gamma correction, I believe it's around 0.76 for LEDs.

I calculated the required RGB intensity from hue, saturation and brightness, with the latter two being fixed to maximum.

My goal was to end up with a LED that has fixed brightness but only changing colours; it did not work, but my 1 year old daughter likes it anyway Smile

Anyway, this is the code I wrote for a T85:
Code:

 hi=hue/60;  // Hue varies between 0 and 359
 f=(hue % 60)*425/100;
 q=(255-f);

 switch(hi)
  {
  case 0: r=255; g=f;   b=0;   break;
  case 1: r=q;   g=255; b=0;   break;
  case 2: r=0;   g=255; b=f;   break;
  case 3: r=0;   g=q;   b=255; break;
  case 4: r=f;   g=0;   b=255; break;
  case 5: r=255; g=0;   b=q;   break;
  }

 OCR0B=255-r;
 OCR1B=255-g;
 OCR0A=255-b;


I would use a simple lookup table, you can generate these easily in a spreadsheet.

_________________
Free e-books
Modern Mechanix: Yesterday's Tommorow Today
 
 View user's profile Send private message  
Reply with quote Back to top
Torby
PostPosted: Jun 18, 2009 - 02:48 PM
Resident


Joined: Nov 11, 2003
Posts: 934
Location: Chicago Illinois USA

For exponential, just multiply your level instead of incrementing. Like a shift left.

_________________
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits