2 buttons to fade 2 LEDs , only one LED fades

Hi All ,

I am using an Arduino UNO with two buttons/switches connected using internal pull up resistors and two LEDs . The power source is USB port of laptop .

This is part of a bigger project but I thought I should solve the problems individually as they occur .

What I want to happen in this code :

  1. Push button 1 , white LED fades in
  2. Push button 1 again , white LED fades out
  3. Push button 2 , red LED fades in
  4. Push button 2 again , red LED fades out

What actually happens :

  1. When I push button 1 , white LED fades in , push button 1 again , white LED turns off ( I would like it to fade out but I am not sure how to put that in my code )
  2. When I push button 2 the red LED does not fade in

So , two questions : how do I get the 2nd button to work and how do I get the LEDs to fade out instead of snap off ? I hope this is all the info you need to help troubleshoot my problem , if not , let me know what else I need to post and I will do so .

This is my first time attaching code , hope this works .

const int button1Pin = 2;
const int button2Pin = 4;
const int whitePin = 3; // LED pin is a PWM pin
const int redPin = 5; // LED pin is a PWM pin

int button1State = HIGH, lastButton1State = LOW, led1State = LOW;
int button2State = HIGH, lastButton2State = LOW, led2State = LOW;


unsigned long lastDebounce1Time = 0,
              debounce1Delay    = 50,
              lastPress1Time    = 0,
              fade1Duration     = 3000; // fade over 3 seconds (3000ms)

unsigned long lastDebounce2Time = 0,
              debounce2Delay    = 50,
              lastPress2Time    = 0,
              fade2Duration     = 3000; // fade over 3 seconds (3000ms)

void setup() {
  pinMode(button1Pin, INPUT_PULLUP);
  pinMode(whitePin, OUTPUT);
  analogWrite(whitePin, 0);

  pinMode(button2Pin, INPUT_PULLUP);
  pinMode(redPin, OUTPUT);
  analogWrite(redPin, 0);

}

void loop() {
  int reading1 = digitalRead(button1Pin);
  int reading2 = digitalRead(button2Pin);

  unsigned long now1 = millis();
  unsigned long now2 = millis();

  if (reading1 != lastButton1State) {
    lastDebounce1Time = now1;
  }
  if ((now1 - lastDebounce1Time) > debounce1Delay) {
    if (reading1 != button1State) {
      button1State = reading1;

      if (button1State == LOW) {
        led1State = !led1State;
        lastPress1Time = now1;
      }
    }
  }

  if (led1State == HIGH) {
    analogWrite(whitePin, min(255, (now1 - lastPress1Time) * 255 / fade1Duration));
  } else {
    analogWrite(whitePin, 0);
  }
  lastButton1State = reading1;



  if (reading2 != lastButton2State) {
    lastDebounce2Time = now2;

    if ((now2 - lastDebounce2Time) > debounce2Delay) {
      if (reading2 != button2State) {
        button2State = reading2;

        if (button2State == LOW) {
          led2State = !led2State;
          lastPress2Time = now2;
        }
      }
    }

    if (led2State == HIGH) {
      analogWrite(redPin, min(255, (now2 - lastPress2Time) * 255 / fade2Duration));
    } else {
      analogWrite(redPin, 0);
    }

    lastButton2State = reading2;
  }
}

Thanks for taking the time to read through this post and any help would be appreciated .

Tom

if (led1State == HIGH) {
    analogWrite(whitePin, min(255, (now1 - lastPress1Time) * 255 / fade1Duration));
  } else {
    analogWrite(whitePin, 0);
  }

When led1State goes low you immediately write a zero to whitePin.

Hi dougp ,

Thanks for the reply .

That piece of code is one of my stumbling blocks ,it is there now so i can make sure my button is working . I have the analogWrite set to 0 for the whitePin because I do not know what code to add that will fade the LED out instead of snapping off .

Any suggestions with what to add to get the fade out ?

Thanks for your time ,

Tom

I have not done it myself but with 6k+ results for a site search of 'fading leds', it's a virtual certainty that multiple somebodies have been down this road already.

Also, this code calls for arrays.

Place all those parameters in a 2-element array, then you can almost halve the length of your code by iterating over the two button/LED combinations. After all, behaviour of both is the same. It also makes coding a lot easier as you can change everything in one place.

And to do the fade-out: basically the exact same as the code for the fade-in, just counting in the opposite direction.

Hi wvmarle ,

I will do some reading on arrays and try to edit the fade out code .

Thanks for the suggestions .

Tom