Maggie BaoSritha Cheemerla
Published

Bright Light Night Light

A vibey nightlight with all the colors you could ever want that stops at whatever color you pick!

BeginnerFull instructions provided71
Bright Light Night Light

Things used in this project

Hardware components

EK-TM4C123GXL TM4C Tiva LaunchPad
Texas Instruments EK-TM4C123GXL TM4C Tiva LaunchPad
×1
RGB Diffused Common Cathode
RGB Diffused Common Cathode
×1
Tilt Switch, SPST
Tilt Switch, SPST
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Energia
Texas Instruments Energia

Story

Read more

Schematics

Schematics

Sample Schematic for the circuit. Components: RBG LED, tilt switch, and a 100 KΩ resistor connected in series.

Code

Code

C/C++
/**
 * Project: Bright Light Night Light
 * 
 * This code uses Pulse Width Modulation to fade an RGB LED through 8 colors.
 * It also adds a tilt switch in order to stop the fading on the color that the cycle is
 * on when the tilt switch is tilted. The LED will halt at this color until the circuit board
 * is untilted, at which point it will resume the fade cycle.
 * 
 * Use #define to rename our pins from numbers to readable variables
 */
#define RED 36    //PC5
#define GREEN 35  //PC6
#define BLUE 34   //PC7
#define TILT 11   //PA2

#define delayTime 30 //delay between color changes (30ms)


// Initialize the global variables for RGB values to be written to RED, GREEN, and BLUE
// starting the cycle with red
float redVal = 255.0;
float greenVal = 0.0;
float blueVal = 0.0;

// Initialize a global variable to keep track of our tilt switch's state 
// (i.e. tilted or not tilted)
int tiltSwitch;

// Initialize the RGB values for 8 colors to loop through
int GRADIENT_PALETTE[9][4] = {
  {0, 255, 0, 0},    // Red
  {32, 171, 85, 0},  // Orange
  {64, 171, 171, 0}, // Yellow
  {96, 0, 255, 0},   // Green
  {128, 0, 171, 85}, // Aqua
  {160, 0, 0, 255},  // Blue
  {192, 85, 0, 171}, // Purple
  {224, 171, 0, 85}, // Pink
  {255, 255, 0, 0},  // and back to Red
};

/**
 * Sets the tilt switch to be an input and RGB pins of the LED to be outputs
 */
void setup() {
  pinMode(TILT, INPUT);
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
}

/**
 * Fades through the colors while checking if the board is tilted, and performs
 * the appropriate actions if it is.
 */
void loop() {
   
    for(int i = 0; i < 8; i+=1) {
      
      //fades between each color defined in GRADIENT_PALETTE (i.e. red to orange, orange to yellow, etc.)
      fadeRGB(GRADIENT_PALETTE[i], GRADIENT_PALETTE[i+1]);

      //reads the state of the tilt switch (tilted or not tilted)
      tiltSwitch = digitalRead(TILT);

      // if the tiltswitch is tilted, executes code to halt the fade and hold the color
      // that the fade is halted at
      if (tiltSwitch == LOW) {
        
         //executes tilt code
         tilt(tiltSwitch, i);
      }
      
    }
 }
 
/*
 * Keeps the LED at the color it is halted at when tilted, for as long as
 * the circuit board is tilted
 */
 void tilt(int state, int color) {
  color -= 3;
  
  //adjusts the input color to be a valid index of GRADIENT_PALETTE
  if (color < 0) {
    color += 8;
  }

  //while the circuit board is tilted, sets LED to the halted color
  while (state == LOW) {
    
    //outputs the halted color to the LED
    analogWrite(RED, GRADIENT_PALETTE[color][1]);
    analogWrite(GREEN, GRADIENT_PALETTE[color][2]);
    analogWrite(BLUE, GRADIENT_PALETTE[color][3]);

    //checks whether the board is still tilted or not
    state = digitalRead(TILT);
  }

  return;
 }
  

/**
 * Fades between two colors
 */
void fadeRGB(int firstColor[], int secondColor[]) {
  //uses the difference between the first element of each color array
  //as the number of steps to take in order to fade seemlessly between two colors
  float steps = secondColor[0] - firstColor[0];
  
  //increments each value in RGB to fade between the input colors
  //outputs these values to the LED
  for(int i = 0; i < steps; i++) {
    
    //increments the RGB values
    redVal += (secondColor[1] - firstColor[1])/steps;
    greenVal += (secondColor[2] - firstColor[2])/steps;
    blueVal += (secondColor[3] - firstColor[3])/steps;

    //outputs values to LED
    analogWrite(RED, 255 - redVal);
    analogWrite(GREEN, 255 - greenVal);
    analogWrite(BLUE, 255 - blueVal);

    //delays between each step by predetermined amount of time
    delay(delayTime);
  }
}

Credits

Maggie Bao

Maggie Bao

1 project • 0 followers
Sritha Cheemerla

Sritha Cheemerla

1 project • 0 followers

Comments