Don't use Pin 1 unless you are out of other options. Pin 1 and Pin 0 are the Serial I/O pins and are useful for debugging.
Start with a debounce example to clean up the button pushes:
/// constants won't change. They're used here to set pin numbers:
const int buttonPin = 5; // the number of the pushbutton pin
const int led1Pin = 2; // the number of the LED1 pin
const int led2Pin = 3; // the number of the LED2 pin
const int led3Pin = 4; // the number of the LED3 pin
// Variables will change:
int ledState = 0; // the current state of the LEDs
int buttonState; // the latest stable state of the input pin
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
}
void loop()
{
static int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
static unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
const unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
int currentButtonState = digitalRead(buttonPin);
if (currentButtonState != lastButtonState) {
lastDebounceTime = millis();
lastButtonState = currentButtonState;
}
if ((millis() - lastDebounceTime) > debounceDelay &&
currentButtonState != buttonState) {
// We have a new and different stable button state
buttonState = currentButtonState;
if (buttonState == LOW) {
// The new state is "Pressed"
ledState++;
if (ledState > 3)
ledState = 1; // After 3, go back to 1
if (ledState == 1) {
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
}
if (ledState == 2) {
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, HIGH);
digitalWrite(led3Pin, LOW);
}
if (ledState == 3) {
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, HIGH);
}
}
}
}