user40031: I've tried to modify this code, but everything I tried failed.
Where am I going wrong?
All I want to happen is when button 1 is pressed LED 1 fades up and when released LED 1 fades out and the same again with the 2nd LED when button 2 is pressed LED 2 fades up and when released it fades out.
int ledpin = 10;  
int ledpin1 = 11;  
int pushbutton = 7;  
int pushbutton1 = 8;  

int brightness = 255;  
int fadeamount = 1;  

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

void loop ()  
{   
  if( digitalRead(pushbutton)==HIGH && brightness<255 )  
  {  
    // fade in ledpin   
    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 ledpin  
    brightness = brightness - fadeamount;// decrease brightness  
    if( brightness<0 ) // check that brightness doesn't go below 0  
      brightness = 0;  
    analogWrite(ledpin,brightness);  
    delay (1);  

  }  


  if( digitalRead(pushbutton1)==HIGH && brightness<255 )  
  {  
    // fade in ledpin1   
    brightness = brightness + fadeamount;// increase brightness  
    if( brightness>255 ) // check that brightness doesn't exceed 255  
      brightness = 255;  
    analogWrite(ledpin1,brightness);  
    delay (1);  
  }  
  else if( digitalRead(pushbutton1)==LOW && brightness>0 )  
  {  
    // fade out ledpin1  
    brightness = brightness - fadeamount;// decrease brightness  
    if( brightness<0 ) // check that brightness doesn't go below 0  
      brightness = 0;  
    analogWrite(ledpin1,brightness);  
    delay (1);  

  }  

}


user359: Create 2 sets of brightness, fadeamount variables, one for each button/LED. You can put them in a struct to make things simpler:
struct brightness {
    int brightness; 
    int fadeamount;
    int ledpin; 
    int pushbutton;
};

struct brightness set1;
struct brightness set2;

Then initialize them in init.
The LED goes from full brightness to off in 0.255 seconds. That is pretty quick.
You can add a millis() based timer on the brightness change:
struct brightness {
    int brightness; 
    int fadeamount;
    int ledpin; 
    int pushbutton;

    //add the following
    unsigned long timeChanged;
    unsigned long timeBetweenChanges;
};

void handleButtonFade(struct brightness* set){
      if( millis() - set.timeChanged < set.timeBetweenChanges)
          return;

      if(digitalRead(set1.pushbutton)==HIGH && set.brightness<255 )
      {
        // fade in ledpin 
        set.brightness = set.brightness + set.fadeamount;// increase brightness
        if( set.brightness>255 ) // check that brightness doesn't exceed 255
          set.brightness = 255;
        analogWrite(set.ledpin,set.brightness);
      }
      else if( digitalRead(set.pushbutton)==LOW && set.brightness>0 )
      {
        // fade out ledpin
        set.brightness = set.brightness - set.fadeamount;// decrease brightness
        if( set.brightness<0 ) // check that brightness doesn't go below 0
          set.brightness = 0;
        analogWrite(set.ledpin,set.brightness);

      }

}

void loop(){
    handleButtonFade(&set1);
    handleButtonFade(&set2);
}


40031: <p>I've tried to modify this code, but everything I tried failed.<br>
Where am I going wrong?</p>

<p>All I want to happen is when button 1 is pressed LED 1 fades up and when released LED 1 fades out and the same again with the 2nd LED when button 2 is pressed LED 2 fades up and when released it fades out.</p>

<pre class="lang-cpp prettyprint-override"><code>int ledpin = 10;  
int ledpin1 = 11;  
int pushbutton = 7;  
int pushbutton1 = 8;  

int brightness = 255;  
int fadeamount = 1;  

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

void loop ()  
{   
  if( digitalRead(pushbutton)==HIGH &amp;&amp; brightness&lt;255 )  
  {  
    // fade in ledpin   
    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 ledpin  
    brightness = brightness - fadeamount;// decrease brightness  
    if( brightness&lt;0 ) // check that brightness doesn't go below 0  
      brightness = 0;  
    analogWrite(ledpin,brightness);  
    delay (1);  

  }  


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

  }  

}
</code></pre>
 359: <p>Create 2 sets of brightness, fadeamount variables, one for each button/LED. You can put them in a struct to make things simpler:</p>

<pre class="lang-cpp prettyprint-override"><code>struct brightness {
    int brightness; 
    int fadeamount;
    int ledpin; 
    int pushbutton;
};

struct brightness set1;
struct brightness set2;
</code></pre>

<p>Then initialize them in init.</p>

<p>The LED goes from full brightness to off in 0.255 seconds. That is pretty quick.</p>

<p>You can add a <code>millis()</code> based timer on the brightness change:</p>

<pre class="lang-cpp prettyprint-override"><code>struct brightness {
    int brightness; 
    int fadeamount;
    int ledpin; 
    int pushbutton;

    //add the following
    unsigned long timeChanged;
    unsigned long timeBetweenChanges;
};

void handleButtonFade(struct brightness* set){
      if( millis() - set.timeChanged &lt; set.timeBetweenChanges)
          return;

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

      }

}

void loop(){
    handleButtonFade(&amp;set1);
    handleButtonFade(&amp;set2);
}
</code></pre>
