- Sparkle (fire random colors)
- Fade multiple Led's to a single color
- Fade multiple Led's to a to complimentary colors
While I could have written the code to handle an array of Led's, I'm only really going to use two for the main dress (run in series of 8 each for the panels of the dress and 2-4 for the hair,) so decided to keep it simple.
Code follows.
Shiftcolors.ino
#include <RGBColor.h>
#include <LED.h>
Led * c2;
Led * c1;
void setup()
{
c1=new Led(3,5,6);
c2=new Led(9,10,11);
Serial.begin(9600);
}
void loop()
{
do {
Serial.println("Flashing");
RGBFlash(*c1,*c2,100);
Serial.println("Fading");
RGBColor * color=new RGBColor();
shiftTogether(*c1, *c2, *color, random(10,50));
Serial.println("FadingComplete");
delay(5000);
RGBColor * color2=new RGBColor();
Serial.println("Fading compliments");
shiftCompliments(*c1,*c2, *color2,30);
Serial.println("Fading complete");
delay(5000);
}
while (true);
delay(5000);
}
void ShiftRandomColor()
{
RGBColor * color=new RGBColor();
shiftCompliments(*c1,*c2, *color,30);
}
void RGBFlash (Led &led1, Led &led2, int times)
{
int count=0;
do {
RGBColor * color=new RGBColor();
RGBColor * color2=new RGBColor();
led1.SetRGB(color->Red(),color->Green(),color->Blue());
led2.SetRGB(color2->Red(),color2->Green(),color2->Blue());
fireLed(led1);
fireLed(led2);
delay(random(30,100));
count++;
} while (count<times);
}
void shiftTogether (Led &led1, Led &led2, RGBColor color, byte pause)
{
bool halt=false;
do {
if (matchColors(led1,color)==true&&matchColors(led2,color)==true)
{
halt=true;
}
fireLed(led1);
fireLed(led2);
delay(pause);
} while(halt==false);
}
void shiftCompliments(Led &led1, Led &led2, RGBColor color, byte pause)
{
RGBColor * color2=new RGBColor(color.ID(),true);
bool halt=false;
do {
if (matchColors(led1,color)==true&&matchColors(led2,*color2)==true)
{
halt=true;
}
fireLed(led1);
fireLed(led2);
delay(pause);
} while(halt==false);
}
bool matchColors(Led &led,RGBColor &color)
{
bool r=0;
bool g=0;
bool b=0;
if (led.Blue()==color.Blue())
{
b=1;
}
else {
if (led.Blue()>color.Blue()) led.SetBlue(led.Blue()-1);
else led.SetBlue(led.Blue()+1);
}
if (led.Red()==color.Red())
{
r=1;
}
else {
if (led.Red()>color.Red()) led.SetRed(led.Red()-1);
else led.SetRed(led.Red()+1);
}
if (led.Green()==color.Green())
{
g=1;
}
else {
if (led.Green()>color.Green()) led.SetGreen(led.Green()-1);
else led.SetGreen(led.Green()+1);
}
if (r==1)
{
if (g==1) {
if (b==1) {
Serial.print(led.RPin());
Serial.print(" ");
Serial.print(led.GPin());
Serial.print(" ");
Serial.print(led.BPin());
Serial.print(" ");
Serial.print(color.Red());
Serial.print(",");
Serial.print(color.Green());
Serial.print(",");
Serial.print(color.Blue());
Serial.print("=");
Serial.print(led.Red());
Serial.print(",");
Serial.print(led.Green());
Serial.print(",");
Serial.println(led.Blue());
return true;
}
}
}
return false;
}
void shiftColors(Led &led, RGBColor color, int pause)
{
bool halt=0;
do {
// fade to black until colors match
if (matchColors(led,color)==true)
{
halt=true;
}
fireLed(led);
delay(pause);
} while(halt==false);
}
void fadeLed(Led led, int pause)
{
// Create a color of 0,0,0 (black)
RGBColor * color=new RGBColor(0,0,0);
bool halt=false;
do {
// fade to black until colors match
if (matchColors(led,*color)==true)
{
halt=true;
}
fireLed(led);
delay(pause);
} while(halt==false);
}
void fireLed(Led led)
{
if (led.IsRGB())
{
analogWrite(led.RPin(),led.Red());
analogWrite(led.GPin(),led.Green());
analogWrite(led.BPin(),led.Blue());
}
}
Led.h
#ifndef LED_h
#define LED_h
#include <Arduino.h>
#include <WProgram.h>
/*
LED.h - Library for standard and RGB LED's
Beau Bureau, 2014
Released into the public domain.
*/
class Led {
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, rPin, gPin, bPin;
public:
Led() {};
~Led() {};
// Standard static constructor
Led(byte);
// standard RGB constructor
Led(byte,byte,byte);
// Public members for red, green and blue.
byte Blue() {return blue;}
byte Green() {return green;}
byte Red() {return red;}
//Pins for RGB. RPin is also the power pin.
byte RPin() {return rPin;}
byte GPin() {return gPin;}
byte BPin() {return bPin;}
// Public member for power.
byte Power() {return rPin;}
// checks whether the bPin has been set to -1. If it is set to -1 in the constructor, it is not an RGB LED.
bool IsRGB() {return bPin!=255;}
// public methods to set RGB
void SetRed(byte);
void SetGreen(byte);
void SetBlue(byte);
void SetRGB(byte,byte,byte);
// public method to set a random color
void SetRandomColor();
};
#endif
Led.cpp
#include<LED.h>
#include <Arduino.h>
#include <WProgram.h>
Led::Led(byte p) {
//Initiates a standard LED. the rPin is power.
rPin=p;
// Either g or b can bee tested against to see if this is an RGB. They will be equal to -1 if a standard RGB.
gPin=-1;
bPin=-1;
red=0;
green=0;
blue=0;
}
Led::Led(byte r,byte g,byte b) {
// Initiates an RGB LED, sets color to white.
rPin=r;
gPin=g;
bPin=b;
red=255;
green=255;
blue=255;
}
void Led::SetRandomColor() {
// Randomize, choose a color of 0-6. 0=red, 1=green, 2=blue, 3=purple, 4=yellow, 5=orange
int color=random(0,5);
switch (color) {
case 0:
red=255;
green=0;
blue=0;
break;
case 1:
green=255;
red=0;
blue=0;
break;
case 2:
blue=255;
red=0;
blue=0;
break;
case 3:
red=255;
blue=255;
green=0;
break;
case 4:
red=255;
green=255;
blue=0;
break;
default:
red=255;
green=128;
blue=0;
break;
}
}
void Led::SetRGB(byte r, byte g, byte b) {
red=r;
green=g;
blue=b;
}
void Led::SetRed(byte val) {
red=val;
}
void Led::SetBlue(byte val) {
blue=val;
}
void Led::SetGreen(byte val) {
green=val;
}
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(){Serial.println("Destructor called");}
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
RGBColor.cpp
#include<RGBColor.h>
#include <Arduino.h>
#include <WProgram.h>
RGBColor::RGBColor() {
byte input=random(1,9);
setColors(input);
}
RGBColor::RGBColor(byte input) {
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() {
setColors(compliment);
}
void RGBColor::setColors(byte input) {
if (input>0&&input<12) {}
else input=random(1,9);
switch (input)
{
// red
case 1:
red=255;
green=0;
blue=0;
id=1;
compliment=2;
break;
//green
case 2:
red=0;
green=255;
blue=0;
id=2;
compliment=1;
break;
// blue
case 3:
red=0;
green=0;
blue=255;
id=3;
compliment=4;
break;
// orange
case 4:
red=255;
green=102;
blue=0;
id=4;
compliment=3;
break;
// crimson
case 5:
red=255;
green=0;
blue=71;
id=5;
compliment=6;
break;
// lemon
case 6:
red=255;
green=255;
blue=102;
id=6;
compliment=7;
break;
// purple
case 7:
red=102;
green=0;
blue=204;
id=7;
compliment=6;
break;
// Different purple
case 8:
red=204;
green=0;
blue=102;
id=8;
compliment=6;
break;
// Yet another purple
case 9:
red=215;
green=30;
blue=172;
id=9;
compliment=4;
break;
case 10:
red=0;
green=255;
blue=255;
id=10;
compliment=11;
break;
case 11:
red=255;
green=127;
blue=80;
id=11;
compliment=10;
break;
}
}
This comment has been removed by the author.
ReplyDeleteGreat Job!! I created each section in its own file using notepad++ but when I compile the ShiftColors.ino in Arduino IDE it complains that there is no "#include WProgram.h ". I don't see a section for that script? Also will this compile in Arduino IDE or must I use Microsoft Visual Studio or Atmel Studio? I do have Visual Studio 2015 community installed with the VisualMicro Add-on for Arduino. I also see "Public:" and "Private:" in your Led.h script section does that belong there?
ReplyDeleteI did as you said removed the WProgram.h wherever it could be found. Arduino.h was already included in each section. At first I got errors in both LED.cpp and RGBColor.cpp. I listed some errors at the end but after compiling again it was all good almost magical and I don't know why? Here was some of the errors which was basically the same thing over and over again for both CPP files: sketch\RGBColor.cpp.o:sketch/RGBColor.cpp:22: first defined here, then: libraries\Shiftcolors\RGBColor.cpp.o: In function `RGBColor::RGBColor(unsigned char, unsigned char, unsigned char)':
ReplyDelete