Tuesday, September 11, 2012

Erin | Ch3 Pr8 rgb mood lamp

// Project 8 - Mood Lamp with comments
float RGB1[3];  //assign rgb1[3] floating point (decimal option)
float RGB2[3];
float INC[3];

int red, green, blue;

int RedPin = 11;
int GreenPin = 10;
int BluePin = 9;

void setup()
{
randomSeed(analogRead(0)); //pseudorandom numbers
RGB1[0] = 0;
RGB1[1] = 0;
RGB1[2] = 0;

RGB2[0] = random(256);   //between 0 and 255
RGB2[1] = random(256);
RGB2[2] = random(256);
}

void loop() 
{
      
  randomSeed(analogRead(0));       
        
        for (int x=0; x<3; x++) {            
                 // first loop sets the increment values (steps) for RGB channels
                //by subtracting the two values and divide by 256
                INC[x] = (RGB1[x] - RGB2[x]) / 256; }    
        for (int x=0; x<256; x++) {        //second loop sets the RGB values for the RGB1 array
                red = int(RGB1[0]);        //writes them to pins 9,10,11 and inputs the INC values
                green = int(RGB1[1]);      //repeats 256 times for random colors
                blue = int(RGB1[2]);

                analogWrite (RedPin, red);
                analogWrite (GreenPin, green);
                analogWrite (BluePin, blue);
                delay(100);                      //can change progression speed here

                RGB1[0] -= INC[0];
                RGB1[1] -= INC[1];
                RGB1[2] -= INC[2];

for (int x=0; x<3; x++) {          
//make third set of random numbers between 0 and 556
//and then subtracting 300 to be sure not only getting pastel shades
        RGB2[x] = random(556)-300;
RGB2[x] = constrain(RGB2[x], 0, 255); //make sure number isn't negative, 
// if the number is lower than a (or 0 in this case) it sets it at a, 
//if higher than b it sets to b
delay(1000);
}
}



No comments:

Post a Comment