LED brightness control

MaJiG:
Here's my try:

Connect a SPST N.O. momentary from pin 8 to GND with no resistor.
Connect an LED from pin 9 to an LED in series with a current-limiting resistor (220 to 330 ohm) to GND.

My code:

int but = 8;

int led = 9;
int val = 0;

void setup()
{
 pinMode(but, INPUT_PULLUP);
 pinMode(led, OUTPUT);             // not needed, but I like to be explicit
}

void loop()
{
 if(digitalRead(but) == LOW)
 {
   //here: the button is pushed
    val += 20;                                 // increment the brightness of the LED by 1/24th
   val = min(val, 255);                     // maximum PWM value is 255
 }
 else
 {
   //here: the button has been released
   val = 0;                                  // turn the LED OFF
 }

analogWrite(led,val);                 // adjust the PWM
 delay( 250 );                           // give yourself a chance to see the result
}




should take about 3 seconds from OFF to full brightness. If you need it to be quicker, adjust the delay time
the delay also tends to debounce the switch too

Thank you, that one worked, but I didn't know input_pullup thing.

HazardsMind:
Ok those need to be debounced to work properly. So even if you just press it once, being that it is not being debounce, it will put out a LOT of noise. Meaning even if YOU press it once, the arduino will see that it was pressed multiple times, because of that noise. Also did you include a pulling resistor?

Don't know what pulling resistor is
What exactly is it ?