Double Trouble!

Hi all,
I have a question regarding my code. I have created a toggle function to a momentary switch that switches outputs when it is pressed, after a break; press1: LED1 ON/LED2 OFF, press2: LED1 & 2 OFF, press3: LED1 OFF/LED2, press4: LED1 & 2 OFF, repeat.
The issue I have encountered is that when I try to have two buttons to each have control of two LEDs each, the LEDs seem to bypass the second button and flash in an unexpected manner.
The files compile fine without errors, so I am hoping for some advice and critique.
I have included the code for both single and double button iterations.
Thank you:)

int ledPin1 = 10;
int ledPin2 = 9;
int switchPin = 2;
int count = 0;

boolean lastButton;
boolean currentButton = false;
boolean ledOn = false;

void setup() {

  pinMode(switchPin, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  count = 0;
}
//debounce function to stabilise the button
boolean debounce(boolean last)
{
  boolean current = digitalRead(switchPin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;  
}
void loop() {
  lastButton = currentButton;
  currentButton = debounce(lastButton);
  if (lastButton == false && currentButton == true)
  {
    if (count == 0) 
    { 
      count++;
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, LOW);
    }

    else if (count == 1)
    { 
      count++;
      digitalWrite(ledPin1, LOW); 
      digitalWrite(ledPin2, LOW);
    }

        else if (count == 2)
    { 
      count++;
      digitalWrite(ledPin1, LOW); 
      digitalWrite(ledPin2, HIGH);
    }

    else if (count == 3)
    { 
      count = 0;
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, LOW);
    }

  } 
}
int ledPin1 = 10;
int ledPin2 = 9;
int ledPin3 = 8;
int ledPin4 = 7;

int switchPin = 2;
int switchPin2 = 3;
int count = 0;

boolean lastButton;
boolean currentButton = false;
boolean ledOn = false;

boolean lastButton2;
boolean currentButton2 = false;
boolean ledOn2 = false;

void setup() {

  pinMode(switchPin, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);

  pinMode(switchPin2, INPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT); 

  count = 0;
}
//debounce function to stabilise the button1
boolean debounce(boolean last)
{
  boolean current = digitalRead(switchPin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;  


//debounce function to stabilise the button2
}
 boolean debounce2(boolean last2)

{  boolean current2 = digitalRead(switchPin2);
  if (last2 != current2)
  {
    delay(5);
    current2 = digitalRead(switchPin2);
  }
  return current2;  
}


void loop() {
  lastButton = currentButton;
  currentButton = debounce(lastButton);
  if (lastButton == false && currentButton == true)
  {
    if (count == 0) 
    { 
      count++;
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, LOW);
    }

    else if (count == 1)
    { 
      count++;
      digitalWrite(ledPin1, LOW); 
      digitalWrite(ledPin2, LOW);
    }

        else if (count == 2)
    { 
      count++;
      digitalWrite(ledPin1, LOW); 
      digitalWrite(ledPin2, HIGH);
    }

    else if (count == 3)
    { 
      count = 0;
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, LOW);
    }


  } 

   lastButton2 = currentButton2;
  currentButton2 = debounce(lastButton2);
  if (lastButton2 == false && currentButton2 == true)
  {
    if (count == 0) 
    { 
      count++;
      digitalWrite(ledPin3, HIGH);
      digitalWrite(ledPin4, LOW);
    }

    else if (count == 1)
    { 
      count++;
      digitalWrite(ledPin3, LOW); 
      digitalWrite(ledPin4, LOW);
    }

        else if (count == 2)
    { 
      count++;
      digitalWrite(ledPin3, LOW); 
      digitalWrite(ledPin4, HIGH);
    }

    else if (count == 3)
    { 
      count = 0;
      digitalWrite(ledPin3, LOW);
      digitalWrite(ledPin4, LOW);
    }

  } 
}

To add a further complication I am dealing with, I also would prefer the buttons to operate as a momentary switch instead of a latching toggle. I have some code to operate the momentary switch but don't know how or even if I can integrate it into the toggle code.
Thanks
Here is my momentary switch code:

 const int buttonPin = 2;
 const int ledPin = 13;

 // define variables
 int buttonState = 0;

 
 void setup() 
 {
  pinMode(ledPin, OUTPUT);    // initializes LEDpin as an output
  pinMode(buttonPin, INPUT);  // initializes buttonPin as an input
}

void loop() {
  buttonState = digitalRead(buttonPin);   // reads the state of the buttonPin value

  if ( buttonState == HIGH) {             // checks to see if pushbutton is pushed, if yes, then buttonState is HIGH
    digitalWrite(ledPin, HIGH);           // turns LED on
  }
  else {
    digitalWrite(ledPin, LOW);            // turns LED off
    
  }
  

}

Do you have a pulldown (or pullup) resistor on pin 2 to keep it from floating when the button is not pressed? Without it, the pin might show HIGH or LOW at any time button isn't pressed.

I have a 10k pulldown resistoron pin 2.

One thing I notice, you have only defined one "count" variable.

(edit)

also

currentButton2 = debounce(lastButton2);

Thank you @david_nc, that was a helpful find. I'm still finding that the LEDs are still only controlled by switchPin and not switchPin2.

Why do you have two nearly identical debounce functions? The ONE function should take the pin number as an argument.

I have a 10k pulldown resistoron pin 2.

What about pin 3?

Pictures (schematics) are worth a thousand words.

Kendo147:
Thank you @david_nc, that was a helpful find. I’m still finding that the LEDs are still only controlled by switchPin and not switchPin2.

Did you correct both of the problems I pointed out?

Kendo147:
To add a further complication I am dealing with, I also would prefer the buttons to operate as a momentary switch instead of a latching toggle. I have some code to operate the momentary switch but don't know how or even if I can integrate it into the toggle code.
Thanks
Here is my momentary switch code:

 const int buttonPin = 2;

const int ledPin = 13;

// define variables
int buttonState = 0;

void setup()
{
  pinMode(ledPin, OUTPUT);    // initializes LEDpin as an output
  pinMode(buttonPin, INPUT);  // initializes buttonPin as an input
}

void loop() {
  buttonState = digitalRead(buttonPin);  // reads the state of the buttonPin value

if ( buttonState == HIGH) {            // checks to see if pushbutton is pushed, if yes, then buttonState is HIGH
    digitalWrite(ledPin, HIGH);          // turns LED on
  }
  else {
    digitalWrite(ledPin, LOW);            // turns LED off
   
  }

}

You need more detail then. When button 1 is held down, what LEDs should light? When it is released, what LEDs? When it is held again, what LEDs? What is the full cycle? Maybe draw a list on paper or in a spreadsheet.

@PaulS : Thank you, I also have a 10k pulldown resistor on pin3.

@david_nc : Thank you, I followed your advice and I added another count variable, as per the other issue you mentioned, I'm not sure what needs to be modified with "currentButton2 = debounce(lastButton2);". I have limited understanding and experience with the syntax, but I am learning, so I do appreciate any further guidance you can offer.

Here is my code in it's latest iteration:

int ledPin1 = 10;
int ledPin2 = 9;
int ledPin3 = 8;
int ledPin4 = 7;
int switchPin = 2;
int switchPin2 = 3;
int count = 0;
int count2 = 0;

boolean lastButton;
boolean currentButton = false;
boolean ledOn = false;

boolean lastButton2;
boolean currentButton2 = false;
boolean ledOn2 = false;

void setup() {

  pinMode(switchPin, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);

  pinMode(switchPin2, INPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT); 

  count = 0;
}
//debounce function to stabilise the button1
boolean debounce(boolean last)
{
  boolean current = digitalRead(switchPin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;  


//debounce function to stabilise the button2
}
 boolean debounce2(boolean last2)

{  boolean current2 = digitalRead(switchPin2);
  if (last2 != current2)
  {
    delay(5);
    current2 = digitalRead(switchPin2);
  }
  return current2;  
}


void loop() {
  lastButton = currentButton;
  currentButton = debounce(lastButton);
  if (lastButton == false && currentButton == true)
  {
    if (count == 0) 
    { 
      count++;
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, LOW);
    }

    else if (count == 1)
    { 
      count++;
      digitalWrite(ledPin1, LOW); 
      digitalWrite(ledPin2, LOW);
    }

        else if (count == 2)
    { 
      count++;
      digitalWrite(ledPin1, LOW); 
      digitalWrite(ledPin2, HIGH);
    }

    else if (count == 3)
    { 
      count = 0;
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, LOW);
    }


  } 

   lastButton2 = currentButton2;
  currentButton2 = debounce(lastButton2);
  if (lastButton2 == false && currentButton2 == true)
  {
    if (count2 == 0) 
    { 
      count2++;
      digitalWrite(ledPin3, HIGH);
      digitalWrite(ledPin4, LOW);
    }

    else if (count2 == 1)
    { 
      count2++;
      digitalWrite(ledPin3, LOW); 
      digitalWrite(ledPin4, LOW);
    }

        else if (count2 == 2)
    { 
      count2++;
      digitalWrite(ledPin3, LOW); 
      digitalWrite(ledPin4, HIGH);
    }

    else if (count2 == 3)
    { 
      count2 = 0;
      digitalWrite(ledPin3, LOW);
      digitalWrite(ledPin4, LOW);
    }

  } 
}

@MorganS : Thank you, as well.
Ideally, when SWITCHPIN(pin2) is pressed, LED1(pin7) turns ON, when released, LED1 turns OFF, when SWITCHPIN is pressed again, LED2(pin8) turns ON, OFF when SWITCHPIN is released, pattern continues. The same pattern will happen to SWITCHPIN2(pin3) controlling LED3(pin9) and LED4(pin10).
I am unsure if it is possible to make the SWITCHPINs momenatry in this context.

I’m not sure what needs to be modified with “currentButton2 = debounce(lastButton2);”.

You defined a separate debounce function for each button, but are using the debounce for button 1 on button 2 - that is why button 2 appears to not function.

Start simple and get it working with 1 button and 1 pair of LEDs first. Also read up on "state machine".

@david_nc : Thank you for your guidance, the program works perfectly now. My next challenge is to see if I can make the buttons work as momentary toggle switches instead of latching toggle switches. I will post my code here if and when I get it sorted.

@Metallor : Thanks for your interest in this. I started with 1 button and 2 LEDs and it worked fine, when I doubled up the code to allow 2 buttons each with their own 2 LEDs, I obviously missed out on some key coding errors that david_nc was able to help me out with. Also, thanks for the suggestion to read up on 'state machine,' I'm on it!