Pages: [1]   Go Down
Author Topic: How to get RGB's to change from Green to Red as a number goes from 1 to 1000  (Read 483 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I feel like the title is terribly confusing and I apologize for that. I couldn't think of a better way to ask.

Basically I'm making an ambient light "thing" (not sure what shape it will take yet) that reads the amount of emails we have in our inbox at work and translates that number into a color on the LED's. I'm hoping to get the LED's to change fluidly through the spectrum from Green to Red as the email count goes from 1-1000.

I can currently query the number from our email client, put it to the Arduino through Gobetwino and, using the sample code in Gobetwino, and have the Arduino blink the number it reads.

I have a strand of the 12mm RGB pixels with the WS2801 chip, https://www.adafruit.com/products/322, and which I was planning on using unless someone has a better suggestion. I thought about getting a Piranha LED instead but being that I already have these pixels I figured I would start with them.

My main question would be what sort of code should I look into that will translate a number into a specific color on these pixels or, if somewhere on the interwebs there is a better suited RGB for this task, what is needed when using that?

Any help would be greatly appreciated.
Logged

UK
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ive been using Pyranha RGBs in a recent project. They have a common anode which you wire to about 3V ish if i remember right. (I think I used a 60ohm resistor to 5V). Then you wire the the R, G and B pins to the PWM pins on your arduino. When tied LOW the current flows from the anode into the arduino and you get light. So you then have to use the analogWrite command to balance (effectivly fade) the different colours. I used this code to control the colour with 3 pots, then a push switch to tell me what the values I was using were:

Code:
const int Switch = 13;
const int ROne = 11;
const int GOne = 10;
const int BOne = 9;
const int RIn = A4;
const int GIn = A2;
const int BIn = A0;

int RPotValue = 0;
int RLEDValue = 0;
int GPotValue = 0;
int GLEDValue = 0;
int BPotValue = 0;
int BLEDValue = 0;
int SwitchState = LOW;


void setup(){
  Serial.begin(9600);
  pinMode(Switch, INPUT);
  pinMode(ROne, OUTPUT);
  pinMode(GOne, OUTPUT);
  pinMode(BOne, OUTPUT);
  pinMode(RIn, INPUT);
  pinMode(GIn, INPUT);
  pinMode(BIn, INPUT);
}

void loop(){
   
  RPotValue = analogRead(RIn);
  RLEDValue = map(RPotValue, 0, 1023, 0, 255);
  analogWrite(ROne, RLEDValue);
 
  GPotValue = analogRead(GIn);
  GLEDValue = map(GPotValue, 0, 1023, 0, 255);
  analogWrite(GOne, GLEDValue);
 
  BPotValue = analogRead(BIn);
  BLEDValue = map(BPotValue, 0, 1023, 0, 255);
  analogWrite(BOne, BLEDValue);
 
  SwitchState = digitalRead(Switch);
 
  if(SwitchState == HIGH) {
    Serial.print (" R");
    Serial.print (RLEDValue);
    Serial.print (" G");
    Serial.print (GLEDValue);
    Serial.print (" B");
    Serial.print (BLEDValue);
    Serial.print ("    ");
    delay (2000);
  }
   
   else {
 
     delay(50);
     
  }
}

Because the output pins are cathodes, a signal of 0 for PWM is full on, and 255 is full off, reverse to normal.
So you need the map function to turn the number of emails you have into a number between 0 and 255, then you need the analogWrite function to feed that value to the LEDs, (So from Red to green would be Red 0 going to 255, and Green 255 going to 0. So at 500 emails you should have Red 127, Green 127. Does that make sense?

drop me an PM for clarity if you like.
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Fairly easy:

Code:
  unsigned char color;

  //obtain color somehow
  analogWrite(LED_G, color); //write color to green led
  analogWrite(LED_R, -color); //write 255-color to red led

That's all it takes.

Logged

Southern California
Offline Offline
God Member
*****
Karma: 5
Posts: 539
I like blinky lights
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

To expand on what dhenry wrote --

What you want to do is pretty easy. You want solid green (RGB 0, 255, 0) for value 1, and solid red (RGB 255, 0, 0) for value 1000.

So all you'd have to do is set

red = value / (1000  / 255.0);

and

green = -red;

For the WS2801 you'd just output the (red, green, 0) tuple 25 times so it gets to all the lights, or you could just output a new set every 5 minutes or so and have a visual history.

Good luck!

[edit: green/red wrong. Fixed]
« Last Edit: December 04, 2012, 04:24:42 pm by magagna » Logged

http://en.wiktionary.org/wiki/magagna <-- My last name.  Pretty apt.

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wow, thanks all!
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12487
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


for a complete rainbow you can check - http://arduino.cc/forum/index.php?topic=95019.15 -

it uses a different colorspace HSL which makes it easier to shift colors.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: