Fading LED with push button

Hi, First of all I am new in Arduino, and a new project wanted to use it. The project requires a common LED to fade when a pushbutton is pressed. A code I found and modified a bit is this:

int brightness1;
int brightness2;

void setup() {
pinMode(5, OUTPUT); //LED
pinMode(6, OUTPUT); //LED
pinMode(12, INPUT_PULLUP); //button
pinMode(11, INPUT_PULLUP);
}

void loop() {
brightness1 = constrain(brightness1, 0, 255);
brightness2 = constrain(brightness2, 0, 255);

analogWrite(5, brightness1);
analogWrite(6, brightness2);

delay(10);

if (digitalRead(12) == LOW) {
(brightness1 = brightness1 + 1);
(brightness2 = brightness2 - 1);

}

if (digitalRead(11) == LOW) {
brightness1 = brightness1 - 1;
brightness2 = brightness2 + 1;

}
}

The problem is that when the pushbutton needs to be pressed too activate the loop. Is there a way to start the loop with just a push of a button?

Thanks

matthew_ghigo_99:
The problem is that when the pushbutton needs to be pressed too activate the loop. Is there a way to start the loop with just a push of a button?

I'm not sure what you mean. The loop starts as soon as the setup finishes, and it keeps on running until the end of times (or when you unplug the Arduino).

Your code doesn't really do much unless you press a button, so it shouldn't matter in this case.

Please edit your original post and add [code][/code] tags around your code.

And if you want easy, have a look at the FadeLed library :slight_smile: