user19511: 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 && digitalRead(ledpin) == LOW)
       {
           brightness = brightness + fadeamount;
           if ( brightness == 0 || brightness == 255)
            { 
             fadeamount = -fadeamount;}
             analogWrite(ledpin, brightness);
             delay (30);
             buttonpressed = false;
        }
      if (buttonpressed == true && analogRead(ledpin) == brightness)
        {
          digitalWrite(ledpin,LOW); 
          buttonpressed = false;
        }
   }
 }

THIS IS THE CIRCUIT DIAGRAM


user19513: try this, its simple and easy to understand,
int ledPin = 9;
int btnPin = 8;
int brightness = 0;
int btnState = LOW;
bool trigger = LOW;
void setup ()
{
  pinMode (ledPin, OUTPUT);
  pinMode (btnPin, INPUT);
}

void loop ()
{
  if ( digitalRead(btnState) && !btnState)
  {
    if (trigger)
    {
      for (int i = 0; i < 256; i++)
      {
        analogWrite(ledPin, i);
        // wait for 30 milliseconds to see the dimming effect
        delay(30);
      }
    }
    else
    {
      for (int i = 255; i >= 0; i--)
      {
        analogWrite(ledPin, i);
        // wait for 30 milliseconds to see the dimming effect
        delay(30);
      }
    }
    trigger = !trigger;
  }
  btnState = digitalRead(btnState);
}


user32573: I changed things a bit and got a solution
int led = 3;
int buttonpin = 8;
boolean waspressed = false;

int inertia = 10;

void setup (){ 
  pinMode(buttonpin,INPUT);
  pinMode(led,OUTPUT);
  analogWrite(led,0);
}

void loop(){
  if(digitalRead(buttonpin) == HIGH){
    waspressed = true;
  }else{
     if (waspressed == true){
       fadeLed(digitalRead(led), inertia);
       waspressed = false;
     }
  }
}

void fadeLed(boolean input, int inertia){
  for(int state=0;state<256;state++){
    if (input==LOW){
      analogWrite(led, state);
    }else{
      analogWrite(led, 255-state);
    }
    delay(inertia);
  }
}

I created a function called fadeLed that receives two arguments: The actual state of the led and the delay - in miliseconds - that is going to determine how long the fading is going to last.
You can see that the rest of the code is pretty much the one you got in the other question you asked. This is because we already got a good code to control the behavior of the button, so its a good practice to keep it and just change what you want to be changed: to fade instead of blink. So we just change a single line and create a function to be called by that line. Functions are a good way to keep a clean code and are a good programming practice. They make the code easy to read and maintain.
The function fadeLed() uses PWM to fade the led. In Arduino Uno, PWM is available in digital pins 3, 5, 6, 9, 10, and 11. This may change according to the model you are using. It's very important to understand PWM so you can know why analogWrite() is used in a digital pin.
If you check the documentation of analogWrite(), you are going to see that for most of the Arduino boards the default resolution is 8 bits, i.e, 0 to 255. To use all this 256 values to fade the led, a for loop is used. If you are not used to for loops, I strongly recommend you to read the documentation.
The for loop is programed to go through 256 iterations - the variable state going from 0 to 255. At each iteration, the value of state is incremented by 1. When state gets to be equal to 256, the loop ends itself. Every iteration verifies the value of input, which is just the state of the led when the button was released. This value is passed to the function only when the button is released, but the function uses it internally at every iteration. If input is LOW, it means that the led was off, so analogWrite(led, state); uses the value of state - increasing at every iteration - to fade the led, turning it on. If input is  HIGH, it means that the led was on, so analogWrite(led, 255-state); will fade the led turning it off. In this case, 255-state is just a way to set a range starting at 255 and ending at 0 while using a range that we already have - state - that starts at 0 and ends at 255.
The last line is just to set how long the fade action is going to last. To be more specific, delay(inertia) determine how long each iteration is going to last. So, when you set inertia to 10, will you see that fading will take 10(miliseconds)*255(iterations) + execution times =~ 2,6 seconds. I chose this name when I was coding and I'm not sure now if it was the better one. Maybe something like duration will have a more clear meaning. If you want to set the duration of the fade on your code - instead of the duration of each iteration - you can to something like
int fadeDuration = 3000; //duration of fade in ms

...


void fadeLed(boolean input, int inertia){
  for(int state=0;state<256;state++){
    if (input==LOW){
      analogWrite(led, state);
    }else{
      analogWrite(led, 255-state);
    }
    // delay in every iteration to achieve 3 seconds in total.
    delay(fadeDuration/256); //=(total fading duration)/(number of iterations)
  }
}

Hope this is clear. Don't forget to check the links I placed, they are all VERY important.

user2881: 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 && brightness<255 )
  {
    // fade in led 
    brightness = brightness + fadeamount;// increase brightness
    if( brightness>255 ) // check that brightness doesn't exceed 255
      brightness = 255;
    analogWrite(ledpin,brightness);
    delay (1);
  }
  else if( digitalRead(pushbutton)==LOW && brightness>0 )
  {
    // fade out led
    brightness = brightness - fadeamount;// decrease brightness
    if( brightness<0 ) // check that brightness doesn't go below 0
      brightness = 0;
    analogWrite(ledpin,brightness);
    delay (1);
  }

}


19511: <p>Guys please help me out in verifying my program for my project -- led fading effect using a push button.</p>
<p>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.</p>
<p>The main problem is coming in the programming part and rest all the circuit and components are fine.</p>
<p>THIS IS MY SKETCH :-</p>
<pre><code>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;
        }
   }
 }
</code></pre>
<p><strong>THIS IS THE CIRCUIT DIAGRAM</strong>
<a href="https://i.stack.imgur.com/LYfci.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LYfci.jpg" alt="" /></a></p>
 19513: <p>try this, its simple and easy to understand,</p>

<pre><code>int ledPin = 9;
int btnPin = 8;
int brightness = 0;
int btnState = LOW;
bool trigger = LOW;
void setup ()
{
  pinMode (ledPin, OUTPUT);
  pinMode (btnPin, INPUT);
}

void loop ()
{
  if ( digitalRead(btnState) &amp;&amp; !btnState)
  {
    if (trigger)
    {
      for (int i = 0; i &lt; 256; i++)
      {
        analogWrite(ledPin, i);
        // wait for 30 milliseconds to see the dimming effect
        delay(30);
      }
    }
    else
    {
      for (int i = 255; i &gt;= 0; i--)
      {
        analogWrite(ledPin, i);
        // wait for 30 milliseconds to see the dimming effect
        delay(30);
      }
    }
    trigger = !trigger;
  }
  btnState = digitalRead(btnState);
}
</code></pre>
 32573: <p>I changed things a bit and got a solution</p>

<pre><code>int led = 3;
int buttonpin = 8;
boolean waspressed = false;

int inertia = 10;

void setup (){ 
  pinMode(buttonpin,INPUT);
  pinMode(led,OUTPUT);
  analogWrite(led,0);
}

void loop(){
  if(digitalRead(buttonpin) == HIGH){
    waspressed = true;
  }else{
     if (waspressed == true){
       fadeLed(digitalRead(led), inertia);
       waspressed = false;
     }
  }
}

void fadeLed(boolean input, int inertia){
  for(int state=0;state&lt;256;state++){
    if (input==LOW){
      analogWrite(led, state);
    }else{
      analogWrite(led, 255-state);
    }
    delay(inertia);
  }
}
</code></pre>

<p>I created a function called <code>fadeLed</code> that receives two arguments: The actual state of the led and the delay - in miliseconds - that is going to determine how long the fading is going to last.</p>

<p>You can see that the rest of the code is pretty much the one you got in the other question you asked. This is because we already got a good code to control the behavior of the button, so its a good practice to keep it and just change what you want to be changed: to fade instead of blink. So we just change a single line and create a function to be called by that line. Functions are a good way to keep a clean code and are a good programming practice. They make the code easy to read and maintain.</p>

<p>The function <code>fadeLed()</code> uses <a href="https://www.arduino.cc/en/Tutorial/PWM" rel="nofollow noreferrer">PWM</a> to fade the led. In Arduino Uno, PWM is available in digital pins 3, 5, 6, 9, 10, and 11. This may change according to the model you are using. It's very important to understand PWM so you can know why <code>analogWrite()</code> is used in a digital pin.</p>

<p>If you check the documentation of <a href="https://www.arduino.cc/en/Reference/analogWrite" rel="nofollow noreferrer">analogWrite()</a>, you are going to see that for most of the Arduino boards the default resolution is 8 bits, i.e, 0 to 255. To use all this 256 values to fade the led, a <code>for</code> loop is used. If you are not used to for loops, I strongly recommend you to read the <a href="https://www.arduino.cc/en/reference/for" rel="nofollow noreferrer">documentation</a>.</p>

<p>The for loop is programed to go through 256 iterations - the variable <code>state</code> going from 0 to 255. At each iteration, the value of <code>state</code> is incremented by 1. When <code>state</code> gets to be equal to 256, the loop ends itself. Every iteration verifies the value of <code>input</code>, which is just the state of the led when the button was released. <strong>This value is passed to the function only when the button is released, but the function uses it internally at every iteration</strong>. If <code>input</code> is <code>LOW</code>, it means that the led was off, so <code>analogWrite(led, state);</code> uses the value of <code>state</code> - increasing at every iteration - to fade the led, turning it on. If <code>input</code> is  <code>HIGH</code>, it means that the led was on, so <code>analogWrite(led, 255-state);</code> will fade the led turning it off. In this case, <strong><code>255-state</code> is just a way to set a range starting at 255 and ending at 0 while using a range that we already have - <code>state</code> - that starts at 0 and ends at 255.</strong></p>

<p>The last line is just to set how long the fade action is going to last. To be more specific, <code>delay(inertia)</code> determine how long <strong>each iteration</strong> is going to last. So, when you set inertia to 10, will you see that fading will take 10(miliseconds)*255(iterations) + execution times =~ 2,6 seconds. I chose this name when I was coding and I'm not sure now if it was the better one. Maybe something like duration will have a more clear meaning. If you want to set the duration of the fade on your code - instead of the duration of each iteration - you can to something like</p>

<pre><code>int fadeDuration = 3000; //duration of fade in ms

...


void fadeLed(boolean input, int inertia){
  for(int state=0;state&lt;256;state++){
    if (input==LOW){
      analogWrite(led, state);
    }else{
      analogWrite(led, 255-state);
    }
    // delay in every iteration to achieve 3 seconds in total.
    delay(fadeDuration/256); //=(total fading duration)/(number of iterations)
  }
}
</code></pre>

<p>Hope this is clear. Don't forget to check the links I placed, they are <em>all</em> VERY important.</p>
 2881: <p>The 1k (pulldown-)resistor must be placed at the other side of the button (the pin8 side, not the 5v side).</p>

<p>The led needs a resistor, to limit the current.</p>

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

<pre><code>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);
  }

}
</code></pre>
