Go Down

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

rmd6502

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 {
 MODE_NONE = 128, MODE_R, MODE_G, MODE_B };

void setColor();

void setup() {
 Serial.begin(19200);
 analogWrite(r_pin, 0);
 analogWrite(g_pin, 0);
 analogWrite(b_pin, 0);
 Serial.write("Ready\n");
}

void loop() {  
 if (Serial.available()) {
   byte s = Serial.read();
   
   switch(s) {
     case 'R':
   case 'r':
     if (m) setColor();
     m = MODE_R;
     break;
   case 'G':
   case 'g':
     if (m) setColor();
     m = MODE_G;
     break;
   case 'B':
   case 'b':
     if (m) setColor();
     m = MODE_B;
     break;
   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');
     break;
   case 8:
     n = last_n;
     last_n = n / 10;
     break;
   case 10:
   case ' ':
   case ';':
   s = 10;
     setColor();
     break;
   default:
     break;
   }
   Serial.write(s);
 }
}

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

rmd6502

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!

Arduino
via Egeo 16
Torino, 10131
Italy