Adjusting code for multiple sizes of 7-segment displays

This code is written for an 8x7 LED display that uses an I2C backpack, and I’m trying to write a similar program that utilizes a 4x7 LED display. I think that I need to edit the "byte anode, “byte cathode”, “byte decimal” sections, and change everything that says “byte e = 8”, “display8x7”, and the 8 digit “values” to all reflect a numerical string of no more than 4 digits. Is this correct?

Or does “byte cathode” tell the display what to actually display?

And to remove 4 display sections would I remove “byte anode” 1-4 or 5-8?

//Countdown Timer Sketch

#define LATCH  12  //pin 12 connect to RCK
#define CLOCK  11  //pin 11 connect to SCK
#define DATA   10  //pin 10 connect to DIO
#define BUZZ    3  //pin 3  connect to Buzzer
#define BUTTON  5  //pin 5 connect to Button
#define LEFT   0  // define the value for left justify
#define RIGHT  1  // define the value for right justify

int buzztrig = 9; //Trigger counter for Buzzer
String mystring; //Save Counter Value to a String
unsigned long value = 2000000; // Countdown value: HHMMSSS

// array to activate particular digit on the 8x7segment module
// it is the common anode of 7 segment
byte anode[8] = { 0b10000000,  //digit 1 from right
                    0b01000000,  //digit 2 from right
                    0b00100000,  //digit 3 from right
                    0b00010000,  //digit 4 from right
                    0b00001000,  //digit 5 from right
                    0b00000100,  //digit 6 from right
                    0b00000010,  //digit 7 from right
                    0b00000001   //digit 8 from right                                         
                    };

//array for decimal number, it is the cathode.
//logic low will activate the particular segment
byte cathode[12] = {0b11000000,  // 0
                    0b11111001,  // 1
                    0b10100100,  // 2
                    0b10110000,  // 3
                    0b10011001,  // 4
                    0b10010010,  // 5
                    0b10000010,  // 6
                    0b11111000,  // 7
                    0b10000000,  // 8
                    0b10010000,  // 9  
                    0b01111111,  //dot                  
                    0b11111111   //blank
                    };     
 
//fucntion to send the serial data out to two 74HC595 serial to parallel shift register and activate the 7 segment.                  
void display8x7segment(byte datapin, byte clockpin, byte latchpin, byte digit, byte number)
{
    digitalWrite(latchpin, LOW);
    shiftOut(datapin, clockpin, MSBFIRST, digit); // clears the right display
    shiftOut(datapin, clockpin, MSBFIRST, number); // clears the left display
    digitalWrite(latchpin, HIGH);  
}

//function to display value on 8x7 segment display according to the justify state
void displayNumber8x7segment(byte justify, unsigned long value)
{

   byte decimal[8] = {0};   
   value = value % 100000000;  //ensure the value is within 8 digits only
   decimal[7] = value / 10000000;  //extract digit 7 from value
   value = value % 10000000;       //extract the rest of 7 digit value
   decimal[6] = value / 1000000;
   value = value % 1000000;
   decimal[5] = value / 100000;
   value = value % 100000;
   decimal[4] = value / 10000;
   value = value % 10000;
   decimal[3] = value / 1000;
   value = value % 1000;
   decimal[2] = value / 100;
   value = value % 100;
   decimal[1] = value / 10;
   decimal[0] = value % 10;
   byte zero = 0;
   if (justify == RIGHT)
   {   
     for(byte e = 8; e  > 0 ; e --)
     {
       if(zero == 0)
       {
         if(decimal[e-1] != 0)          
         {
           display8x7segment(DATA, CLOCK, LATCH, anode[e-1], cathode[decimal[e-1]]);
           zero = 1;     
         }
       }
       else display8x7segment(DATA, CLOCK, LATCH, anode[e-1], cathode[decimal[e-1]]);
     
     delay(1); 
     }
   }
   else  //if justify == left
   { 
     byte d = 0;     
     for(byte e = 8; e > 0; e --)
     {       
       if(zero == 0)
       {
         if(decimal[e-1] != 0)         
         {
           display8x7segment(DATA, CLOCK, LATCH, anode[7], cathode[decimal[e-1]]);
           zero = 1;
           d ++;     
           delay(1); 
         }
       }
       else 
       {
         display8x7segment(DATA, CLOCK, LATCH, anode[7-d], cathode[decimal[e-1]]);
         d ++;
         delay(1); 
       }     
       
     }
     
   } 
}
 
void setup() {
  pinMode(LATCH, OUTPUT);
  pinMode(CLOCK, OUTPUT);
  pinMode(DATA, OUTPUT);
  pinMode(BUZZ, OUTPUT);
  pinMode(BUTTON, INPUT_PULLUP);
  digitalWrite(LATCH, HIGH); //Wake Up Display 
  digitalWrite(BUZZ, LOW);  //Buzzer Off
  digitalWrite(BUTTON, HIGH); // Button Low  
  delay(1000);  //delay for 1 second
  
}

void loop(){ 
  
  //Wait for button press to start Countdown
  while(digitalRead(BUTTON) == HIGH){
  displayNumber8x7segment(RIGHT, value);  //display the Countdown Start value
  }
  
  for (value; value != -1; value --)
  {        
    for(byte i = 9; i > 0; i --) // Adjust i=x to speed up or slow time
    {
      String mystring = String(value); //Transform Counter Int to String
            
      //Do time Offset 
      for (int z = 0; z < 5; z++){
             
        if ( mystring.substring(z) == "99999" ) {
          value = (value - 40000);
        }        

        if ( mystring.substring(z) == "999" ) {
          value = (value - 400);
        }
      }
      
      displayNumber8x7segment(RIGHT, value);  //display the value in right justify format      
     } 
     
    // Countdown is Finished? Sound Alarm
    if (value == 0) {
     for(byte k = 0; k < 11; k++)
    {
      
      for (int j = 0; j < 80; j++) // Buzzer frequency sound 1
    {
      digitalWrite (BUZZ, HIGH) ;// Buzzer On
      delay (1) ;// Delay 1ms
      digitalWrite (BUZZ, LOW) ;// Buzzer Off
      delay (1) ;// delay 1ms
    }
    for (int j = 0; j < 100; j++) // Buzzer frequency sound 2
    {
      digitalWrite (BUZZ, HIGH) ;// Buzzer On
      delay (2) ;// delay 2ms
      digitalWrite (BUZZ, LOW) ;// Buzzer Off
      delay (2) ;// delay 2ms
    }
      display8x7segment(DATA, CLOCK, LATCH, 0xff, cathode[0]);  //activate all digit  // Put ALL zeros
      delay(300);
      display8x7segment(DATA, CLOCK, LATCH, 0xff, cathode[11]); // Blank Display
      delay(300);
    }  
    }
    
    //Countdown Tick sound
    if (buzztrig == 0){
      for (int i = 0; i < 5; i++) // Buzzer frequency sound
      {
        digitalWrite (BUZZ, HIGH) ;// Buzzer On
        delay (2) ;// Delay 2ms
        digitalWrite (BUZZ, LOW) ;// Buzzer Off
        delay (1) ;// delay ms
      } 
    buzztrig = 9; //  Trigger for countdown sound
    }
      else {
        buzztrig = buzztrig - 1;
      }
    }  
}

You are mistaken. That sketch will not work with an i2c backpack.

Post a link to the display you have purchased, and the backpack, if that is separate. Read the forum guidelines to find out how to post a link correctly.

Are you sure? I pulled that code from https://brainy-bits.com/blogs/tutorials/arduino-countdown-timer and it sure looks like they used an I2C LED display to me.

And this is the LED display that I purchased. 1.2" 7-segment Backpack | Adafruit LED Backpacks | Adafruit Learning System.

From the tutorial link you posted: "And the 7 segment module is connected to Pin 10 (DIO), Pin 11 (SCK) and Pin12 (RCK)."

Do they look like/sound like the I2C pins to you? In the comments of the code they mention:

"//fucntion to send the serial data out to two 74HC595 serial to parallel shift register and activate the 7 segment"

Which confirms what PaulRB said, it's not a backpack display.

The most basic question I have is why do you buy an Adafruit display and then try to hack some inappropriate library code from some unknown internet site? The advantage in buying Adafruit products is that they come with support and known to work libraries. Why are you not using the Adafruit tutorial?

You bought an i2c display. The display in that project, as I said before, is not i2c. You cannot use that code. It's very poor code anyway, so no big loss. If you want proof of how bad that code is, look no further than this line:

zero = 1;

c0pperh34d:
Are you sure? I pulled that code from https://brainy-bits.com/blogs/tutorials/arduino-countdown-timer and it sure looks like they used an I2C LED display to me.

Do you see any I2C code in the sketch you posted? Because I can't find it. That's enough for me to conclude it's not for an I2C display.