# Homework week 2 – Udit Mahajan

I took help from the SIK Guide and made this.  An RGB led that changes colors. It goes from primary to secondary and then displays the entire spectrum.

I did put it inside a box!

Code:

int red_pin = 9;
int green_pin = 10;
int blue_pin = 11;

void setup()
{

pinMode(red_pin, OUTPUT);
pinMode(green_pin, OUTPUT);
pinMode(blue_pin, OUTPUT);
}

void loop()
{
digitalWrite(red_pin, LOW);
digitalWrite(green_pin, LOW);
digitalWrite(blue_pin, LOW);

delay(1000);

digitalWrite(red_pin, HIGH);
digitalWrite(green_pin, LOW);
digitalWrite(blue_pin, LOW);

delay(1000);

digitalWrite(red_pin, LOW);
digitalWrite(green_pin, HIGH);
digitalWrite(blue_pin, LOW);

delay(1000);

digitalWrite(red_pin, LOW);
digitalWrite(green_pin, LOW);
digitalWrite(blue_pin, HIGH);

delay(1000);

digitalWrite(red_pin, HIGH);
digitalWrite(green_pin, HIGH);
digitalWrite(blue_pin, LOW);

delay(1000);

digitalWrite(red_pin, LOW);
digitalWrite(green_pin, HIGH);
digitalWrite(blue_pin, HIGH);

delay(1000);

digitalWrite(red_pin, HIGH);
digitalWrite(green_pin, LOW);
digitalWrite(blue_pin, HIGH);

delay(1000);

digitalWrite(red_pin, HIGH);
digitalWrite(green_pin, HIGH);
digitalWrite(blue_pin, HIGH);

delay(1000);

for (int x = 0; x <= 767; x++)
{
rgb(x);
delay(10);
}
}

void rgb(int color)
{
int red, green, blue;

if (color <= 255)
{
red = 255 – color;
green = color;
blue = 0;
}
else if (color <= 511)
{
red = 0;
green = 255 – (color – 256);
blue = (color – 256);
}
else
{
red = (color – 512);
green = 0;
blue = 255 – (color – 512);
}

analogWrite(red_pin, red);
analogWrite(blue_pin, blue);
analogWrite(green_pin, green);
}