Problem with "if" and "else" commands

I am writing a simple program that alternates two leds on and off if a button is not pressed. If it is pressed I want it to make both LEDs high. It doesn't work... just constantly alternates the leds.

int potPin = 2;    // select the input pin for the potentiometer
int led1 = 11;     // select the pin for the LED1
int led2 = 12;      // select the pin for the LED2
int pot = 0;       // variable to store the value coming from the sensor
int button = 10;    //button pin


void setup() {
  pinMode(led1, OUTPUT);  // declare the ledPin as an OUTPUT
  pinMode(led2, OUTPUT);  // declare the ledPin as an OUTPUT
  pinMode(button, INPUT); // button is an input
}

void loop() {

  if (digitalRead(button) == LOW)
  {
    pot = analogRead(potPin);    // read the value from the sensor
    digitalWrite(led1, HIGH);      // turn the ledPin on
    digitalWrite(led2, LOW);
    delay(pot);                  // stop the program for some time
    pot = analogRead(potPin);
    digitalWrite(led1, LOW);      // turn the ledPin off
    digitalWrite(led2, HIGH);
    delay(pot);                  // stop the program for some time
  }

  else
  {
    digitalWrite(led1, HIGH);      // turn the ledPin on
    digitalWrite(led2, HIGH);
  }

}

Have you wired you switch with a pull-up or pull-down resistor?

it is a onboard button from a makershield. I am also using the onboard potentiometer for the sensor to control delay.

I don't know what that is, can you provide a link?

your right... i connect another switch with a pulldown resistor and it works... whew. I guess the onboard doesnt have resistors. http://www.makershed.com/ProductDetails.asp?ProductCode=MSMS01&Click=37845

dls13dls:
it is a onboard button from a makershield. I am also using the onboard potentiometer for the sensor to control delay.

Without knowing the wiring details of your switch, including pull-up/down details, you may be having a problem with a ‘floating’ input pin and that kind of takes all the logic out of a otherwise properly written program. The hardware has to be correct before the software can function correctly. That’s why you you were asked. So maybe a link to a schematic drawing for a “makershield” would answer the question about switch wiring?

Lefty

EDIT: I see the problem got solved while I was posting this post. But you see it was the right question to ask you!

http://www.makershed.com/v/vspfiles/assets/images/makershield.pdf Schematic link

so when i use analogRead it has to have a resistor on the switch.. ok got it

dls13dls: http://www.makershed.com/v/vspfiles/assets/images/makershield.pdf Schematic link

so when i use analogRead it has to have a resistor on the switch.. ok got it

It has nothing to do with analogRead statements or analog input pins. Digital input pins wired to passive switch contacts have to have to be pulled up or down with resistors.

Lefty

dls13dls: so when i use analogRead it has to have a resistor on the switch.. ok got it

That statement doesn't really make sense. Pull-ups and Pull-downs prevent the pin from being in a floating state. If a pin is unconnected, it isn't defined as a HIGH or LOW, it is whatever noise it picks up. If you analogRead() a floating pin you will get random numbers. If you digitalRead() a floating pin you will randomly get HIGH or LOW.

Looking at the shield's schematic, the Button appears to be floating. So you can either wire in a resistor or use the Arduino's built-in Pull-Up resistor. (If you do that, then your logic needs to be inverted.)

my apologies... i meant digitalRead Ive had the arduino for a week (my first one). I appreciate the help guys.