LED fade while finger held on pushbutton

Hi, I'm trying to combine two bits of basic code from the starter kit. The fade and button sketch, where, while my finger is pressed down the LED comes on and fades on and off. Simple but I can't combine the two 'if' statements and can't find a video on it. Use code tags to format code for the forum

<CODE/>

int led = 3;         // the PWM pin the LED is attached to
int brightness = 0;  // how bright the LED is
int fadeAmount = 5;  // how many points to fade the LED by
int buttonState=0;
int buttonPin=8;

// the setup routine runs once when you press reset:
void setup() {
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
  pinMode(buttonPin, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  
  buttonState = digitalRead(buttonPin);
  
  if (buttonState == HIGH);{
    digitalWrite(led,HIGH);}
  else{ digitalWrite (led,LOW);}}
  
  // set the brightness of pin 9:
  analogWrite(led, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(30);


}

<CODE/>

if (buttonState == HIGH);

That semicolon should not be there. It is the only code that is executed conditionally when buttonState is HIGH

Thank you. It didn't fix the problem though. Do you know how to combine two 'if' statements? Is the way the code is constructed correct?

too much brackets

Hello will353

Welcome to the world's best Arduino forum ever.

Try and check this mods to your sketch:

int led = 3;         // the PWM pin the LED is attached to
int brightness = 0;  // how bright the LED is
int fadeAmount = 5;  // how many points to fade the LED by
int buttonState = 0;
int buttonPin = 8;
// the setup routine runs once when you press reset:
void setup()
{
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
  pinMode(buttonPin, INPUT);
}
// the loop routine runs over and over again forever:
void loop()
{
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH)
  {
//    digitalWrite(led, HIGH);
    // set the brightness of pin 3:
    analogWrite(led, brightness);

    // change the brightness for next time through the loop:
    brightness = brightness + fadeAmount;

    // reverse the direction of the fading at the ends of the fade:
    if (brightness <= 0 || brightness >= 255) {
      fadeAmount = -fadeAmount;
    }
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }
  else
  {
    digitalWrite (led, LOW);
  }
}

Have a nice day and enjoy coding in C++.

Hi @will353 ,

Welcome to the forum..

maybe something like this..

int led = 3;         // the PWM pin the LED is attached to
int brightness = 0;  // how bright the LED is
int fadeAmount = 5;  // how many points to fade the LED by
int buttonState = 1;
int buttonPin = 8;
//is button down..
bool down = false;
//millis timer for btn debounce
unsigned long lastRead = 0;
//millis timer for fade
unsigned long lastDim = 0;
//interval delay for each fade step
int intervalDim = 30;
//are we giong up or down//
bool dimming = true;

// the setup routine runs once when you press reset:
void setup() {
  // declare pin 9 to be an output:
  // pinMode(led, OUTPUT); //not needed for analog
  //switched to use internal pull up..
  //this reveres button logic..
  pinMode(buttonPin, INPUT_PULLUP);
}

// the loop routine runs over and over again forever:
void loop() {

  //what time is it now..
  unsigned long now  = millis();

  if (now - lastRead >= 50) {
    byte btn = digitalRead(buttonPin);
    if (btn != buttonState) {
      lastRead = now; //start debouncing..
      buttonState = btn;
      if (btn == LOW) {
        if (!down) {
          brightness = 255;
          analogWrite(led, brightness);
          dimming = true;
        }
        down = true;
      } else {
        down = false;
        analogWrite(led, 0);
      }
    }
  }


  if (down) {
    if (now - lastDim >= intervalDim) {
      lastDim = now;
      // change the brightness for next time through the loop:
      if (dimming) {
        brightness -= fadeAmount;//subtract fade
        if (brightness == 0) dimming = false;
      } else {
        brightness += fadeAmount;//add fade
        if (brightness == 255) dimming = true;
      }
      // set the brightness of pin 9:
      analogWrite(led, brightness);
    }
  }
}

Project simulated here..

breaking change is switching to use input pull ups but will be better or you have external pull down resistors, i'm thinking no, hence the pull ups..

add debounced to the button..
removed use of delay and replaced with millis timer for fade effect..
delay() function makes your sketch sit and wait, not good when you want to watch for buttons..

happy holidays.. ~q

Thanks Paul Paulson that worked perfectly. I'll be able to try and work out which bit was tripping me up.

Thanks for helping. This worked perfectly only the LED was on permanently and faded only when the button was being pressed. I haven't encountered some of that C++ you used yet but really good for reference thank you

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.