Working with RGB LED's, I wanted a simple way to phase from one predefined color to another. At first, I thought of adding "target" RGB values to each pin. It would then be a simple matter of comparing the current RGB values to the target ones, and either incrementing or decrementing as needed.
As RGB colors may come in useful for other C++ projects, I decided to shift my tack and write into their own class. The member variables are pretty simple; it has:
- R, G, and B (all bytes.)
- An ID (also a byte.) This correlates to a list of 9 possible colors. (The current color load out is very purple-heavy to match my wife's dress.)
- Compliment (also of type byte.) This allows users to choose the compliment of a passed color ID.
All of these are broadcast publicly with the exception of compliment.
There are only two private members:
- setColors: this sets the RGB value to one the 9 "preset" colors.
- setCompliment: this sets the color to the color-wheel compliment of the passed color ID.
RGBColor includes three constructors,
- The standard constructor, which will randomly choose from one of 9 established colors.
- One that takes a byte from 1-9, setting the instantiated color to the chosen color.
- One that takes a byte and a bool, setting the instantiated color to the color compliment of the chosen color.
- One that will set a color to its color-wheel compliment (useful for having two complimentary RGB LED's.)
- One that takes three bytes and manually sets the R, G, and B values to this. If a color is manually set, ID and Compliment are both set to 255.
Will likely add more colors as time goes on and try to find a logical way to mathematically pass the compliment.
Code follows.
RGBColor.h
#ifndef RGBColor_h
#define RGBColor_h
#include <Arduino.h>
#include <WProgram.h>
/*
RGBColor.h - Library for standard and RGB LED's
Beau Bureau, 2014
Released into the public domain.
*/
class RGBColor {
private:
// sets the colors red, green, and blue, and the RGB pins. Red is the power pin for standard LED's.
byte red, green, blue, id, compliment;
void setColors(byte);
void setCompliment();
public:
RGBColor();
RGBColor(byte);
RGBColor(byte, bool);
RGBColor(byte,byte,byte);
// Standard static constructor
byte Red(){return red;}
byte Blue() {return blue;}
byte Green() {return green;}
byte ID(){return id;}
};
#endif
#include<RGBColor.h>
#include <Arduino.h>
#include <WProgram.h>
RGBColor::RGBColor() {
byte input=random(1,9);
setColors(input);
}
RGBColor::RGBColor(byte input) {
if (input>0&&input<10) {}
else input=random(1,9);
setColors(input);
}
RGBColor::RGBColor(byte input, bool comp) {
setColors(input);
setCompliment();
}
RGBColor::RGBColor(byte r, byte g, byte b) {
//Initiates a standard LED. the rPin is power.
red=r;
green=g;
blue=b;
id=255;
compliment=255;
}
void RGBColor::setCompliment() {
Serial.println("compliment");
setColors(compliment);
}
void RGBColor::setColors(byte input) {
switch (input)
{
// red
case 1:
red=255;
green=0;
blue=0;
id=1;
compliment=2;
Serial.println("red");
break;
//green
case 2:
red=0;
green=255;
blue=0;
id=2;
compliment=1;
Serial.println("green");
break;
// blue
case 3:
red=0;
green=0;
blue=255;
id=3;
compliment=4;
Serial.println("blue");
break;
// orange
case 4:
red=255;
green=102;
blue=0;
id=4;
compliment=3;
Serial.println("orange");
break;
// crimson
case 5:
red=255;
green=0;
blue=71;
id=5;
compliment=6;
Serial.println("crimson");
break;
// lemon
case 6:
red=255;
green=255;
blue=102;
id=6;
compliment=7;
Serial.println("lemon");
break;
// purple
case 7:
red=102;
green=0;
blue=204;
id=7;
compliment=6;
Serial.println("purple");
break;
// Different purple
case 8:
red=204;
green=0;
blue=102;
id=8;
compliment=6;
Serial.println("purple2");
break;
// Yet another purple
case 9:
red=215;
green=30;
blue=172;
id=9;
compliment=4;
Serial.println("purple3");
break;
}
}
No comments:
Post a Comment