RGB Eyes

This soothing fading night light will help you relax and fall asleep in an instant!

RGB Eyes

Things used in this project

Hardware components

EK-TM4C123GXL TM4C Tiva LaunchPad
Texas Instruments EK-TM4C123GXL TM4C Tiva LaunchPad
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×2
Tilt Switch, SPST
Tilt Switch, SPST
×1
Resistor 10k ohm
Resistor 10k ohm
×1

Software apps and online services

Energia
Texas Instruments Energia

Story

Read more

Schematics

Fading RGB Circuit Diagram

Code

fade_lights.ino

C/C++
This code controls the LED colors and turns off the light when the tilt switch is triggered.
// Defining the led pins variables
#define RED RED_LED // red led
#define GREEN GREEN_LED // green led
#define BLUE BLUE_LED // blue led
#define DELAYTIME 5 // delay time

// Introducing global variables to keep track of the RGB values and starting value RGB value
int redVal;
int greenVal;
int blueVal;
int rgbVal = 0;
/* each rgbVal corresponds to a certain color red = 0, orange = 1, yellow = 2, green = 3
aqua = 4, blue = 5, purple = 6, pink = 7. Each rgbVal keeps track of the fade to the next
color through if statements in void loop(), such that red goes to orange, orange goes to
yellow, and so on until it gets to pink and goes back to red*/

const int switch_pin = 6; // the pin the tilt swtich is connected to 

// Max LED value
int max_LED = 255;

void setup() {
  // put your setup code here, to run once:
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  pinMode(switch_pin, INPUT_PULLUP); 
  // reads LOW when the tilt switch is level and HIGH when the it is tilted

  // Test Red
  digitalWrite(RED, HIGH);
  digitalWrite(GREEN, LOW);
  digitalWrite(BLUE, LOW);
  delay(500);
  
  // Test Green
  digitalWrite(RED, LOW);
  digitalWrite(GREEN, HIGH);
  digitalWrite(BLUE, LOW);
  delay(500);
  
  // Test Blue
  digitalWrite(RED, LOW);
  digitalWrite(GREEN, LOW);
  digitalWrite(BLUE, HIGH);
  delay(500);
}

void loop() {
  // loop to run the fading lights continuously
  int tilt_state = digitalRead(switch_pin); // checking the state of the tilt switch
  if (tilt_state == HIGH) { // If the switch is tilted, it turns off all of the lights
    analogWrite(RED, LOW);
    analogWrite(GREEN, LOW);
    analogWrite(BLUE, LOW);
  }
  else { // Fades the lights if the switch is level
    if(rgbVal == 0) { // transition from red to orange
      redVal = 256;
      greenVal = 0;
      blueVal = 0;
      for(int i = 0 ; i < 86 ; i += 1) {
        redVal -= 1;
        greenVal += 1;
        analogWrite( RED, redVal ); // set the new red value
        analogWrite( GREEN, greenVal ); // set the new green value
        analogWrite( BLUE, blueVal ); // set the new blue value
        delay(DELAYTIME); // delays the color change
      }
      rgbVal += 1;
      delay(DELAYTIME);
    }
    
    else if(rgbVal == 1) { // transition from orange to yellow
      redVal = 171;
      greenVal = 85;
      blueVal = 0;
      for(int i = 0 ; i < 87 ; i += 1) {
        greenVal += 1;
        analogWrite( RED, redVal ); // set the new red value
        analogWrite( GREEN, greenVal ); // set the new green value
        analogWrite( BLUE, blueVal ); // set the new blue value
        delay(DELAYTIME); // delays the color change
      }
      rgbVal += 1;
      delay(DELAYTIME);
    }

    else if(rgbVal == 2) { // transition from yellow to green
      redVal = 170;
      greenVal = 170;
      blueVal = 0;
      for(int i = 0 ; i < 86 ; i += 1) {
        redVal -= 2;
        greenVal += 1;
        analogWrite( RED, redVal ); // set the new red value
        analogWrite( GREEN, greenVal ); // set the new green value
        analogWrite( BLUE, blueVal ); // set the new blue value
        delay(DELAYTIME); // delays the color change
      }
      rgbVal += 1;
      delay(DELAYTIME);
    }

    else if(rgbVal == 3) { // transition from green to aqua
      redVal = 0;
      greenVal = 256;
      blueVal = 0;
      for(int i = 0 ; i < 86 ; i += 1) {
        greenVal -= 1;
        blueVal += 1;
        analogWrite( RED, redVal ); // set the new red value
        analogWrite( GREEN, greenVal ); // set the new green value
        analogWrite( BLUE, blueVal ); // set the new blue value
        delay(DELAYTIME); // delays the color change
      }
      rgbVal += 1;
      delay(DELAYTIME);
    }

    else if(rgbVal == 4) { // transition from aqua to blue
      redVal = 0;
      greenVal = 170;
      blueVal = 85;
      for(int i = 0 ; i < 86 ; i += 1) {
        greenVal -= 2;
        blueVal += 2;
        analogWrite( RED, redVal ); // set the new red value
        analogWrite( GREEN, greenVal ); // set the new green value
        analogWrite( BLUE, blueVal ); // set the new blue value
        delay(DELAYTIME); // delays the color change
      }
      rgbVal += 1;
      delay(DELAYTIME);
    }

    else if(rgbVal == 5) { // transition from blue to purple
      redVal = 0;
      greenVal = 0;
      blueVal = 256;
      for(int i = 0 ; i < 86 ; i += 1) {
        redVal += 1;
        blueVal -= 1;
        analogWrite( RED, redVal ); // set the new red value
        analogWrite( GREEN, greenVal ); // set the new green value
        analogWrite( BLUE, blueVal ); // set the new blue value
        delay(DELAYTIME); // delays the color change
      }
      rgbVal += 1;
      delay(DELAYTIME);
    }

    else if(rgbVal == 6) { // transition purple to pink
      redVal = 85;
      greenVal = 0;
      blueVal = 171;
      for(int i = 0 ; i < 87 ; i += 1) {
        redVal += 1;
        blueVal -= 1;
        analogWrite( RED, redVal ); // set the new red value
        analogWrite( GREEN, greenVal ); // set the new green value
        analogWrite( BLUE, blueVal ); // set the new blue value
        delay(DELAYTIME); // delays the color change
      }
      rgbVal += 1;
      delay(DELAYTIME);
    }

    else if(rgbVal == 7) { // transition pink to purple
      redVal = 170;
      greenVal = 0;
      blueVal = 85;
      for(int i = 0 ; i < 86 ; i += 1) {
        redVal += 1;
        blueVal -= 1;
        analogWrite( RED, redVal ); // set the new red value
        analogWrite( GREEN, greenVal ); // set the new green value
        analogWrite( BLUE, blueVal ); // set the new blue value
        delay(DELAYTIME); // delays the color change
      }
      rgbVal = 0;
    }
  }
 }

Credits

Reece Eberhardt

Reece Eberhardt

1 project • 0 followers
Mateo Martinez

Mateo Martinez

2 projects • 0 followers
Alan Guo

Alan Guo

1 project • 0 followers
Henry Qin

Henry Qin

1 project • 0 followers
Doug Welsch

Doug Welsch

1 project • 0 followers
Asha Malani

Asha Malani

1 project • 0 followers
David Padierna

David Padierna

2 projects • 0 followers
Brandon Cua

Brandon Cua

1 project • 0 followers

Comments