Double Rainbow

Not just your average fading RGB LED, our project can also switch directions at the tilt of a tilt switch.

BeginnerFull instructions provided494
Double Rainbow

Things used in this project

Hardware components

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

Software apps and online services

Energia
Texas Instruments Energia

Story

Read more

Schematics

Hardware Schematic

Code

ReverseRainbow

Arduino
This code controls the LED's rainbow fading pattern, and reverses it if the tilt switch is activated.
#define R_LED RED_LED
#define G_LED GREEN_LED
#define B_LED BLUE_LED

const int switchPin = 5;   

 
int direction = -1; 
int max = 256; 
int speed_multiplier = 20;

void setup() {

  Serial.begin(9600);

  //Setup pinmodes for LEDs and Tilt_Switch
  pinMode(R_LED, OUTPUT); 
  pinMode(B_LED, OUTPUT); 
  pinMode(G_LED, OUTPUT);   
  pinMode(switchPin, INPUT_PULLUP);
  
}

void loop() {

  //initialise LED variables 
  float red_value = 255;
  float green_value = 0;
  float blue_value = 0;

  //counter for fade loop
  int counter = 0;

  //reversing counter direction
   if(direction == -1){
      counter = 255;
   }

  //main fade loop
  for(counter; counter < max; counter += direction){
    //checking if tilt switch is tilted
    int tiltState = digitalRead(switchPin);

    //if tilt switch is tilted, go in reverse
    if(tiltState == LOW){
      direction = -1;    
    }
    else
    {
      direction = 1;
    }
    //delaying the loop to control fade speed
    delay(speed_multiplier);

    //adjusting values for LEDs 
    if(counter < 32){
        red_value = red_value - (21.0/8.0) * direction; 
        green_value = green_value + (85.0/32.0) * direction; 
    } else if (counter > 31 && counter < 64){
          green_value = green_value + (42.0/16.0) * direction; 
    } else if (counter > 63 && counter < 96){
        red_value = red_value - (171.0/32.0)* direction;
        green_value = green_value + (21.0/8.0)* direction;
    } else if (counter > 95 && counter < 128){
        green_value = green_value - (21.0/8.0)* direction;
        blue_value = blue_value + (85.0/32.0)* direction;
    }else if (counter > 127 && counter < 160){
        green_value = green_value - (171.0/32.0)* direction;
        blue_value = blue_value + (170.0/32.0)* direction;
    } else if (counter > 159 && counter < 192){
        red_value = red_value + (85.0/32.0)* direction;
        blue_value = blue_value - (21.0/8.0)* direction;
    } else if (counter > 191 && counter < 224){
        red_value = red_value + (43.0/16.0)* direction;
        blue_value = blue_value - (43.0/16.0)* direction;
    } else if (counter > 223 && counter < 256){
        red_value = red_value + (21.0/8.0)* direction;
        blue_value = blue_value - (85.0/32.0)* direction;
    }

    //ensuring values cannot go below zero
    if(red_value < 0){
      red_value = 0;
    }
    if(blue_value < 0){
      blue_value = 0;
    }
    if(green_value < 0){
      green_value = 0;
    }

    //ends loop if counter goes below zero - if the direction is reversed
    if(counter < 0){
      break;
    }
        
        // Output values to LEDs
        analogWrite(R_LED, (int)red_value);
        analogWrite(G_LED, (int)green_value);
        analogWrite(B_LED, (int)blue_value);

  }  
  
}

Credits

Nathan Powell
2 projects • 2 followers
Richard Omotosho
0 projects • 0 followers
Aditi Narwaney
0 projects • 0 followers
Vedant Patwari
1 project • 0 followers
Corey Tolbert
0 projects • 0 followers
Akash Karanam
0 projects • 0 followers
Roy Montemayor
0 projects • 0 followers
Cameron Diao
0 projects • 0 followers

Comments