RGB LED control with a button

I wanted to understand how to change the color on a RGB LED and how to control it. This setup have a default blue pulse and a push button to change the color to red-orange for a short time. 

The setup is simple and the code is based on standard Arduino tutorials like blink without delay and state change detection. In the code the button have a small delay() in case of debouncing, and this can be changed using millis() if using delay is a problem.

The breadboard setup:

Skjermbilde

Skjermbilde2Rapidtables.com is a great resource for color information when finding the correct colors and programming color changes.

The arduino code:

/*
https://forum.arduino.cc/index.php?topic=345094.0

Fading LED without delay()
https://www.baldengineer.com/fading-led-analogwrite-millis-example.html

State Change Detection (Edge Detection) for pushbuttons:
https://www.arduino.cc/en/Tutorial/StateChangeDetection

For color values:
http://www.rapidtables.com/web/color/RGB_Color.htm
*/
 
#define GREEN 5
#define BLUE 6
#define RED 4

// define directions for LED fade
#define UP 0
#define DOWN 1
// State Variable for Fade Direction
byte fadeDirection = UP;

// define function for LED fade
#define fadeDefault 0
#define fadeButton 1
// State Variable for LED fucntion
byte LEDfunction = fadeButton;

const int  buttonPin = 2;    // the pin that the pushbutton is attached to
int redVal = 0;
int greenVal = 0;
int blueVal = 0;

int buttonState = 0;                 // current state of the button
int lastButtonState = 0;             // previous state of the button

int fadeInterval = 5;                // delaytime between led value increments
unsigned long previousFadeMillis;    // millis() timing Variable, just for fading

int buttonfadetime = 1500;            // Run the new fade this long
unsigned long lastTimeItHappened = 0;      // The clock time in millis()
unsigned long howLongItsBeen;              // A calculated value


void setup() {
  Serial.begin(9600);  // initialize serial communication
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  pinMode(RED, OUTPUT);
  digitalWrite(GREEN, HIGH);
  digitalWrite(BLUE, HIGH);
  digitalWrite(RED, HIGH);
  LEDfunction = fadeButton;
}
 
void loop() {
  // get the current time, for this time around loop. all millis() timer checks will use this time stamp
  unsigned long currentMillis = millis();
  FadeDefault(currentMillis);
  CheckButton();
  FadeButton(currentMillis);
  UpdateLED();  

  //the push buttons resets this to change the RGB led
  howLongItsBeen = millis() - lastTimeItHappened;
         if ( howLongItsBeen >= buttonfadetime ) {
              LEDfunction = fadeDefault;  //runs all the time unnless button is pushed
         }else{
              LEDfunction = fadeButton;   //change fade function
         }
debug();
}

void debug(){
      Serial.print("   howLongItsBeen:  ");
      Serial.print(howLongItsBeen);
      Serial.print("   LEDfunction:  ");
      Serial.print(LEDfunction);
      Serial.print("   R:");
      Serial.print(redVal);
      Serial.print("   G:");
      Serial.print(greenVal);
      Serial.print("   B:");
      Serial.println(blueVal);
}

void UpdateLED(){
                analogWrite( RED, 255 - redVal );
                analogWrite( GREEN, 255 - greenVal );
                analogWrite( BLUE, 255 - blueVal ); 
}

void FadeDefault(unsigned long thisMillis) {
  if (LEDfunction == fadeDefault) {  //Only run this function if the default is on
            if (thisMillis - previousFadeMillis >= fadeInterval) {
                  if (fadeDirection == UP) {         
                              redVal = 0;
                              greenVal += 2;
                              blueVal += 4;  
                              if (greenVal >= 120 && blueVal >= 240) { // At max, limit and change direction
                                greenVal = 120;
                                blueVal = 240;
                                fadeDirection = DOWN;
                              }
                  } else {   //if we aren't going up, we're going down, swopping increments here
                              redVal = 0;
                              greenVal -= 2;
                              blueVal -= 4;  
                              if (greenVal <= 0 && blueVal = fadeInterval) {
                  if (fadeDirection == UP) {         
                              redVal = 255;
                              greenVal += 10;
                              blueVal = 0;  
                              if (greenVal >= 50) { // At max, limit and change direction
                                greenVal = 50;    
                                fadeDirection = DOWN;
                              }
                  } else {   //if we aren't going up, we're going down, swopping increments here
                              redVal = 255;
                              greenVal -= 10; 
                              blueVal = 0; 
                              if (greenVal <= 0) {
                                greenVal = 0;         // At min, limit and change direction
                                fadeDirection = UP;
                              }
                   }
                previousFadeMillis = thisMillis;   // reset millis for the next iteration (fade timer only)
            }

    } // end if LEDfunction
}

void CheckButton() {

  buttonState = digitalRead(buttonPin);    // read the pushbutton input pin
  if (buttonState != lastButtonState) {    // compare the buttonState to its previous state
    // if the state has changed, reset timer
    if (buttonState == HIGH) {
      lastTimeItHappened = millis();
          } else {
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s