Andrew LeeClaire XuMarc ShenJonathan ShengAndy WangSarah HanNewton HuynhMichael Lee
Published

Ray of Light

Want to brighten up your day? Whether preparing for a party or setting a relaxing ambience, Ray of Light™ will de-LIGHT you every time.

IntermediateFull instructions provided628
Ray of Light

Things used in this project

Story

Read more

Schematics

Schematic

Code

Ray of Light!

C/C++
/**
 * Rename our pins from numbers to more readable variables
 * 23 corresponds to PD0
 * 24 corresponds to PD1
 * 25 corresponds to PD2
 * Each pin is PWM capable
 */
#define RED 23
#define GREEN 24
#define BLUE 25

// Globally define our delay time to be 20ms
#define DELAY_TIME 20

// Initialize the values to be written to RED, GREEN, and BLUE
float redVal = 255.0, greenVal = 0.0, blueVal = 0.0;

// Initialize the colors to be faded through by the program
// This was provided in the assignment pdf
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
  {256, 255, 0, 0}, // Red
};

/**
 * Runs once upon upload. Sets our LED pins to be OUTPUT
 * 
 * @return void
 */
void setup() {
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
}

/**
 * Runs continuously after setup(). Iterates through colors and calls fadeColors() to handle switching.
 * 
 * @return void
 */
void loop() {
  // Iterates through each consecutive pair of colors in GRADIENT_PALETTE and handles fading between them
  for(int i = 0; i < 8; i+=1) {
    fadeColors(GRADIENT_PALETTE[i], GRADIENT_PALETTE[i+1], DELAY_TIME);
    delay(DELAY_TIME);
  }
}

/**
 * Handles fading between two colors
 * 
 * @param  start[]    An array storing the RGB color code of the initial color
 * @param  finish[]   An array storing the RGB color code of the desired end color
 * @param  delayTime  An integer storing the delay (in ms) between each increment of the RGB values.
 *                    Longer delays will slow down the fade effect
 * @return void
 */
void fadeColors(int start[], int finish[], int delayTime) {
  // Computes the number of incr/decrements we want between the initial and end colors
  float change = finish[0] - start[0];
  
  // Computes the step size required to reach the end color from the initial color in the desired number of iterations
  // for each of the three colors
  float rDiff = (finish[1] - start[1])/change;
  float gDiff = (finish[2] - start[2])/change;
  float bDiff = (finish[3] - start[3])/change;
  
  for(int j = 0; j < change; j+=1) {
    // Increments the RGB values by the computed step size
    redVal += rDiff;
    greenVal += gDiff;
    blueVal += bDiff;

    // Writes each incr/decremented RGB value to its corresponding pin
    // We subtract the value from 255 since with analogWrite(), zero voltage gives us full color
    // while full voltage gives us no color
    analogWrite(RED, 255 - redVal);
    analogWrite(GREEN, 255 - greenVal);
    analogWrite(BLUE, 255 - blueVal);

    // Delay the next step by the specified time
    delay(delayTime);
  }
}

Credits

Andrew Lee
1 project • 0 followers
Contact
Claire Xu
1 project • 0 followers
Contact
Marc Shen
1 project • 0 followers
Contact
Jonathan Sheng
1 project • 0 followers
Contact
Andy Wang
1 project • 0 followers
Contact
Sarah Han
1 project • 0 followers
Contact
Newton Huynh
1 project • 0 followers
Contact
Michael Lee
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.