Using 2 push button to control 2 leds

I have written a program but only one switch works.
the program i have written is

const int buttonPin = 0;//pushbutton pin
const int buttonPin1 = 1;// pushbutton pin
const int ledPin = 2; // LED pin
const int ledPin1 = 3;
int ledPinState = HIGH; // the current state of the output pin
int ledPin1State = HIGH;
int buttonPinState; // the current reading from the input pin
int buttonPin1State;
int lastButtonPinState = LOW; // the previous reading from the input pin
int lastButtonPin1State = LOW;
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers

void setup() {
pinMode(buttonPin, INPUT);
pinMode(buttonPin1, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);

// set initial LED state
digitalWrite(ledPin, ledPinState);
digitalWrite(ledPin1, ledPin1State);
}

void loop() {
{
int reading = digitalRead(buttonPin);
if (reading != lastButtonPinState) {
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay && reading != buttonPinState ) {
{
buttonPinState = reading;
if (buttonPinState == HIGH) {
ledPinState = !ledPinState;
}
}
}
lastButtonPinState = reading;
}
{
int reading =digitalRead(buttonPin1);
if(reading!=lastButtonPin1State){
lastDebounceTime=millis();
}

if ((millis() - lastDebounceTime) > debounceDelay && reading != buttonPin1State ) {
{
buttonPin1State=reading;
if(buttonPin1State==HIGH){
ledPinState = !ledPin1State;
}
}
}
lastButtonPin1State=reading;
}

// set the LED:
digitalWrite(ledPin, ledPinState);
digitalWrite(ledPin1, ledPin1State);

}

const int buttonPin = 0;//pushbutton pin
const int buttonPin1 = 1;//  pushbutton pin
const int ledPin = 2;      //  LED pin
const int ledPin1 = 3;
int ledPinState = HIGH;         // the current state of the output pin
int ledPin1State = HIGH;
int buttonPinState;             // the current reading from the input pin
int buttonPin1State;
int lastButtonPinState = LOW;   // the previous reading from the input pin
int lastButtonPin1State = LOW;
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin,  INPUT);
  pinMode(buttonPin1, INPUT);
  pinMode(ledPin,    OUTPUT);
  pinMode(ledPin1,   OUTPUT);

  // set initial LED state
  digitalWrite(ledPin,   ledPinState);
  digitalWrite(ledPin1, ledPin1State);
}

void loop() {
  {  
    int reading = digitalRead(buttonPin);
    if (reading != lastButtonPinState) {
      lastDebounceTime = millis();
    } 

    if ((millis() - lastDebounceTime) > debounceDelay && reading != buttonPinState ) {
      {
        buttonPinState = reading;
        if (buttonPinState == HIGH) {
          ledPinState = !ledPinState;
        }
      } 
    }
    lastButtonPinState = reading;
  
  
    int reading =digitalRead(buttonPin1);
    if(reading!=lastButtonPin1State){
      lastDebounceTime=millis();
    }

    if ((millis() - lastDebounceTime) > debounceDelay && reading != buttonPin1State ) {
      {
        buttonPin1State=reading;
        if(buttonPin1State==HIGH){
          ledPinState = !ledPin1State;
        }
      }
    }
    lastButtonPin1State=reading;
  }

  // set the LED:
  digitalWrite(ledPin, ledPinState);
  digitalWrite(ledPin1, ledPin1State);

}

use brackets properly . try above code, Let me know result.

There code tag option avilable here to post your code, Use autoformat from IDe for proper arrangement

Sry AMPS-N the code which you suggested me in #2 is not working. Both the leds are glowing at the initial stage but afer pressing the pushbutton the leds are not going off.

please elobarte question . So we can solve question.

What you expected ? based on question tag we can't write program??

Please mention the function of each button each LED>

When to turn on /when to turn off . What is status of LED when started??
what status when it is off??

Have you looked at my code yet?

I want to control 2 leds with 2 different push buttons.
AND Paul__B i am trying to solve the code given by you.

Hi, AMPS when i start the arduino the LEDS are switched on. I want led1 to go off when i press the pushbutton1 and if i press it(pushbutton1) again it(led1) must get switched off. And same is applied with pushbutton2 and led2 they must operate irrespective of each other.

I want led1 to go off when i press the pushbutton1 and if i press it(pushbutton1) again it(led1) must get switched off. And same is applied with pushbutton2 and led2 they must operate irrespective of each other.

what does it mean

when you press first time led already goes off, so why you want to switch off again??

You mean to say if button pressed once led goes off , next time when pressed led goes ON.similarly alternating cycle.

I have written a program but only one switch works.
the program i have written is

const int buttonPin = 0;//pushbutton pin
const int buttonPin1 = 1;//  pushbutton pin

These maybe the serial tx/rx pins, so you may need to use other pins for the button pins.

Hi,
As Zoomcat says, not a good idea to use the 0(RX) and 1(TX) pins, as they are used for all serial comms, you must have pleanty of others you could use.

Hope it helps, Regards.

Mel.

Hi,
press button 1 toggles LED1 on and off
press button 2 toggles LED2 on and off

Am I right.

Tom....... :slight_smile:

Why is this thread pinned? It's not a tutorial or a how to use the forum kind of post; it's "just another thread"...

edit.... ok, it's un-pinned now