At Last I See the Light

See alll the colors in light through a RGB LED that cycles through colors with a tilt switch. It's brilliant stuff!

BeginnerShowcase (no instructions)1 hour442
At Last I See the Light

Things used in this project

Hardware components

EK-TM4C123GXL TM4C Tiva LaunchPad
Texas Instruments EK-TM4C123GXL TM4C Tiva LaunchPad
Powerful microprocessor used to compile our code and coordinate the use of the tilt switch with the RGB LED.
×1
Breadboard (generic)
Breadboard (generic)
×1
Tilt Switch, SPST
Tilt Switch, SPST
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Energia
Texas Instruments Energia

Story

Read more

Schematics

RGB Fading Light

How to wire your board so the RGB LED responds to the tilt switch.

Code

RGB Fading Light

C/C++
The magic code that allows the colors to change in response to the tilt switch.
#define Cols 8
# define ENABLE 29

/* Defining RGB Colors to fade through */
int colours[Cols][3] = {
  {255,   0,   0}, // Red
  {171,  85,   0}, // Orange
  {171, 171,   0}, // Yellow
  {  0, 255,   0}, // Green
  {  0, 171,  85}, // Aqua
  {  0,   0, 255}, // Blue
  { 85,   0, 171}, // Purple
  {171,   0,  85} // Pink
};

int LED[3] = {RED_LED, GREEN_LED, BLUE_LED};
int steps = 50;
int current_col = 1;
int math;
int bright;
int interval;

/* Function to select the next color. Loops around from pink to red */
int NEXT(int i){
  return (i+1) % Cols;
}


/* Function to interpolate between colors */
int Linear(int col_1, int step_N, int led){
  int math;
  int col_2;
  col_2=NEXT(col_1);
  math=(colours[col_1][led]*(steps-step_N)+colours[col_2][led]*step_N)/steps;
  return math;
}


void setup()  { 

  /* Setting up the LED and the initial conditions */
  for(int i=0; i<3;i++){
    pinMode(LED[i],OUTPUT);
    analogWrite(LED[i],colours[0][i]);
  }

  /* Setting tilt switch pin as input */
  pinMode(ENABLE, INPUT);
} 


  
void loop()  { 
  /* Looping from color to color (i), from interpolation value to interpolation value (j),
   and from LED to LED (k) */
  for (int i=0; i<Cols; i++){
    for (int j=0; j<steps; j++){
       for (int k=0; k<3; k++){

        /* Calculating and applying luminance values */
             bright=Linear(i,j,k);
             analogWrite(LED[k], bright);
           }
           delay(5);

           /* Enters infinite loop while the tilt switch is enabled */
           interval=digitalRead(ENABLE);
           while(interval){
            interval=digitalRead(ENABLE);
           }
    }
  }
}

Credits

Christine Lee
1 project • 1 follower
Tadeo Casiraghi
1 project • 1 follower
Nikhaz Omar
1 project • 0 followers
Rubén Marroquin
1 project • 1 follower
Mark Saving
0 projects • 0 followers
Scarlett
1 project • 0 followers
Alvaro Camacho Guadamuz
0 projects • 0 followers

Comments