Push button LED fadeing

Hi,
I am new with this arduino but i have struggleing to get this to work.

Idea:

When press press button laed fades up to 255 ;
Keeps this way until release button then fades down 0:

I belive its easy but just cant get idea how tho finalize it.

I manage to find code what was closest what i have seen

#define BUTTON 2
#define LED 9

int dir = 1;
int lum = 0;
boolean toggle = false;

void setup() {
pinMode(BUTTON, INPUT);
pinMode(LED, OUTPUT);
}

void loop() {
if (digitalRead(BUTTON) == HIGH) {
toggle = true;
lum += dir;
if ((lum >= 0) && (lum < 256))
analogWrite(LED, lum);
delay(20);
} else { // toggle direction
if (toggle) {
dir = -1 * dir;
toggle = false;
}
}
}

I would to it in a different way. When the pin of the button is HIGH, is that when the button is pressed ?

// not tested

const int pinButton = 2;
const int pinLed = 9;
 
int lum = 0;
 
void setup() {
  pinMode( pinButton, INPUT);
  pinMode( pinLed, OUTPUT);
}

void loop() {
  if (digitalRead(pinButton) == HIGH && lum<255) {
    lum++;
  } else if (lum>0) {
    lum--;
  }
  analogWrite( pinLed, lum);
  delay(20);    // make loop() run 50 times a second
}

Are you willing to use millis() and create a piece of code that run 100 or 50 times a second ?
Do you need to debounce the switch ?

Thanks its awsome! it works as needed!

Came to another question how you can add another button and second led?

const int pinButton = 2;
const int pinLed = 9;
const int pinButton2 = 3;
const int pinLed2 = 4;

int lum = 0;

void setup() {
pinMode( pinButton, INPUT);
pinMode( pinLed, OUTPUT);
pinMode( pinButton2, INPUT);
** pinMode( pinLed2, OUTPUT);**

}

void loop() {
if (digitalRead(pinButton) == HIGH && lum<255) {
lum++;
} else if (lum>0) {
lum–;
}
analogWrite( pinLed, lum);
delay(20); // make loop() run 50 times a second
}

What to add to loop? if i make copy paste and change variables it dosent work.

You have two buttons and two leds, but you also need two variables that can change independently, so you need lum2.

Do you want the two buttons to work, even at the same time or when one is busy and the other one is pressed ? Then you need millis() and no longer the delay() function.

Hi, Two buttons work separatleywith 2 leds. So i will have to led lights working if button is pressed button1=led1 button2=led2

I don't understand. Is that a question ?

No just answer how leds should work. They need to work like two separate cirucles so both can be switched on sametime or only one is switched on.

So it is "millis()" after all :P

Start here : http://arduino.cc/en/Tutorial/BlinkWithoutDelay And then this : http://forum.arduino.cc/index.php?topic=223286.0

Read the reference for analogWrite() to see which pins can be used: http://arduino.cc/en/Reference/analogWrite

Then there was a bug in my previous sketch :-[
That is why I have now an if-statement inside an if-statement to increment or decrement the lum variable.
The function millis() is used to run a piece of code every 20ms.

Can you add the section for the other led ?

const int pinButton1 = 2;
const int pinLed1 = 9;
const int pinButton2 = 3;
const int pinLed2 = 4;     // doesn't work with PWM ! other pin please.

int lum1 = 0;
int lum2 = 0;

unsigned long prevMillis;
unsigned int interval = 20;
 
void setup() {
  Serial.begin(9600);
  Serial.println("Started");
  
  pinMode( pinButton1, INPUT);
  pinMode( pinLed1, OUTPUT);
  
  pinMode( pinButton2, INPUT);
  pinMode( pinLed2, OUTPUT);

  // Set prevMillis at the end of setup()
  prevMillis = millis();   // 0UL is "zero unsigned long"
}


void loop() {
  // Here is the place to do some debouncing.
  // For example with the Bounce2 library.
  //   http://playground.arduino.cc/code/bounce
  
  
  
  // Make a piece of code that runs every 20ms
  unsigned long currentMillis = millis();
  if( currentMillis - prevMillis >= interval) {
    prevMillis = currentMillis;
    
    
    // --------------------------------------------
    // Section for Led1
    boolean button1;   // state of button
    
    if (digitalRead( pinButton1) == HIGH)
      button1 = true;
    else
      button1 = false;
      
    if (button1) {     // button1 pressed ?
      if (lum1<255)
        lum1++;
    } else {           // buton1 released ?
      if (lum1>0)
        lum1--;
    }
    analogWrite( pinLed1, lum1);
    // --------------------------------------------
    
    
    // For debugging:
    //   Serial.print(lum1);
    //   Serial.print(", ");
    //   Serial.println(lum2);
  }
}