7 segment display coding problem and needed help

I have some problem with my coding. Logic is when i press switch 1 the value at 7 segment display counter will add by 1. The other switch when pressed it will decrease the value at 7 segment display. But my coding is so bad and my friend could not help me very much. My code is below. and my input for the switches is PWM 13 and 9. My coding problem is when i press either one of the switch, the value will randomly change.

byte numbers[10] = { 
                    B11111100, B01100000, B11011010, B11110010, B01100110, 
                    B10110110, B10111110, B11100000, B11111110, B11100110 
                    };
void setup() { 
    for(int i = 2; i <= 8; i++) {
        pinMode(i, OUTPUT); 
    }
    pinMode(10, INPUT);
    pinMode(13, INPUT);
}
 
int counter = 9;
bool go_by_switch = true;
int last_input_value = LOW;
 
void loop() {
    if(go_by_switch) {
        int switch_input_value = digitalRead(13);
        if(last_input_value == LOW && switch_input_value == HIGH) {
            counter = (counter + 1) % 10;
        }
        last_input_value = switch_input_value;
    } else {
        delay(500); 
        counter = (counter + 1) % 10;
    }
    writeNumber(counter);
    
    if(go_by_switch) {
        int switch_input_value = digitalRead(10);
        if(last_input_value == LOW && switch_input_value == HIGH) {
            counter = (counter - 1) % 10;
        }
        last_input_value = switch_input_value;
    } else {
        delay(500); 
        counter = (counter - 1) % 10;
    }
    writeNumber(counter);
}
 
void writeNumber(int number) {
    if(number < 0 || number > 9) {
        return;
    }
    byte mask = numbers[number];
    byte currentPinMask = B10000000;
    for(int i = 2; i <= 8; i++) {
        if(mask & currentPinMask) digitalWrite(i,HIGH); 
        else digitalWrite(i,LOW);
    currentPinMask = currentPinMask >> 1;
    }
}

My coding problem is when i press either one of the switch, the value will randomly change.

How are the switches wired? Why are you not using the easy way, involving the internal pullup resistors?

You'd be better off not using pin 13 as an input.

Your problem might be that your button is bouncing, thus, your button code is executed a few times when you press the button. Search about "debouncing".

This is probably not the cause of your issue, but..

You use last_input_value to keep the state of [u]both[/u] switches.

The result is that on every test: if (last_input_value == LOW && switch_input_value == HIGH), ..the values for last_input_value and switch_input_value are from different switches.

PaulS: How are the switches wired? Why are you not using the easy way, involving the internal pullup resistors?

You'd be better off not using pin 13 as an input.

How is the easy way? Sorry to ask because i am very new to coding.

ariffamir: How is the easy way? Sorry to ask because i am very new to coding.

One leg connected to a digital pin. The other leg connected to ground. The internal pull resistor turned on (using INPUT_PULLUP as the mode).