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);
}
}
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.
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.
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.