Toggle on off switch

I wrote a code recently for control 4 LED, from 4 separate switches. In this project I want to use only single input pin and separate 4 output pins. As input pin I used A0 anlog pin and for out put 4 digitlal pins. My problem is when I turn on all LEDs and when I want turn off or on one of the LED it need press two times to do desired function. How will I over come this problem? Any suggestion ?

//initialize and declare variables
const int led1Pin = 3; //led attached to this pin
const int led2pin = 4;
const int led3pin = 5;
const int led4pin = 6;
const int buttonPin = A0; //push button attached to this pin

int buttonState = LOW; //this variable tracks the state of the button, low if not pressed, high if pressed
int ledState = -1; //this variable tracks the state of the LED, negative if off, positive if on
int buttonvalue;

long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers


void setup() {

//set the mode of the pins...
pinMode(led1Pin, OUTPUT);
pinMode(led2pin,OUTPUT);
pinMode(led3pin,OUTPUT);
pinMode(led4pin,OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);

}//close void setup


void loop() {
Serial.println(analogRead(A0));
buttonvalue=analogRead(A0);

//sample the state of the button - is it pressed or not?
buttonState = digitalRead(buttonPin);

//filter out any noise by setting a time buffer
if ( (millis() - lastDebounceTime) > debounceDelay) {

  //if the button has been pressed, lets toggle the LED from "off to on" or "on to off"
  if ( (buttonState == HIGH) && (ledState < 0)&& (buttonvalue<=1016 && buttonvalue>=1010) ){//led1

    digitalWrite(led1Pin, HIGH); //turn LED on
    ledState = -ledState; //now the LED is on, we need to change the state
    lastDebounceTime = millis(); //set the current time
  }
  else if ( (buttonState == HIGH) && (ledState > 0) && (buttonvalue<=1016 && buttonvalue>=1010)) {

    digitalWrite(led1Pin, LOW); //turn LED off
    ledState = -ledState; //now the LED is off, we need to change the state
    lastDebounceTime = millis(); //set the current time
  }//close if/else
  else if ( (buttonState == HIGH) && (ledState < 0)&& (buttonvalue<=996 && buttonvalue>=990) ){//led2

    digitalWrite(led2pin, HIGH); //turn LED on
    ledState = -ledState; //now the LED is on, we need to change the state
    lastDebounceTime = millis(); //set the current time
    }
  else if ( (buttonState == HIGH) && (ledState > 0)&& (buttonvalue<=996 && buttonvalue>=990) ) {

    digitalWrite(led2pin, LOW); //turn LED off
    ledState = -ledState; //now the LED is off, we need to change the state
    lastDebounceTime = millis(); //set the current time
  }//close if/else
  else if ( (buttonState == HIGH) && (ledState < 0)&& (buttonvalue<=936 && buttonvalue>=926) ){//led3

    digitalWrite(led3pin, HIGH); //turn LED on
    ledState = -ledState; //now the LED is on, we need to change the state
    lastDebounceTime = millis(); //set the current time
  }
  else if ( (buttonState == HIGH) && (ledState > 0) && (buttonvalue<=936 && buttonvalue>=926)) {

    digitalWrite(led3pin, LOW); //turn LED off
    ledState = -ledState; //now the LED is off, we need to change the state
    lastDebounceTime = millis(); //set the current time
  }//close if/else
  else if ( (buttonState == HIGH) && (ledState < 0)&& (buttonvalue<=896 && buttonvalue>=886) ){//led4

    digitalWrite(led4pin, HIGH); //turn LED on
    ledState = -ledState; //now the LED is on, we need to change the state
    lastDebounceTime = millis(); //set the current time
  }
  else if ( (buttonState == HIGH) && (ledState > 0) && (buttonvalue<=896 && buttonvalue>=886)) {

    digitalWrite(led4pin, LOW); //turn LED off
    ledState = -ledState; //now the LED is off, we need to change the state
    lastDebounceTime = millis(); //set the current time
  }//close if/else

}//close if(time buffer)

}//close void loop

toggle_on_and_off_switch1.ino (3.57 KB)

const int buttonPin = A0; //push button attached to this pin
  pinMode(buttonPin, INPUT);
  buttonvalue = analogRead(A0);
  buttonState = digitalRead(buttonPin);

There seems to be some confusion as to whether A0 is a digital pin or an analogue pin

I also note that you only have one ledState variable but you have 4 LEDs. Please explain how the state of 4 LEDs can be tracked by the value of one int in your program. It is possible to do it but only by using individual bits in the int, which you are not doing.

As your suggestion for add 4 separate int (variables) for store LED states seperately one for each LED, it works! Thank you for your valuable suggestion.

//initialize and declare variables
const int led1Pin = 3; //led attached to this pin
const int led2pin = 4;
const int led3pin = 5;
const int led4pin = 6;
const int buttonPin = A0; //push button attached to this pin

int buttonState = LOW; //this variable tracks the state of the button, low if not pressed, high if pressed
int led1State = -1; //this variable tracks the state of the LED, negative if off, positive if on
int led2State = -1;
int led3State = -1;
int led4State = -1;
int buttonvalue;

long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers


void setup() {

  //set the mode of the pins...
  pinMode(led1Pin, OUTPUT);
  pinMode(led2pin,OUTPUT);
  pinMode(led3pin,OUTPUT);
  pinMode(led4pin,OUTPUT);
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);

}//close void setup


void loop() {
  Serial.println(analogRead(A0));
  buttonvalue=analogRead(A0);

  //sample the state of the button - is it pressed or not?
  buttonState = digitalRead(buttonPin);

  //filter out any noise by setting a time buffer
  if ( (millis() - lastDebounceTime) > debounceDelay) {

    //if the button has been pressed, lets toggle the LED from "off to on" or "on to off"
    if ( (buttonState == HIGH) && (led1State < 0)&& (buttonvalue<=1016 && buttonvalue>=1010) ){//led1

      digitalWrite(led1Pin, HIGH); //turn LED on
      led1State = -led1State; //now the LED is on, we need to change the state
      lastDebounceTime = millis(); //set the current time
    }
    else if ( (buttonState == HIGH) && (led1State > 0) && (buttonvalue<=1016 && buttonvalue>=1010)) {

      digitalWrite(led1Pin, LOW); //turn LED off
      led1State = -led1State; //now the LED is off, we need to change the state
      lastDebounceTime = millis(); //set the current time
    }//close if/else
    else if ( (buttonState == HIGH) && (led2State < 0)&& (buttonvalue<=996 && buttonvalue>=990) ){//led2

      digitalWrite(led2pin, HIGH); //turn LED on
      led2State = -led2State; //now the LED is on, we need to change the state
      lastDebounceTime = millis(); //set the current time
      }
    else if ( (buttonState == HIGH) && (led2State > 0)&& (buttonvalue<=996 && buttonvalue>=990) ) {

      digitalWrite(led2pin, LOW); //turn LED off
      led2State = -led2State; //now the LED is off, we need to change the state
      lastDebounceTime = millis(); //set the current time
    }//close if/else
    else if ( (buttonState == HIGH) && (led3State < 0)&& (buttonvalue<=936 && buttonvalue>=926) ){//led3

      digitalWrite(led3pin, HIGH); //turn LED on
      led3State = -led3State; //now the LED is on, we need to change the state
      lastDebounceTime = millis(); //set the current time
    }
    else if ( (buttonState == HIGH) && (led3State > 0) && (buttonvalue<=936 && buttonvalue>=926)) {

      digitalWrite(led3pin, LOW); //turn LED off
      led3State = -led3State; //now the LED is off, we need to change the state
      lastDebounceTime = millis(); //set the current time
    }//close if/else
    else if ( (buttonState == HIGH) && (led4State < 0)&& (buttonvalue<=896 && buttonvalue>=886) ){//led4

      digitalWrite(led4pin, HIGH); //turn LED on
      led4State = -led4State; //now the LED is on, we need to change the state
      lastDebounceTime = millis(); //set the current time
    }
    else if ( (buttonState == HIGH) && (led4State > 0) && (buttonvalue<=896 && buttonvalue>=886)) {

      digitalWrite(led4pin, LOW); //turn LED off
      led4State = -led4State; //now the LED is off, we need to change the state
      lastDebounceTime = millis(); //set the current time
    }//close if/else

  }//close if(time buffer)

}//close void loop}//close void loop

Now improve it some more by making the ledState variables byte instead of int (why waste memory ?) with values of HIGH or LOW to match that of the associated LED

Then consider improving it even more by using arrays to hold values to avoid repeating code

Why are you using A0 as both an analogue and digital pin ?
I assume from the code that you are using a resistor ladder connected to the 4 buttons and A0 to derive the values, so using it as an analogue pin would be the most appropriate