IF ELSE -- again!

Using the button example,

can the IF ELSE execute more then one command?
Even after I changed the code the example part worked ok just the second command did not
ledPin1 was HIGH all the time but ledPin worked with the button

// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
const int ledPin1 = 12; // My added LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin1, LOW);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
digitalWrite(ledPin1, HIGH);
}
}

I don't spot anything wrong with the program-- is your wiring correct? Describe how you have it wired to to pin 12...

John

// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin,HIGH); // <<<<<<< Add this line

This code adds an internal pullup resistor to +5Volts.
Or, you can add an external resistor to the i/p pin if you want.

LarryD:
This code adds an internal pullup resistor to +5Volts.
Or, you can add an external resistor to the i/p pin if you want.

Not a bad idea, but shouldn't be relevant to his problem IMO...

Here is my circuit the resistors are a little bit small but is what I happened to have on hand

Sorry, I don't see a problem. Check your wiring carefully--is this on a solderless breadboard, or what...

If you add the line I said to in my response #2 above, it will fix your problem.

What is happening is the i/p is floating and the sketch is reading noise,
hence the led is really pulsing on and off at a fast rate.
If you have a logic probe you will detect this.

I would write this problem, so it only changed the output pin states when the button state actually changed.

adding the pullup command fixed the problem but I do see if I was actually using this circuit in a project I would need to add debounce code! the off LED still tries to light very very dimly every so often when the button switch is down.

Thanks

Dan

In this project, not debouncing the switch is really not much of a problem.

In a project which counts the number of pushes on a switch, debounce is very important since one push/release could have hundreds of HIGH/LOW transitions.
Note: you can debounce a switch using software and/or in hardware.