int redPin = 9;   //D0
int greenPin = 10;  //D1
int bluePin = 11;   //D2

int r = 0;
int g = 0;
int b = 0;

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void loop() {
  // Flash();
  // Strobe();
  Fade();
  // Smooth();
}

void red() {
  setColor(255, 0, 0);  // red
}
void red2() {
  setColor(255, 99, 71);  // orange
}
void red3() {
  setColor(255, 127, 80);  // coral
}
void red4() {
  setColor(80, 0, 80);  // pink
}
void red5() {
  setColor(255, 255, 0);  // yellow
}

void green() {
  setColor(0, 255, 0);  // green
}
void green2() {
  setColor(50, 255, 50);  // lime green
}
void green3() {
  setColor(135, 206, 250);  // sky blue
}
void green4() {
  setColor(0, 255, 255);  // aqua
}
void green5() {
  setColor(30, 144, 255);  // dodger blue
}

void blue() {
  setColor(0, 0, 255);  // blue
}
void blue2() {
  setColor(65, 105, 255);  // royel blue
}
void blue3() {
  setColor(0, 0, 205);  // medium blue
}
void blue4() {
  setColor(106, 90, 205);  // slate blue
}
void blue5() {
  setColor(138, 43, 226);  // purple
}

void white() {
  setColor(255, 255, 255);  //White
}

void LedOn() {
  white();
}

void LedOff() {
  digitalWrite(redPin, LOW);
  digitalWrite(greenPin, LOW);
  digitalWrite(bluePin, LOW);  // All off
}

void Flash() {
  digitalWrite(redPin, HIGH);
  digitalWrite(greenPin, LOW);
  digitalWrite(bluePin, LOW);  // red
  delay(500);
  digitalWrite(redPin, LOW);
  digitalWrite(greenPin, HIGH);
  digitalWrite(bluePin, LOW);  // green
  delay(500);
  digitalWrite(redPin, LOW);
  digitalWrite(greenPin, LOW);
  digitalWrite(bluePin, HIGH);  // blue
  delay(500);
  digitalWrite(redPin, HIGH);
  digitalWrite(greenPin, HIGH);
  digitalWrite(bluePin, LOW);  // yellow
  delay(500);
  digitalWrite(redPin, HIGH);
  digitalWrite(greenPin, LOW);
  digitalWrite(bluePin, HIGH);  // purple
  delay(500);
  digitalWrite(redPin, LOW);
  digitalWrite(greenPin, HIGH);
  digitalWrite(bluePin, HIGH);  // aqua
  delay(500);
  digitalWrite(redPin, HIGH);
  digitalWrite(greenPin, HIGH);
  digitalWrite(bluePin, HIGH);  // White
  delay(500);
  // LedOff();
  // delay(1000);
}

void Strobe() {
  red();
  LedOff();
  delay(500);
  green();
  LedOff();
  delay(500);
  blue();
  LedOff();
  delay(500);
  red5();
  LedOff();
  delay(500);
  red4();
  LedOff();
  delay(500);
  green4();
  LedOff();
  delay(500);
  white();
  LedOff();
  delay(500);
}

void Smooth() {
  setColorSmooth(255, 0, 0);      // red
  setColorSmooth(255, 99, 71);    // orange
  setColorSmooth(255, 127, 80);   // coral
  setColorSmooth(80, 0, 80);      // pink
  setColorSmooth(255, 255, 0);    // yellow
  setColorSmooth(0, 255, 0);      // green
  setColorSmooth(50, 255, 50);    // lime green
  setColorSmooth(135, 206, 250);  // sky blue
  setColorSmooth(0, 255, 255);    // aqua
  setColorSmooth(30, 144, 255);   // dodger blue
  setColorSmooth(0, 0, 255);      // blue
  setColorSmooth(65, 105, 255);   // royel blue
  setColorSmooth(0, 0, 205);      // medium blue
  setColorSmooth(106, 90, 205);   // slate blue
  setColorSmooth(138, 43, 226);   // purple
}

void Fade() {
  red();   // red
  red2();  // orange
  red3();  // coral
  red4();  // pink
  red5();  // yellow

  green();   // green
  green2();  // lime green
  green3();  // sky blue
  green4();  // aqua
  green5();  // dodger blue

  blue();   // blue
  blue2();  // royel blue
  blue3();  // medium blue
  blue4();  // slate blue
  blue5();  // purple
}

void setColor(int red, int green, int blue) {
  while (r != red || g != green || b != blue) {
    if (r < red) r += 1;
    if (r > red) r -= 1;

    if (g < green) g += 1;
    if (g > green) g -= 1;

    if (b < blue) b += 1;
    if (b > blue) b -= 1;

    _setColor();
    delay(10);
  }
}

void setColorSmooth(int red, int green, int blue) {
  while (r != red || g != green || b != blue) {
    if (r < red) r += 1;
    if (r > red) r -= 1;

    if (g < green) g += 1;
    if (g > green) g -= 1;

    if (b < blue) b += 1;
    if (b > blue) b -= 1;

    _setColor();
    delay(100);
  }
}

void _setColor() {
  analogWrite(redPin, r);
  analogWrite(greenPin, g);
  analogWrite(bluePin, b);
}