LED COLOR FADING WITH 3 BUTTONS

Hello,

I'm pretty new to Arduino but have programmed PIC's before. Anyway I'm really enjoying the Arduino resources and ease of getting up and running. I'm starting with what I think should be a pretty simple project to have an LED RGB display fade through the colours, but with a twist. I have 3 buttons. One to pause the colour cycling, one to change the intensity of the display, and at the moment one to reset the board when things go out of whack. It kind of works. I'm sure there are some bugs in there. Anyway, it's pretty fun to play with the buttons and watch the lights. I've borrowed code from people on here but by now is pretty heavily modified so I don't feel to bad about the fact I can't find links to where the code came from anymore. Anyway, here's some code, if it helps anyone that's great, if it is full of errors (which I'm sure there are some) sorry about that and feel free to comment / correct what you think is wrong with it. The code is I believe set up for common Anode LEDs.

//Good working mixer with interupt on pin 2
//set up for common Anode LED
//Button is setup for pullup type resistor(pin connected to +5v through resistor)
#define GREEN 10
#define BLUE 9
#define RED 11
#define delayTime 20
#define FADEBUTTON 2
#define DIMMERBUTTON 3

long debouncing_time = 15; //Debouncing Time in Milliseconds
volatile unsigned long last_micros;

void setup() {

pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(RED, OUTPUT);
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, LOW);
digitalWrite(RED, LOW);
pinMode(FADEBUTTON, INPUT_PULLUP);
pinMode(DIMMERBUTTON, INPUT_PULLUP);
attachInterrupt(0, debounceInterrupt0, FALLING);
attachInterrupt(1, debounceInterrupt1, FALLING);
}

int redVal;
int blueVal;
int greenVal;
bool lightson = HIGH;
volatile bool fading = LOW;
volatile int dim = 0;

void loop() {
while(fading){
if(fading){
int redVal = (255 - dim);
int blueVal = 0;
int greenVal = 0;
for( int i = 0 ; i < 255 ; i += 1 ){

greenVal += 1;
redVal -= 1;
analogWrite( GREEN, (255 - greenVal) - dim );
analogWrite( RED, (255 - redVal) - dim );

delay( delayTime );
}
}
else{
break;
}
if(fading){
redVal = 0;
blueVal = 0;
greenVal = (255 - dim);
for( int i = 0 ; i < 255 ; i += 1 ){

blueVal += 1;
greenVal -= 1;
analogWrite( BLUE, (255 - blueVal) - dim );
analogWrite( GREEN, (255 - greenVal) - dim );

delay( delayTime );
}
}
else{
break;
}
if(fading){
redVal = 0;
blueVal = (255 - dim);
greenVal = 0;
for( int i = 0 ; i < 255 ; i += 1 ){
redVal += 1;
blueVal -= 1;
analogWrite( RED, (255 - redVal) - dim );
analogWrite( BLUE, (255 - blueVal) - dim );

delay( delayTime );
}
}
else{
break;
}
if (micros()<last_micros) last_micros = micros();
}
}

void debounceInterrupt0() {
if((long)(micros() - last_micros) >= debouncing_time * 1000) {
fading = !fading;
last_micros = micros();
}
}

void debounceInterrupt1() {
if((long)(micros() - last_micros) >= debouncing_time * 1000) {
if(dim<200){
dim = dim + 50;
}
else{
dim = 0;
}
if(!fading){
analogWrite( RED, (255 - redVal) - dim );
analogWrite( GREEN, (255 - greenVal) - dim );
analogWrite( BLUE, (255 - blueVal) - dim );
}
}
}

To make it easy for people to help you please modify your post and use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor. See How to use the Forum

...R

You should tell us what's wrong with it. "kind of works" isn't very helpful. We don't have your hardware setup to test it with.

Interrupts are almost never the right thing for human inputs, like buttons. You are only trying interrupts because you are using delay() for timing and you want to make the controls more responsive. The correct answer is to use the "BlinkWithoutDelay" method for timing and then you can check your buttons hundreds or thousands of times per second withoout interrupts:

const byte GreenLED_PWMPin = 10;
const byte BlueLED_PWMPin = 9;
const byte RedLED_PWMPin = 11;
const byte ColorFadeButton_DIPin = 2;
const byte DimmerButton_DIPin = 3;


unsigned CurrentHue = 0;  // 0 = Red.  255 = Green.  511 = Blue. 767 = Red
unsigned CurrentBrightness = 255;


const unsigned long DebounceInterval = 10;


const unsigned long FadeInterval = 20;


bool Fading = true;


void setup()
{
  pinMode(RedLED_PWMPin, OUTPUT);
  pinMode(GreenLED_PWMPin, OUTPUT);
  pinMode(BlueLED_PWMPin, OUTPUT);


  analogWrite( RedLED_PWMPin, 0);
  analogWrite( GreenLED_PWMPin, 0);
  analogWrite( BlueLED_PWMPin, 0);


  pinMode(ColorFadeButton_DIPin, INPUT_PULLUP);
  pinMode(DimmerButton_DIPin, INPUT_PULLUP);
}




void loop()
{
  unsigned long currentMillis = millis();
  static unsigned long lastFadeTime = 0;


  boolean fadeButtonIsPressed = digitalRead(ColorFadeButton_DIPin) == LOW;
  static boolean fadeButtonWasPressed = false;
  static unsigned long lastButtonPressTime;


  if (fadeButtonIsPressed != fadeButtonWasPressed && currentMillis - lastButtonPressTime >= DebounceInterval)
  {
    fadeButtonWasPressed = fadeButtonIsPressed;
    lastButtonPressTime = currentMillis;
    if (fadeButtonIsPressed)
      Fading = !Fading;
  }


  if (currentMillis - lastFadeTime >= FadeInterval)
  {
    lastFadeTime = currentMillis;
    if (Fading)
    {
      CurrentHue = (CurrentHue + 1) % 768;
    }


    // If the Dimmer button is being held down, fade up each FadeInterval
    if (digitalRead(DimmerButton_DIPin) == LOW)
      CurrentBrightness = (CurrentBrightness + 1) % 256;
  }


  // Convert Hue to RGB
  byte redValue, greenValue, blueValue;


  if (CurrentHue <= 255)
  {
    // Fade from Red to Green
    greenValue = CurrentHue;  // While green fades in...
    redValue = 255 - greenValue;  // ... red fades out
    blueValue = 0;
  }
  else if (CurrentHue <= 511)
  {
    // Fade from Green to Blue
    blueValue = CurrentHue - 256;  // While blue fades in...
    greenValue = 255 - blueValue;  // ... green fades out
    redValue = 0;
  }
  else
  {
    // Fade from Blue to Red
    redValue = CurrentHue - 512;  // While red fades in...
    blueValue = 255 - redValue;  // ... blue fades out
    greenValue = 0;
  }


  analogWrite( RedLED_PWMPin, (redValue * CurrentBrightness) / 255);
  analogWrite( GreenLED_PWMPin, (greenValue * CurrentBrightness) / 255);
  analogWrite( BlueLED_PWMPin, (blueValue * CurrentBrightness) / 255);
}