Button reading not going off I made a circuit which in my intentions would allow me to toggle a LED dimming loop.

Problem is that once I push the button the first time pushing it a second time doesn't toggle the LED loop off.

Here is the code:

const int LED = 9; // the pin for the LED
const int BUTTON = 7;


int val = LOW;

int old_val = LOW;
int state = 0;
int i = 0;

void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(BUTTON, INPUT);

}

void loop()
{
  val = digitalRead(BUTTON);

  if ((val == HIGH) &amp;&amp; (old_val==LOW))
  {
    state = 1 - state;
    delay(10);
  }

  old_val = val;

  if (state == 1)
  {
    for (i = 0; i &lt; 255; i++)  // loop from 0 to 254 (fade in)
    {
      analogWrite(LED, i); // set the LED brightness
      delay(10);           // Wait 10ms because analogWrite
      // is instantaneous and we would
      // not see any change
    }

    for (i = 255; i &gt; 0; i--) // loop from 255 to 1 (fade out)
    { 
      analogWrite(LED, i); // set the LED brightness
      delay(10);           // Wait 10ms  
    }
  } else
  {
    analogWrite(LED, 0);
  }

  if ((val == LOW) &amp;&amp; (old_val == HIGH)) 
  {
    delay(10);
  }
}


Here is the circuit scheme:



I would be interested both in learning what the problem is AND learning any mental methods I can apply to debug in such situations.
While the led is fading in and out (which takes 5 seconds), the state of the button is never checked. So you'd need to have the button released for at least 5 seconds (so old_val becomes LOW), and then pressed exactly we the led is faded out (again, 5 seconds later). So try keeping the button pressed for at least 5 seconds.

You probably want to rewrite your code, so the button is more 'responsive'. Below I added an example of how to do this.

const int LED = 9; // the pin for the LED
const int BUTTON = 7;


int val = LOW;
int old_val = LOW;

bool fadeLed = false;
int brightness = 0;//current pwm value
int direction = 1;// 1 is brightness is increasing, -1 if brightness is decreasing 

void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(BUTTON, INPUT);
}

void loop()
{

  /* check the button */
  val = digitalRead(BUTTON);
  if ((val == HIGH) &amp;&amp; (old_val==LOW))
  {
    // button pressed ( called only once per press )
    fadeLed = !fadeLed;// invert boolean
  }
  old_val = val;

  /* update the leds brightness */
  if( fadeLed )
  {
    // fade led in and out
    if( brightness==0 )
      direction = 1;//change direction is minimum value reached
    if( brightness==255 )
      direction = -1;//change direction is maximum value reached
    brightness += direction;// increase/decrease brightness
  }
  else
  {
    // fadeLed is disabled, so only decrease brightness till it reaches zero
    if( brightness&gt;0 )
      brightness--;
  }

  analogWrite(LED, brightness); // set the LED brightness
  delay(10);           // Wait 10ms because analogWrite
                       // is instantaneous and we would
                       // not see any change

}


Any questions about the code; just let me know.