Simar Over the RAYnbow

Create a comforting beacon of hope in this trying time. Cause an RGB LED to cycle through all hues.

BeginnerWork in progress30 minutes417
Simar Over the RAYnbow

Things used in this project

Story

Read more

Schematics

Block Diagram

Schematic

Tilt Vid

Code

Rainbow Fade

Arduino
does the LED fading with linear steps to provided value
alternate version where fading pauses when tilt switch is off
int brightness_g = 0;    //green LED initial brightness
int brightness_r = 0;    //red LED initial brightness
int brightness_b = 0;    //blue LED initial brightness

//array defining each color's gradient palatte
int gradient[8][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

//helper function to fade to specific red, green, blue target brightnesses
void fade(int r_target, int g_target, int b_target)
{
  //set step values to reach target brightness from current brightness after 10 steps
  int step_g = (g_target - brightness_g)/10;
  int step_r = (r_target - brightness_r)/10;
  int step_b = (b_target - brightness_b)/10;

  for (int i = 0; i < 10; i++)
  {
    //halt fading if tilt switch is off
    while(!digitalRead(10)){}

    //increment each color brignteness by previously calculated step value
    brightness_g += step_g;
    brightness_r += step_r;
    brightness_b += step_b;

    //write new values to each LED
    analogWrite(GREEN_LED, brightness_g);
    analogWrite(RED_LED, brightness_r);
    analogWrite(BLUE_LED, brightness_b);

    delay(30);
  }
}

void setup()  { 
  // declare all RGB leds to be outputs:
  pinMode(GREEN_LED, OUTPUT);
  pinMode(RED_LED, OUTPUT);
  pinMode(BLUE_LED, OUTPUT);
  
  // declare pin 10 connected to tilt switch to be input
  pinMode(10, INPUT);
} 

void loop() 
{    
  // loop through each defined color in the predefined array and fade to each one in order
  for (int i = 0; i < 9; i ++)
  {
    fade(gradient[i][0], gradient[i][1], gradient[i][2]);
  }
}

Pointer-Based Color Arrays and Tilt Switch

Arduino
Color only transitions when the tilt switch is rotated.
#define COLOR_CHANNELS 3//number of channels per color
#define LEN_COLOR_VAL 8//number of bits per color channel
#define TILT_PIN 10//pin the tilt switch is on

#define NUM_COLORS 8//number of colors to cycle through

int speed = 100;
int timestep = 256/speed;

//define all the colors. Remember to make the lengths match!
uint8_t colors[NUM_COLORS][COLOR_CHANNELS] = {
  {255,   0,   0},
  {171,  85,   0},
  {171, 171,   0},
  {  0, 255,   0},
  {  0, 171,  85},
  {  0,   0, 255},
  { 85,   0, 171},
  {171,   0,  85}
};

//allocate memory for the active color
uint8_t* pActiveColor = (uint8_t*) malloc(COLOR_CHANNELS*LEN_COLOR_VAL/8);

/**
 * Linearly interpolate two values
 * Inputs:
 *  start- beginning value
 *  ends - ending value
 *  pos - position to evaluate interpolation
 * Returns an int between start and ends
 */
int lerp(int start, int ends, double pos){
  double s = (double) start;
  double e = (double) ends;
  return (int) (e-s)*pos+s;//travel pos along the line from e to s, add s to it.
}

/**
 * Interpolate an RGB color at a position
 * Inputs:
 *  pStart - pointer to first element of color start array
 *  pEnd - pointer to first element of color end array
 *  pTarget - pointer to first element of target array
 *  pos - position along interpolation to evaluate
 * Sets the values at pTarget to be equal to the interpolation of pos length from pStart to pEnd
 */
void interpolateColor(uint8_t* pStart, uint8_t* pEnd, uint8_t* pTarget, double pos){
  int i;
  for(i = 0; i < COLOR_CHANNELS; i++){
    int offset = i*LEN_COLOR_VAL;
    int startValue = *(pStart + offset);
    int endValue = *(pEnd + offset);
    *(pTarget+offset) = (uint8_t) (lerp(startValue, endValue, pos));
  }
}

void setup(){
  Serial.begin(9600);
  //Set up pins
  pinMode(GREEN_LED, OUTPUT);
  pinMode(RED_LED, OUTPUT);
  pinMode(BLUE_LED, OUTPUT);
  pinMode(TILT_PIN, INPUT);
}


void loop() {
  // put your main code here, to run repeatedly: 
  int i;//index of color
  int j;//index of transition
  for (i = 0; i<NUM_COLORS; i++){
    //color we are transitioning from
    uint8_t* pStartColor = &colors[i][0];
    uint8_t* pEndColor;

    //figure out which color we will be transitioning to
    if (i != NUM_COLORS - 1){
      pEndColor = &colors[i+1][0];
    }
    else{
      pEndColor = &colors[0][0];//prevent overflow
    }
    
    for(j=0; j<256; j++){

      //stop while the tilt is active
      while (digitalRead(TILT_PIN) == HIGH);
      

      
      double pos = ((float) j)/256.0;
      interpolateColor(pStartColor, pEndColor, pActiveColor, pos);
      int red = *pActiveColor;
      int green = *(pActiveColor + LEN_COLOR_VAL);
      int blue = *(pActiveColor + 2*LEN_COLOR_VAL);
      //Debug prints
      /*Serial.print("Red: ");
      Serial.print(red);
      Serial.print(", Green: ");
      Serial.print(green);
      Serial.print(", Blue: ");
      Serial.println(blue);*/
      analogWrite(RED_LED, red);
      analogWrite(GREEN_LED, green);
      analogWrite(BLUE_LED, blue);
      delay(timestep);
    }
  }
}

Credits

Clayton Ramsey
1 project • 0 followers
Contact
Rosemary Lach
1 project • 0 followers
Contact
Matthew Dai
1 project • 0 followers
Contact
Grant Parajuli
1 project • 0 followers
Contact
Ian Ramsey
1 project • 0 followers
Contact
Nathan Moskowitz
1 project • 0 followers
Contact
Noah Rossi
1 project • 0 followers
Contact
Samatar Dalmar
2 projects • 0 followers
Contact

Comments

Please log in or sign up to comment.