counting up with 7 segment automatically then counting with a button

I’m trying to write a code that can switch between counting 0-9 on its own. However, once a button is pressed it begins to count button presses then once it hits 9 it starts counting 0-9 again on its own.
I have already written the count code but but do not know how to use the button to begin its own task.

I’ve found a few bits of information online for using debounce but i don’t understand how to fit it into my code.
Project uses 74HC595N shift register and common cathode segment display

shift_segment.ino (1.55 KB)

I can’t test this code but it might give you a place to start wrapping the manual button logic around your auto counting ability. The idea being to define a flag for which mode your counter is in, either auto or manual, then setting that flag on a button press to switch modes and clearing it when the count reaches 9. There is no debounce specific code here but it only counts presses that occur after the display changes with the 1 sec delay. You might also consider consolidating the switch statement in its own subroutine rather than repeating it. Other questions to consider are do you want increment on press or increment on release of the button. Do you want auto to start after the 10th press or 9th press of the button. Most on this forum won’t write code for you, but I hope this helps you get going again.

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
#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 button 10


bool count_mode_auto = true;  //<-- declare a flag for keeping the count mode as either auto (true) or manual (false)
unsigned char count = 0;      //<-- declare a place to keep the manual count

void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  //  pinMode(button, OUTPUT);
  pinMode(button, INPUT_PULLUP);   //<-- Assume button is connected to IO pin 10, with pullup and driven low with button press.

}
void loop() {
  unsigned char i, j, k;
  if (count_mode_auto) {           //<-- Make Counting Code conditional on count_mode_auto being True
    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 (!digitalRead(button)) count_mode = false;  // <-- switch count_mode_auto to false if button is pressed
    }
  } else {                                           // <-- add code for manually counting when count_mode_auto is false
     switch (count) {                                 // <-- display the manual count starting with zero
      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 (count == 9) {                 //<-- Reset count and mode when count just displayed reaches 9
      count = 0;
      count_mode_auto = true;         //<-- Next time through loop will be in auto count mode rather than manual.
    } else {
      while (!digitalRead(button)) {};// <-- wait for button to be released if not already so you don't count the same press twice
      while (digitalRead(button)) {}; //<-- wait on button press to increment count and continue back again through loop 
      count = count + 1;  
    }
  }
  
  void shift_segment(int value) {
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, value);
    digitalWrite(latchPin, HIGH);
    delay(1000);
  }