Duplicating debounce sketch

Hello I have an arduino uno and am trying to individually switch a second LED with a Second switch independently.
So I took the debounce example and then duplicated all the lines I could think would need it but unfortunately the leds switch in random order for the first few presses of either button then just pin 13 switches on and off some how it seems to b crossing over in the loop can any one point me in the right direction please ?
To recap I am trying to use a momentary switch on pin 1 to toggle pin13 on and off and another on pin2 to toggle pin 12 on and off

// constants won't change. They're used here to

// set pin numbers:

const int buttonPin = 2; // the number of the pushbutton pin

const int buttonPin1 = 3;

const int ledPin = 13; // the number of the LED pin

const int ledPin1 = 12;

// Variables will change:

int ledState = HIGH; // the current state of the output pin

int buttonState; // the current reading from the input pin

int lastButtonState = LOW; // the previous reading from the input pin

int ledState1 = HIGH;

int buttonState1;

int lastButtonState1 = LOW;

// the following variables are unsigned long's because the time, measured in miliseconds,

// will quickly become a bigger number than can be stored in an int.

unsigned long lastDebounceTime = 0; // the last time the output pin was toggled

unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers

unsigned long lastDebounceTime1 = 1; // the last time the output pin was toggled

unsigned long debounceDelay1 = 49; // the debounce time; increase if the output

void setup() {

pinMode(buttonPin, INPUT);

pinMode(ledPin, OUTPUT);

pinMode(buttonPin1, INPUT);

pinMode(ledPin1, OUTPUT);

// set initial LED state

digitalWrite(ledPin, ledState);

digitalWrite(ledPin1, ledState1);

}

void loop() {

// read the state of the switch into a local variable:

int reading = digitalRead(buttonPin);

// check to see if you just pressed the button

// (i.e. the input went from LOW to HIGH), and you've waited

// long enough since the last press to ignore any noise:

// If the switch changed, due to noise or pressing:

if (reading != lastButtonState) {

// reset the debouncing timer

lastDebounceTime = millis();

}

if ((millis() - lastDebounceTime) > debounceDelay) {

// whatever the reading is at, it's been there for longer

// than the debounce delay, so take it as the actual current state:

// if the button state has changed:

if (reading != buttonState) {

buttonState = reading;

// only toggle the LED if the new button state is HIGH

if (buttonState == HIGH) {

ledState = !ledState;

}

}

}

// set the LED:

digitalWrite(ledPin, ledState);

// save the reading. Next time through the loop,

// it'll be the lastButtonState:

lastButtonState = reading;

// read the state of the switch into a local variable:

int reading1 = digitalRead(buttonPin1);

// check to see if you just pressed the button

// (i.e. the input went from LOW to HIGH), and you've waited

// long enough since the last press to ignore any noise:

// If the switch changed, due to noise or pressing:

if (reading1 != lastButtonState1) {

// reset the debouncing timer

lastDebounceTime1 = millis();

}

if ((millis() - lastDebounceTime1) > debounceDelay1) {

// whatever the reading is at, it's been there for longer

// than the debounce delay, so take it as the actual current state:

// if the button state has changed:

if (reading1 != buttonState1) {

buttonState1= reading1;

// only toggle the LED if the new button state is HIGH

if (buttonState1 == HIGH) {

ledState1 = !ledState1;

}

}

}

// set the LED:

digitalWrite(ledPin1, ledState1);

// save the reading. Next time through the loop,

// it'll be the lastButtonState:

lastButtonState1 = reading1;

}

Can you please edit your post to include code tags. See How to use the forum.

And

what's

up

with

all

those

blank

lines

?

And:
Tip1: If you start numbering variables, arrays. Always arrays.

Tip2: If you get how the debounce works just make you life easy and grab a library like Bounce2 to do all the heavy lifting :wink:

Please do us all a favour, including yourself.

Remove the superfluous blank lines from your code.
Auto Format it in the IDE
Post it here again but use code tags this time. See read this before posting a programming question

Tip1: If you start numbering variables, arrays. Always arrays.

I’ll offer an alternate opinion here. When the number of variables in a set exceeds two, arrays! For two, I think arrays are a bit overkill. I’d use arrays, but not everyone needs to.

OP: One thing I do insist on, though, is that if you start numbering variables in a set, you number ALL of them.
ledState and ledState1 just looks stupid. ledState1 and ledState2 make it look like you are not just trying to bandaid your code.

What that forces you to do is examine EVERYWHERE where you use a variable, to make sure that you are not using variable where you meant to use variable1. If you replace variable with variable1 or variable2 consciously, the compiler will tell you where you missed one, since variable will not be defined. If you meant to replace variable with variable1 in some code you copied, but miss it, the compiler will be perfectly happy, because variable still exists, but the code will not do what you expect.

// constants won't change. They're used here to
// set pin numbers:

const int buttonPin = 2; // the number of the pushbutton pin
const int buttonPin1 = 3;
const int ledPin = 13;      // the number of the LED pin
const int ledPin1 = 12;


 

// Variables will change:

int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin
int ledState1 = HIGH;
int buttonState1;         
int lastButtonState1 = LOW;


// the following variables are unsigned long's because the time, measured in miliseconds,

// will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
unsigned long lastDebounceTime1 = 1;  // the last time the output pin was toggled
unsigned long debounceDelay1 = 49;    // the debounce time; increase if the output

 

void setup() {

  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin1, INPUT);
  pinMode(ledPin1, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
  digitalWrite(ledPin1, ledState1);

}

 

void loop() {

  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);


  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise:

 
  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

 

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    // if the button state has changed:
    if (reading != buttonState) {
     buttonState = reading;
     // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
       ledState = !ledState;

      }
    }
  }
  // set the LED:
 digitalWrite(ledPin, ledState);
  // save the reading.  Next time through the loop,
 // it'll be the lastButtonState:

  lastButtonState = reading;

   // read the state of the switch into a local variable:
 int reading1 = digitalRead(buttonPin1)
  // check to see if you just pressed the button
 // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise:
  // If the switch changed, due to noise or pressing:
  if (reading1 != lastButtonState1) {
    // reset the debouncing timer
   lastDebounceTime1 = millis();

  }

  if ((millis() - lastDebounceTime1) > debounceDelay1) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

     // if the button state has changed:
   if (reading1 != buttonState1) {
      buttonState1= reading1;
     // only toggle the LED if the new button state is HIGH
      if (buttonState1 == HIGH) {
       ledState1 = !ledState1;
      }
    }
  }

 // set the LED:
  digitalWrite(ledPin1, ledState1);
  // save the reading.  Next time through the loop,
 // it'll be the lastButtonState:
  lastButtonState1 = reading1;

}

Hello all and thanks for the input .
Sorry about the space invasion I didn't preview it first and some how they made their way in ,code ammended and reposted above .
Ive done some research on arrays ,definitely an improvement I'll add as I want have 7switches and LED s eventually but I'm starting small till I crack it .
Bounce2 looks like improvement too..
Correct me if I'm wrong but my thought was the attempt I've made already is quite obviously pretty primitive and riddled with mistakes but I'll be best off straightening it out till it actually works and then getting into tidying and simplifying with arrays,bounce2 etc
A question I have : how many times have I unnecessarily doubled up? (not using an array is obviously one)

Somewhere along the line you have lost a semicolon from your code and it won't compile.

Apart from that you seem to have duplicated the code OK but one annoying thing, to me at least, is the naming of variables. It will make no difference to the operation of the code but variables numbered buttonPin0 and buttonPin1 etc make more sense than buttonPin and buttonPin1. This will become more apparent when you start to use arrays.

I think that you are right to get one button/LED working first but knowing that you will eventually need 7 pairs then using arrays is the obvious answer.

Although I don't totally agree with Paul, because if you make an array even for two elements it's peanuts to expand to more if you want later on, but at least number all variables...

And about the use of Bounce2, that's not just to tidy up. It just gives you an easy start. Once you know how debouncing works (because in my opinion that's the whole idea of the example sketch) it's just easier to grab a library. You already do that for setting pins (because yeah, digitalWrite() is part of a library).

const int buttonPin = 2; // the number of the pushbutton pin

Great you use a const! But try to get in the habit of using the smallest possible variable that's big enough. A byte would do for pin declararions.

int ledState = HIGH;         // the current state of the output pin

Same here, you use a 16-bit in to store HIGH or LOW which would fit in a 1-bit bool. That's for every variable you use just to store a HIGH/LOW or true/false.

And as far of the state of the led, you can also just read the state if you want to know it (digitalRead() also works on outputs). That makes life simpler because now it's up to you the state variable and the real state actually match.

int buttonState;             // the current reading from the input pin
[/code
Better name would be stableButtonState.

[code]unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

You can use the same debounce time for both. It's just the time the signal needs to be stable before you say it's a valid input. Also, you don't change it on runtime so it can be const. And a unsigned long for the value 50 is a bit overkill. Even a byte will do :slight_smile:

  pinMode(buttonPin, INPUT);

So I guess external pull down resistors on the buttons? Easier to use the internal pull up resistors. That way you need no external resistor, just the button between GND and a pin.[/code]

digitalWrite() is a library

It may just be semantics, but is digitalWrite() actually a library or is it a function in a library ?

You're right, edited :slight_smile: