[Beginner] Help in troubleshooting this code

Hi guys! I’m doing this electronic metronome that uses a 7 segment display. The problem is when i turn on the system, the display shows random lights. Can anyone help me? Thank you so much

#define F_CPU 1000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

#define MAX_SPEED              250   //Max metronome speed in BPM
#define MIN_SPEED              30    //Min metronome speed in BPM

#define DISP_CATH_PORT         PORTD //The Display cathodes are connected to pins PD0-PD6
#define DISP_AN_PORT           PORTC //The Display anodes are connected to pins PC0-PC2
#define DIGIT1                 0     //PC0
#define DIGIT2                 1     //PC1
#define DIGIT3                 2     //PC2
#define OUTPUT_PORT            PORTB 
#define INPUT_PIN                PINB  //Buttons are on PB2-PB4
#define LED_PIN                0     
#define DEBOUNCE_COUNT_MAX     240   
#define TIMESIG_DISPLAY_LENGTH 500 


enum {BUTTON_UP = 2, BUTTON_DOWN, BUTTON_TIMESIG, NUM_BUTTONS} buttons;
enum {FOUR_FOUR, TWO_FOUR, TWO_TWO, THREE_EIGHT, FIVE_EIGHT, SIX_EIGHT, NUM_TIMESIG} timeSigs;


uint8_t speed = 60; //Metronome speed, initialized to 60 BPM
uint8_t current_display = 1; //The current display we are on, used for multiplexing the display
uint16_t button_counter[NUM_BUTTONS];

uint16_t toneFreq = 2000; //Frequency of the tone sent to the buzzer, in Hz
uint8_t buzzPeriod = 40; //The length of the tone, toneFreq * 20ms
uint16_t iCounterMax; //Used for sounding the tone, see the timer1 ISR
uint16_t iCounter = 0;
uint8_t buzzerflag = 0;

uint8_t current_TimeSig = FOUR_FOUR;
uint8_t beatCounter = 0; //Counts the number of beats
uint8_t divisor = 4; //Determined based on time signature

void setup(); //Setup I/O and timers
void setDigit(int digit, int value); //sets the given value to the display at a given digit
void displayTimeSig(); //Displays the current time signature to the display



int main()
{
  setup(); //call setup function to get everything ready
  
  while(1)
  {
    for(int i = 2; i < NUM_BUTTONS; i++)
    {
      if((INPUT_PIN & (1<<i)) == 0)
      {
        button_counter[i]++;
      }
      else
      {
        button_counter[i] = 0;
      }
    }
    
    if(button_counter[BUTTON_UP] == DEBOUNCE_COUNT_MAX)
    {

      while((INPUT_PIN & (1<<BUTTON_UP)) == 0)
      {
        TIMSK1 = 0;
        speed++;
        _delay_ms(50);
      }
        
      TCNT1 = 0; //Restart the timer at zero
      TIMSK1 = (1<<OCIE1A); //Re-enable the interrupt on timer1
    }
    else if(button_counter[BUTTON_DOWN] == DEBOUNCE_COUNT_MAX)
    {
      while((INPUT_PIN & (1<<BUTTON_DOWN)) == 0)
      {
        TIMSK1 = 0;
        speed--;
        _delay_ms(50);
      }
      
      TCNT1 = 0;
      TIMSK1 = (1<<OCIE1A);
    }
    else if(button_counter[BUTTON_TIMESIG] == DEBOUNCE_COUNT_MAX)
    {

      if((current_TimeSig + 1) >= NUM_TIMESIG)
      {
        current_TimeSig = FOUR_FOUR;
      }
      else
      {
        current_TimeSig++;
      }

      switch(current_TimeSig)
      {
        case FOUR_FOUR: divisor = 4;
        break;
        case TWO_TWO: ;
        case TWO_FOUR: divisor = 2;
        break;
        case THREE_EIGHT: divisor = 3;
        break;
        case FIVE_EIGHT: divisor = 5;
        break;
        case SIX_EIGHT: divisor = 6;
      }       
      displayTimeSig();
    }
  }
  
  return 0;
}

void setup()
{
  //Handle Pin Direction Settings
  DDRD = 0xFF; //DISP_CATH_PORT, set all pins as output
  DDRC = 0x07; //DISP_AN_PORT, set pins PC0, PC1, PC2 as output
  DDRB = 0x03; //I/O pins on port B, set pins 0 and 1 as output
  PORTB = 0x1C; //Set internal pull-ups on pins PB2, PB3, PB4
  
  iCounterMax = toneFreq*((float)60/(float)speed);
  
  for(int i = 0; i < NUM_BUTTONS; i++)
  {
    button_counter[i] = 0;
  }
  TCCR0A = (1<<WGM01); //set timer with normal port operation and clear on compare match mode or CTC
  TCCR0B = (1<<CS01) | (1<<CS00); //use clock divided by 64
  OCR0A = 38; //timer cycles at approx. 400Hz, (1MHz/64/400Hz - 1) = 38
  TIMSK0 = (1<<OCIE0A); //Enable match interrupt on compare register A
  
  TCCR1A = (1<<WGM10) | (1<<WGM11); //Fast PWM mode, start with output disabled
  TCCR1B = (1<<WGM12) | (1<<WGM13) | (1<<CS11); //clock divided by 8
  OCR1A = (F_CPU/8)/toneFreq - 1; //This calculation will generate a PWM with the frequency given by toneFreq
  TIMSK1 = (1<<OCIE1A); //Enable match interrupt
  
  sei(); //Enable global interrupt
}

void setDigit(int digit, int value)
{
  switch(digit)
  {
    case DIGIT1: DISP_AN_PORT |= (1<<DIGIT1);
    break;
    case DIGIT2: DISP_AN_PORT |= (1<<DIGIT2);
    break;
    case DIGIT3: DISP_AN_PORT |= (1<<DIGIT3);
    break;
  }
  switch(value)
  {
    case 0: DISP_CATH_PORT = 0b01000000;
    break;
    case 1: DISP_CATH_PORT = 0b01111001;
    break;
    case 2: DISP_CATH_PORT = 0b00100100;
    break;
    case 3: DISP_CATH_PORT = 0b00110000;
    break;
    case 4: DISP_CATH_PORT = 0b00011001;
    break;
    case 5: DISP_CATH_PORT = 0b00010010;
    break;
    case 6: DISP_CATH_PORT = 0b00000010;
    break;
    case 7: DISP_CATH_PORT = 0b01111000;
    break;
    case 8: DISP_CATH_PORT = 0b00000000;
    break;
    case 9: DISP_CATH_PORT = 0b00010000;
  }
}


void displayTimeSig()
{
 
  uint16_t i = 0; //loop control variable i
  int flag = 0; //flag to determine current display to be active
  

  TIMSK0 = 0;
  TIMSK1 = 0;

  DISP_AN_PORT &= ~((1<<DIGIT1) | (1<<DIGIT2) | (1<<DIGIT3));
  DISP_CATH_PORT = 0b11111111;
  
  while(i <= TIMESIG_DISPLAY_LENGTH)
  {
    if(flag == 0)
    {
      DISP_AN_PORT &= ~(1<<DIGIT3); //Disable third digit
      DISP_AN_PORT |= (1<<DIGIT1); //Enable the first digit
      flag = 1; //update flag
      
      //Set the correct digit corresponding to the first number in
      //the time signature
      if(current_TimeSig == FOUR_FOUR)
        DISP_CATH_PORT = 0b00011001;
      else if((current_TimeSig == TWO_TWO) || (current_TimeSig == TWO_FOUR))
        DISP_CATH_PORT = 0b00100100;
      else if(current_TimeSig == THREE_EIGHT)
        DISP_CATH_PORT = 0b00110000;
      else if(current_TimeSig == FIVE_EIGHT)
        DISP_CATH_PORT = 0b00010010;
      else if(current_TimeSig == SIX_EIGHT)
        DISP_CATH_PORT = 0b00000010;
        
    }
    else
    {
      DISP_AN_PORT &= ~(1<<DIGIT1);
      DISP_AN_PORT |= (1<<DIGIT3);
      flag = 0;
      
      if((current_TimeSig == FOUR_FOUR) || (current_TimeSig == TWO_FOUR))
        DISP_CATH_PORT = 0b00011001;
      else if(current_TimeSig == TWO_TWO)
        DISP_CATH_PORT = 0b00100100;
      else if((current_TimeSig == THREE_EIGHT) || (current_TimeSig == FIVE_EIGHT) || (current_TimeSig == SIX_EIGHT))
        DISP_CATH_PORT = 0b00000000;
        
    }
    
    _delay_us(1); //MAY NOT NEED TO BE HERE??????
    i++; //Update counter
  }
  
  //Re-enable the interrupts
  TIMSK0 = (1<<OCIE0A);
  TIMSK1 = (1<<OCIE1A);
}

ISR(TIMER0_COMPA_vect)
{
  if(current_display == DIGIT1)
  {
    if(speed >= 100)
    {
      DISP_AN_PORT &= ~((1<<DIGIT2) | (1<<DIGIT3));
      setDigit(current_display, (speed - (speed % 100))/100);
    }
    current_display = DIGIT2;
  }
  else if(current_display == DIGIT2)
  {
    if(speed >= 10)
    {
      DISP_AN_PORT &= ~((1<<DIGIT1) | (1<<DIGIT3));
      setDigit(current_display, ((speed % 100) - ((speed % 100) % 10))/10);
    }
    current_display = DIGIT3;
  }
  else if(current_display == DIGIT3)
  {
    DISP_AN_PORT &= ~((1<<DIGIT1) | (1<<DIGIT2));
    setDigit(current_display, (speed % 100) % 10);
    current_display = DIGIT1;
  }
}

ISR(TIMER1_COMPA_vect)
{
  iCounter++; //increment interrupt counter
  TCCR1A &= ~(1<<COM1A0); //disable PWM output on pin PB1
  
  if((iCounter == iCounterMax) || buzzerflag == 1)
  {     
    buzzerflag = 1; //set buzzer flag to on
    OUTPUT_PORT ^= (1<<LED_PIN); //toggle LED
    TCCR1A |= (1<<COM1A0); //enable PWM output on pin PB1
    
    //if the buzzer has been on for the specified length of time
    if(iCounter == (iCounterMax + buzzPeriod))
    {
      buzzerflag = 0; //set buzzer flag to off
      iCounter = 0; //reset increment counter
      OUTPUT_PORT &= ~(1<<LED_PIN); //Turn off LED

      if(beatCounter % divisor == 0) 
      {
        toneFreq = 1000;
        buzzPeriod = 20;
        beatCounter++;
      }
      else
      {
        toneFreq = 2000;
        buzzPeriod = 40;
        beatCounter++;
      }
      
      iCounterMax = toneFreq*((float)60/(float)speed); //recalculate interrupt counter max
      OCR1A = (F_CPU/8)/toneFreq; //modify compare value so timer1 fires at proper rate
    }   
    
  }
}

Source: http://www.instructables.com/id/Electronic-Metronome/

Put something in setup() to turn off the anode or cathode outputs.

void setup()
{
  //Handle Pin Direction Settings
  DDRD = 0xFF; //DISP_CATH_PORT, set all pins as output
  DDRC = 0x07; //DISP_AN_PORT, set pins PC0, PC1, PC2 as output
  DDRB = 0x03; //I/O pins on port B, set pins 0 and 1 as output
  PORTB = 0x1C; //Set internal pull-ups on pins PB2, PB3, PB4
  
  iCounterMax = toneFreq*((float)60/(float)speed);
  
  for(int i = 0; i < NUM_BUTTONS; i++)
  {
    button_counter[i] = 0;
  }
  TCCR0A = (1<<WGM01); //set timer with normal port operation and clear on compare match mode or CTC
  TCCR0B = (1<<CS01) | (1<<CS00); //use clock divided by 64
  OCR0A = 38; //timer cycles at approx. 400Hz, (1MHz/64/400Hz - 1) = 38
  TIMSK0 = (1<<OCIE0A); //Enable match interrupt on compare register A
  
  TCCR1A = (1<<WGM10) | (1<<WGM11); //Fast PWM mode, start with output disabled
  TCCR1B = (1<<WGM12) | (1<<WGM13) | (1<<CS11); //clock divided by 8
  OCR1A = (F_CPU/8)/toneFreq - 1; //This calculation will generate a PWM with the frequency given by toneFreq
  TIMSK1 = (1<<OCIE1A); //Enable match interrupt
  
  sei(); //Enable global interrupt
}