user39821: I'm fairly new to Arduino and am trying to use a push button to toggle an LED on and off using a momentary press of the button. This code is lifted straight off the tutorial on the Arduino website but is not working for me. It seems to send the LED on a loop - switching between on and off by itself, and pressing the button pauses the cycle. 
Any ideas as to why it is doing that?
int buttonPin = 2;         // the number of the input pin
int ledPin = 13;       // the number of the output pin

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);

}

void loop()
{


  reading = digitalRead(buttonPin);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
if (reading == HIGH  && previous == LOW && millis() - time > debounce) {
  if (state == HIGH)
    state = LOW;
  else 
    state = HIGH;
time = millis();    
}

else 
  if (state == LOW)
    state = LOW;
  else 
    state = HIGH;
  digitalWrite(ledPin, state);
 //Serial.println(buttonPin);
  // 
 Serial.println(reading);
previous = reading;
} 


user33871: pinMode(buttonPin, INPUT);

Do you have an external pull-down resistor?  Without one, the pin is floating when the button isn't pressed so it might read anything and you may be picking up spurious noise.  
It is much more common to wire a button so that it connects to ground and reads LOW when pressed.  If you wire it that way, you can use the internal pull-up resistors by using pinMode(pin, INPUT_PULLUP) and you won't need any external resistor for the button.  

user-1: I tested your sketch and it works. I simplified it; it's the same logic, but in a more concise way:
void loop()
{
    reading = digitalRead(buttonPin);

    // if the input just went from LOW and HIGH and we've waited long enough
    // to ignore any noise on the circuit, toggle the output pin and remember
    // the time
    if(reading == HIGH  && previous == LOW && millis() - time > debounce) {
        state = !state;
        time = millis();
    }

    digitalWrite(ledPin, state);
    Serial.println(reading);
    previous = reading;
}

setup is still the same.

39821: <p>I'm fairly new to Arduino and am trying to use a push button to toggle an LED on and off using a momentary press of the button. This code is lifted straight off the tutorial on the Arduino website but is not working for me. It seems to send the LED on a loop - switching between on and off by itself, and pressing the button pauses the cycle. </p>

<p>Any ideas as to why it is doing that?</p>

<pre><code>int buttonPin = 2;         // the number of the input pin
int ledPin = 13;       // the number of the output pin

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);

}

void loop()
{


  reading = digitalRead(buttonPin);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
if (reading == HIGH  &amp;&amp; previous == LOW &amp;&amp; millis() - time &gt; debounce) {
  if (state == HIGH)
    state = LOW;
  else 
    state = HIGH;
time = millis();    
}

else 
  if (state == LOW)
    state = LOW;
  else 
    state = HIGH;
  digitalWrite(ledPin, state);
 //Serial.println(buttonPin);
  // 
 Serial.println(reading);
previous = reading;
} 
</code></pre>
 33871: <pre><code>pinMode(buttonPin, INPUT);
</code></pre>

<p>Do you have an external pull-down resistor?  Without one, the pin is floating when the button isn't pressed so it might read anything and you may be picking up spurious noise.  </p>

<p>It is much more common to wire a button so that it connects to ground and reads LOW when pressed.  If you wire it that way, you can use the internal pull-up resistors by using pinMode(pin, INPUT_PULLUP) and you won't need any external resistor for the button.  </p>
 -1: <p>I tested your sketch and it works. I simplified it; it's the same logic, but in a more concise way:</p>

<pre><code>void loop()
{
    reading = digitalRead(buttonPin);

    // if the input just went from LOW and HIGH and we've waited long enough
    // to ignore any noise on the circuit, toggle the output pin and remember
    // the time
    if(reading == HIGH  &amp;&amp; previous == LOW &amp;&amp; millis() - time &gt; debounce) {
        state = !state;
        time = millis();
    }

    digitalWrite(ledPin, state);
    Serial.println(reading);
    previous = reading;
}
</code></pre>

<p><code>setup</code> is still the same.</p>
