How to code 3 Butons with debounce function ?

Hello,

I am fairly new to Arduino and Coding alltogether so I am sorry if this is a silly question.

But I wanted to make a Pneumatic System, nothing big, just a small pump powered by a motor, and a servo motor which controlls the Air Switch. Now, currently via Serial Communication Ive got it to make a Pneumatic Cylinder to Push and Pull or just staying depending on a button I press on my Keyboard.

All is well and fine with that.

Code is here:

#include <Servo.h>
int motorPin = 9;


Servo myservo;
int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(10);  // attaches the servo on pin 10 to the servo object
pinMode(motorPin, OUTPUT);

Serial.begin(9600);
}

void loop() {
  
while(Serial.available() == 0);


int val = Serial.read() - '0';

if( val == 1)
{
  Serial.println("Pull");
  analogWrite(motorPin, 255); // Motor Speed for the Air Pump
  myservo.write(10); // Moves the Servo to the Degree neccessary to move the Switch Shaft
}
     else if(val == 2)
     {
      Serial.println("Neutral");
      analogWrite(motorPin, 0); // Since the Cylinder is in a Neutral state which doesnt Move the Motor is not Moving
      myservo.write(60); // Moves the Servo to the Degree neccessary to move the Switch Shaft
     }  
      else if(val == 3)
      {
        Serial.println("Push");
        analogWrite(motorPin, 255);  // Motor Speed for the Air Pump
        myservo.write(120); // Moves the Servo to the Degree neccessary to move the Switch Shaft
      }
        else
        {
          Serial.println("INVALID");
          }
}

Now I would like some more Controll over this using actual buttons, three of them actualy which are supposed to do the same as what the above Code shows, One for Pushing, one for Pulling, one for Neutral position.

However I have only used simple LED Button Controll code and Wiring so far with a SINGULAR Button.

int switchPin = 8;
int ledPin = 11;
boolean lastButton = LOW;
boolean currentButton = LOW;
int ledLevel = 0;





void setup() {
  // put your setup code here, to run once:
pinMode (switchPin, INPUT);
pinMode (ledPin,OUTPUT);
}
//Boolean to debounce the Button
boolean debounce(boolean last)
{
  boolean current = digitalRead(switchPin);
  if (last!= current)
  {delay(5);
  current = digitalRead(switchPin);
}
return current;
}

void loop() 
{
  // Detecting whether the Last button that was pressed was On or not in Order to change the LED Brightness

  currentButton = debounce(lastButton);
if ( lastButton == LOW && currentButton == HIGH)
{
  ledLevel = ledLevel + 25;
}
lastButton = currentButton;

if (ledLevel > 250) ledLevel = 0;
analogWrite(ledPin, ledLevel);
}

Now I know how to Wire the Buttons up but I dont know what to transfer from the 2nd Code into the 1st Code and how often and what I need to adjust.

I can Imagine that the booleans and the if sentence in the void loop are supposed to be used, however I dont quite understand what I need to adjust on it and if I even need to reuse them several times or whether Im fine with just using that singular one.

If theres anything that you dont understand, Im sorry however I will answer any questions.
I can sadly not take a Photo of the Wiring since I dont have a camera.

Ive looked in the Internet already but all there was were complicated and not very overlookable examples.

Thanks in Advance if someone is able to help me.
P.S Sorry for my English grammar I know it is not the best.

Maybe you could have a look at:

OK, here's code for four fully debounced buttons to toggle each of four LEDs. (See, you get one extra!)

All you need to do is to substitute your servo commands for the "toggle" functions in "loop()".

// Multiple toggles!
const int led1Pin =  3;    // LED pin number
const int button1 =  2;
const int led2Pin =  5; 
const int button2 =  4;
const int led3Pin =  6;
const int button3 =  7;
const int led4Pin =  9;
const int button4 =  8;
char bstate1 = 0;
char bstate2 = 0;
char bstate3 = 0;
char bstate4 = 0;
unsigned long bcount1 = 0; // button debounce timer.  Replicate as necessary.
unsigned long bcount2 = 0;
unsigned long bcount3 = 0;
unsigned long bcount4 = 0;

char led1State = LOW;        // initialise the LED
char led2State = LOW;
char led3State = LOW;
char led4State = LOW;

// Have we completed the specified interval since last confirmed event?
// "marker" chooses which counter to check
// Routines by Paul__B of Arduino Forum
boolean timeout(unsigned long *marker, unsigned long interval) {
  if (millis() - *marker >= interval) { 
    *marker += interval;    // move on ready for next interval
    return true;       
  } 
  else return false;
}

// Deal with a button read; true if button pressed and debounced is a new event
// Uses reading of button input, debounce store, state store and debounce interval.
// Routines by Paul__B of Arduino Forum
boolean butndown(char button, unsigned long *marker, char *butnstate, unsigned long interval) {
  switch (*butnstate) {               // Odd states if was pressed, >= 2 if debounce in progress
  case 0: // Button up so far, 
    if (button == HIGH) return false; // Nothing happening!
    else { 
      *butnstate = 2;                 // record that is now pressed
      *marker = millis();             // note when was pressed
      return false;                   // and move on
    }

  case 1: // Button down so far, 
    if (button == LOW) return false; // Nothing happening!
    else { 
      *butnstate = 3;                 // record that is now released
      *marker = millis();             // note when was released
      return false;                   // and move on
    }

  case 2: // Button was up, now down.
    if (button == HIGH) {
      *butnstate = 0;                 // no, not debounced; revert the state
      return false;                   // False alarm!
    }
    else { 
      if (millis() - *marker >= interval) {
        *butnstate = 1;               // jackpot!  update the state
        return true;                  // because we have the desired event!
      }
      else 
        return false;                 // not done yet; just move on
    }

  case 3: // Button was down, now up.
    if (button == LOW) {
      *butnstate = 1;                 // no, not debounced; revert the state
      return false;                   // False alarm!
    }
    else { 
      if (millis() - *marker >= interval) {
        *butnstate = 0;               // Debounced; update the state
        return false;                 // but it is not the event we want
      }
      else 
        return false;                 // not done yet; just move on
    }
  default:                            // Error; recover anyway
    {  
      *butnstate = 0;
      return false;                   // Definitely false!
    }
  }
}

// ----------------------------- toggle ------------------------------------------
char toggle(char *flip) {   // Yes, it toggles the variable pointed to
  if (*flip == LOW) {
    *flip = HIGH;
  }
  else {
    *flip = LOW; 
  } 
  return *flip;
}

void setup() {
  pinMode(led1Pin, OUTPUT);      
  pinMode(button1, INPUT); 
  pinMode(led2Pin, OUTPUT);      
  pinMode(button2, INPUT);      
  pinMode(led3Pin, OUTPUT);      
  pinMode(button3, INPUT);      
  pinMode(led4Pin, OUTPUT);      
  pinMode(button4, INPUT);        
  digitalWrite (led1Pin, LOW);
  digitalWrite (led2Pin, LOW);
  digitalWrite (led3Pin, LOW);
  digitalWrite (led4Pin, LOW);
}

void loop() {
  // Toggle LED if button debounced
  if (butndown(digitalRead(button1), &bcount1, &bstate1, 10UL )) {
    toggle(&led1State);
    digitalWrite(led1Pin, led1State);
  } 

  if (butndown(digitalRead(button2), &bcount2, &bstate2, 10UL )) {
    toggle(&led2State);
    digitalWrite(led2Pin, led2State);
  } 

  if (butndown(digitalRead(button3), &bcount3, &bstate3, 10UL )) {
    toggle(&led3State);
    digitalWrite(led3Pin, led3State);
  } 

  if (butndown(digitalRead(button4), &bcount4, &bstate4, 10UL )) {
    toggle(&led4State);
    digitalWrite(led4Pin, led4State);
  } 
}

I could do it for you, but you should be able to with no problems. Note buttons are always from input to ground. Use pull-up resistors or set INPUT_PULLUP mode.