Go Down

Topic: Yet Another RGB LED N00b (Read 758 times) previous topic - next topic


Yup, bought a Luxeon RGB from Sparkfun, hooked it to the PWM pins (9,10,11) via a ULN2003, and grabbed one of the many serial RGB sketches available on the web.  
Since I wanted to test it with hyperterminal, I made it a bit more human-friendly, at the cost of taking more time and space.  The platform was an ATMega88 without a bootloader (the one I made didn't work, but I think the problem was the baud rate was off due to the CLKDIV8 being set), with a Dragon and an STK500.
Here is my code for your amusement and edification:
Code: [Select]
/*Serially controlled RGB-LED (originally by Roland Grichnik)
*17.03.2009.  Modified by Robert Diamond 7 Sep 2009
*inspired by the Serial tutorials and some threads on Arduino-Forum
*Thanks to mem and AlphaBeta from the Arduino Forum, which made serial communication another bit (if not a byte...) clearer for me!
byte r_pin = 9;
byte g_pin = 10;
byte b_pin = 11;

// number being constructed
byte n = 0;
// 1 = r, 2 = g, 3 = b
byte m = 0;
byte last_n = 0;
enum colorMode {

void setColor();

void setup() {
 analogWrite(r_pin, 0);
 analogWrite(g_pin, 0);
 analogWrite(b_pin, 0);

void loop() {  
 if (Serial.available()) {
   byte s = Serial.read();
   switch(s) {
     case 'R':
   case 'r':
     if (m) setColor();
     m = MODE_R;
   case 'G':
   case 'g':
     if (m) setColor();
     m = MODE_G;
   case 'B':
   case 'b':
     if (m) setColor();
     m = MODE_B;
   case '6':
   case '7':
   case '8':
   case '9':
   case '0':
   case '1':
   case '2':
   case '3':
   case '4':
   case '5':
     m &= 0x7f;
     last_n = n;
     if (n >= (26 - (s > '5'))) n = n % 100;
     n = n * 10 + (s - '0');
   case 8:
     n = last_n;
     last_n = n / 10;
   case 10:
   case ' ':
   case ';':
   s = 10;

void setColor() {
 const char *c = NULL;
 switch(m) {
 case MODE_R-128:  
   analogWrite(r_pin, n);
 case MODE_G-128:
   analogWrite(g_pin, n);
 case MODE_B-128:
   analogWrite(b_pin, n);
 if (c) {
   Serial.write("\nSetting ");
   Serial.write(" to 0x");
   Serial.println(n, HEX);
 last_n = 0;
 n = 0;


Now that I've posted, I can tell you that Sparkfun's part page is http://www.sparkfun.com/commerce/product_info.php?products_id=8718.

Also, when I do this 'for real' I'll probably use a NSS40601 (Vce(sat) = .2V) or one of the newer FETs with low Rdson, since the ULN drops about 1.2v, so it's eating up to 1.2W, almost half what the LED uses!

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131