fade effect - 2 push buttons

What i want to do is to fade low to high and high to low an led using 2 pushbutons very slowly and when i release the button to stay in that state. I have all on my breadboard but i have no clue how to do this. Tomorrow i want to present it to my teacher. Yesterday i got an arduino, and what can i do is to light the led with a pushbutton. Thank you so much!

``````const byte LED_PIN = 13;
const byte UP_BUTTON_PIN = 2;  // Connect button between pin and Ground
const byte DOWN_BUTTON_PIN = 3;  // Connect button between pin and Ground

byte brightness = 0;

void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(UP_BUTTON_PIN, INPUT_PULLUP);
pinMode(DOWN_BUTTON_PIN, INPUT_PULLUP);
}

void loop() {
if (digitalRead(UP_BUTTON_PIN) == LOW  && brightness < 255)
brightness++;
if (digitalRead(DOWN_BUTTON_PIN) == LOW  && brightness > 0)
brightness--;
analogWrite(LED_PIN, brightness);
delay(20);  // About 5 seconds from full off to full on
}
``````

Should i use 10k resistor like the man in this video does?

Do i short it out if i connect it directly? I don't quite understand how digitalRead is done. Is there any current flow from the pin 2 and 3 when i'm reading from? If there's now current flowing from pin 2 and 3 through the ground, why did he use 10k resistor and didn't just simply connect it directly?

And why do we need to connect it to the ground as long as just the +5v matters? How about pushbuttons with only 2 leads?

The best & normal way is just as johnwasser shows you in reply #1. You don't connect the button to +5v, just connect the buttons between pins 2 & 3 & ground. johnwasser's code is looking for a ground on pin2 or 3 when the button is pressed. The code in setup()

``````pinMode(UP_BUTTON_PIN, INPUT_PULLUP);
pinMode(DOWN_BUTTON_PIN, INPUT_PULLUP);
``````

uses INPUT_PULLUP to use a resistance internal to the chip that makes pin 2 & 3 +5V when the button is not closed.

Just change

``````const byte LED_PIN = 13;
``````

to

``````const byte LED_PIN = 10;
``````

and it will work just fine for you.
You do not need the 10K resistor using this method.

You also need to use his code in loop() just as it's written

``````void loop() {
if (digitalRead(UP_BUTTON_PIN) == LOW  && brightness < 255)
brightness++;
if (digitalRead(DOWN_BUTTON_PIN) == LOW  && brightness > 0)
brightness--;
analogWrite(LED_PIN, brightness);
delay(20);  // About 5 seconds from full off to full on
}
``````

You never want to leave an input pin "open" because you will just read random noise. You should always use a resistor to connect the input pin to +5 (pull-up) or Ground (pull-down). That means there are two ways to connect a button:

If you use a pull-up resistor the button goes between the pin and Ground. Closing the button causes the input to switch from HIGH to LOW.

If you use a pull-down resistor the button goes between the pin and +5. Closing the button causes the input to switch from LOW to HIGH.

I tend to use the pull-up version because the Arduino has internal pull-up resistors that can be enabled in software:

``````// Old style, pre Arduino 1.0:
pinMode(inputPin, INPUT);
digitalWrite(inputPin, HIGH);
``````
``````// New style, Arduino 1.0+:
pinMode(inputPin, INPUT_PULLUP);
``````

Because of the internal pull-up resistors this form doesn't require an external resistor like the pull-down form does.