Jacob WilliamsJing HuZach RewolinskiYulei ShenCheng PengTochukwu Okwuonu
Published

RAYgbiv

Are you scared of the cold embrace of darkness? Brighten up your night and dream with RAYgbiv.

BeginnerFull instructions provided430
RAYgbiv

Things used in this project

Story

Read more

Schematics

Schematic

Code

Fading RAYgbiv

C/C++
Hold down on either button to turn on your nightlight and fall asleep quickly! Saves energy and turns off after button is no longer pressed. Button 1 dims the redness of the colors when tilted and button 2 dims the blueness of the colors when tilted.
// Rename our pins from numbers to readable variables
#define RED RED_LED
#define GREEN GREEN_LED
#define BLUE BLUE_LED
#define TILT 24

// Define delay time (ms)
#define delayTime 10 

// The values of each color
int redVal;
int greenVal;
int blueVal;
int color = 255;

// Button Use
const int buttonPin1 = PUSH1;
const int buttonPin2 = PUSH2;
int buttonState1 = 0; 
int buttonState2 = 0;
int tiltSwitch = 0;


void setup() {
  // Set up pins
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  // Set up button for input
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(TILT, INPUT);
  delay(10);
}

void dimRED(){
  //read state of tilt switch
  tiltSwitch = digitalRead(TILT);
  //dims the red RGB value
  while(tiltSwitch){
    analogWrite(RED, LOW);
    tiltSwitch = digitalRead(TILT);
  }
  //resets if not tilted
  analogWrite(RED, redVal);
}

void dimBLUE(){
  //read state of tilt switch
  tiltSwitch = digitalRead(TILT);
  //dims the blue RGB value
  while(tiltSwitch){
    analogWrite(BLUE, LOW);
    tiltSwitch = digitalRead(TILT);
  }
  //resets if not tilted
  analogWrite(BLUE, blueVal);
}


void loop() {
  //read state of button
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  if(buttonState1 == LOW || buttonState2 == LOW){
    redVal = 255;
    blueVal = 0;
    greenVal = 0;
    /* Fade from red to orange */
    for(int i = 0; i < 85; i++ ){
      greenVal += 1;
      redVal -= 1;
      analogWrite(RED, redVal); // set the new red value
      analogWrite(GREEN, greenVal); // set the new green value
      delay(delayTime); // wait for how long delay time is
      if(buttonState1 == LOW) {
        dimRED();
      }
      if(buttonState2 == LOW) {
        dimBLUE();
      }
    }

    /* Fade from orange to yellow */
    for(int i = 0; i < 85; i++){
      greenVal += 1;
      analogWrite(GREEN, greenVal); // set the new green value
      delay(delayTime); // wait for how long delay time is
      if(buttonState1 == LOW) {
        dimRED();
      }
      if(buttonState2 == LOW) {
        dimBLUE();
      }
    }

    /* Fade from yellow to green */
    for(int i = 0 ; i < 85; i++){
      redVal -= 2;
      greenVal += 1;
      analogWrite(RED, redVal); // set the new red value
      analogWrite(GREEN, greenVal); // set the new blue value
      delay(delayTime); // wait for how long delay time is
      if(buttonState1 == LOW) {
        dimRED();
      }
      if(buttonState2 == LOW) {
        dimBLUE();
      }
    }

    /* Fade from green to aqua to blue */
    for(int i = 0 ; i < 255; i++){
      greenVal -= 1;
      blueVal += 1;
      analogWrite(GREEN, greenVal); // set the new red value
      analogWrite(BLUE, blueVal); // set the new blue value
      delay(delayTime); // wait for how long delay time is
      if(buttonState1 == LOW) {
        dimRED();
      }
      if(buttonState2 == LOW) {
        dimBLUE();
      }
    }
  
    /* Fade from blue to purple to pink to red */
    for(int i = 0 ; i < 255; i++){
      redVal += 1;
      blueVal -= 1;
      analogWrite(RED, redVal); // set the new red value
      analogWrite(BLUE, blueVal); // set the new blue value
      delay(delayTime); // wait for how long delay time is
      if(buttonState1 == LOW) {
        dimRED();
      }
      if(buttonState2 == LOW) {
        dimBLUE();
      }
    }
  }
  else {
    //if button not pressed, turn LED off
    digitalWrite(RED, LOW);
    digitalWrite(GREEN, LOW);
    digitalWrite(BLUE, LOW);
  }
}

Credits

Jacob Williams
1 project • 3 followers
Contact
Jing Hu
1 project • 6 followers
elec 220!
Contact
Zach Rewolinski
1 project • 3 followers
Contact
Yulei Shen
2 projects • 2 followers
Contact
Cheng Peng
1 project • 3 followers
Contact
Tochukwu Okwuonu
1 project • 3 followers
Contact

Comments

Please log in or sign up to comment.