Have a look at the StateChangeDetection example in the IDE (File->Examples->02 Digital ->StateChangeDetection). It will show you have to successfully detect your switch changing state and then you can react to it.
Here is a small sketch to illustrate using the state change detection method to toggle the state of a pin. Note that the switch is wired from the input (set to pinMode INPUT_PULLUP) to ground or active LOW versus the active HIGH switch in the tutorial.
// This is to illustrate using tne state change detectin method to
// toggle the state of a pin (Pin 13, the on board LED) with
// a momentary switch (pushbutton) is wired from pin 8 to ground
// by C Goulding aka groundFungus
// this constant won't change:
const int buttonPin = 8; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
boolean buttonState = 0; // current state of the button
boolean lastButtonState = 0; // previous state of the button
void setup()
{
// initialize the button pin as a input with internal pullup enabled
pinMode(buttonPin, INPUT_PULLUP);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop()
{
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState)
{
if (buttonState == LOW)
{
// if the current state is LOW then the button
// went from off to on:
digitalWrite(ledPin, !digitalRead(ledPin)); // toggle the output
}
delay(20);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}