using button to interupt a seven segment counting loop

I’m trying to write a seven segment counting loop 0-9 repeatedly. But can be interrupted with a button to count button presses 0-9. however, when I start pressing the button it lights up random segments on the display rather than 0-9. anyone know how to fix this.

The code uses a 74HC595N shift register.

const int dataPin = 4; // blue wire to 74HC595 pin 14
const int latchPin = 7; // green to 74HC595 pin 12
const int clockPin = 8; // yellow to 74HC595 pin 11
const int button = 9;
#define Led_off ((0))
#define Led_dot ((unsigned char) (128))
#define Led_zero ((1+2+4+8+16+32))
#define Led_one ((2+4))
#define Led_two ((1+2+8+16+64))
#define Led_three ((1+2+4+8+64))
#define Led_four ((2+4+32+64))
#define Led_five ((1+4+8+32+64))
#define Led_six ((1+4+8+16+32+64))
#define Led_seven ((1+2+4))
#define Led_eight ((1+2+4+8+16+32+64))
#define Led_nine ((1+2+4+32+64))
#define Total_shift_pins 8
long currentTime = 0;
long debounce = 50;
int previous = LOW;

int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button

void shift_segment(int value){
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, value);
digitalWrite(latchPin, HIGH);
delay(1000);
}
void setup(){
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(button, INPUT);
}
void loop(){
unsigned char i,j,k;
buttonState = digitalRead(button);
if (buttonState != lastButtonState) {
if (buttonState == HIGH && previous == LOW && millis() - currentTime > debounce) {
// if the current state is HIGH then the button went from off to on:
j++;
currentTime = millis();
}
switch(j){
case 0:shift_segment(Led_zero);break;
case 1:shift_segment(Led_one);break;
case 2:shift_segment(Led_two);break;
case 3:shift_segment(Led_three);break;
case 4:shift_segment(Led_four);break;
case 5:shift_segment(Led_five);break;
case 6:shift_segment(Led_six);break;
case 7:shift_segment(Led_seven);break;
case 8:shift_segment(Led_eight);break;
case 9:shift_segment(Led_nine);break;
default:shift_segment(Led_off);break;
}
delay(50);
}else{
// if the current state is LOW then the button went from on to off:

}
lastButtonState = buttonState;

for(i=0; i<10; i++){

switch(i){
case 0:shift_segment(Led_zero);break;
case 1:shift_segment(Led_one);break;
case 2:shift_segment(Led_two);break;
case 3:shift_segment(Led_three);break;
case 4:shift_segment(Led_four);break;
case 5:shift_segment(Led_five);break;
case 6:shift_segment(Led_six); break;
case 7:shift_segment(Led_seven);break;
case 8:shift_segment(Led_eight);break;
case 9:shift_segment(Led_nine);break;
default:shift_segment(Led_off);break;
}
}
if(j==10){
j=0;}

}

seven_segment_display_code.ino (2.58 KB)

When you declare variables inside a function, they do not retain their values after a return from the function unless you declare them "static":

void loop(){
  static unsigned char i,j,k;

loop() is called repeatedly and returns repeatedly. That is definitely a problem for variable j.