Sunday, February 1, 2015

Using the RGBColor Class

Just created a bit of code to feed off my RGBColor class.  Main functions include:

  • shiftColors: takes an LED, an RGBColor, and an int.  Runs matchColors against both pins until both match.  The passed int represents the # of milliseconds to delay after each iteration of matchColors.
  • shiftCompliments: takes two LED's, an RGBColor, and an int.  Shifts two LED's, one to a color, and another to a color-wheel compliment of that color.  Runs matchColors against both pins until both match. The passed int represents the # of milliseconds to delay after each iteration of matchColors.
  • matchColors: tales an LED and an RGBColor.  Checks each of the three colors on the LED against the values of the color; increments or decrements by 1 as needed.  Passes true if all values match, otherwise passes false.
  • fadeLed: takes an LED and an int.  Runs matchColors against both pins until both match. The passed int represents the # of milliseconds to delay after each iteration of matchColors.
  • fireLed: takes an LED.  AnalogWrites to the RGB pins of the LED.


Code follows

#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() 
{
  RGBColor * color=new RGBColor();
  shiftCompliments(*c1,*c2, *color,30);
  Serial.println("Reached!");
  delay(5000);
  
}


void ShiftRandomColor()

  RGBColor * color=new RGBColor();
  shiftCompliments(*c1,*c2, *color,30);
}

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());
  }
}

No comments:

Post a Comment