LED fading effect using a push button Guys please help me out in verifying my program for my project -- led fading effect using a push button.
My project is simple, I want to start the LED fading effect (i.e., glowing and dimming one after another) at one click on the push button that is pushing and then leaving the push button and on my another click I want the effect to get off.
The main problem is coming in the programming part and rest all the circuit and components are fine.
THIS IS MY SKETCH :-
int ledpin = 9;
int buttonpin = 8;
boolean buttonpressed = false;   // to check whether the button was pressed //
int brightness = 0;
int fadeamount = 5;

void setup ()
{ 
pinMode (ledpin, OUTPUT);
pinMode (buttonpin,INPUT);
digitalWrite (ledpin,LOW);
}

void loop ()
{      
if ( digitalRead (buttonpin) == HIGH)
 { 
  buttonpressed = true;
 }

 else {
     if ( buttonpressed = true &amp;&amp; digitalRead(ledpin) == LOW)
       {
           brightness = brightness + fadeamount;
           if ( brightness == 0 || brightness == 255)
            { 
             fadeamount = -fadeamount;}
             analogWrite(ledpin, brightness);
             delay (30);
             buttonpressed = false;
        }
      if (buttonpressed == true &amp;&amp; analogRead(ledpin) == brightness)
        {
          digitalWrite(ledpin,LOW); 
          buttonpressed = false;
        }
   }
 }

THIS IS THE CIRCUIT DIAGRAM
The 1k (pulldown-)resistor must be placed at the other side of the button (the pin8 side, not the 5v side).

The led needs a resistor, to limit the current.

There is no need for the buttonstate variable, as you can just check the brightness to see if the PWM value must be changed.

int ledpin = 9;
int pushbutton = 8;

int brightness = 0;
int fadeamount = 1;

void setup ()
{ 
  pinMode (ledpin,OUTPUT);
  pinMode (pushbutton,INPUT);
}

void loop ()
{ 
  if( digitalRead(pushbutton)==HIGH &amp;&amp; brightness&lt;255 )
  {
    // fade in led 
    brightness = brightness + fadeamount;// increase brightness
    if( brightness&gt;255 ) // check that brightness doesn't exceed 255
      brightness = 255;
    analogWrite(ledpin,brightness);
    delay (1);
  }
  else if( digitalRead(pushbutton)==LOW &amp;&amp; brightness&gt;0 )
  {
    // fade out led
    brightness = brightness - fadeamount;// decrease brightness
    if( brightness&lt;0 ) // check that brightness doesn't go below 0
      brightness = 0;
    analogWrite(ledpin,brightness);
    delay (1);
  }

}