HC4LED Scoreboard

when i use this code above, i have to hold the up and down buttons for at least 1 second, is there a way to minimize this delay?

Well, down towards the end you have delay(1000); - that's your one second delay. Another "feature" of this method is that it'll repeat once a second if you continue to hold it.

You are using a method called polling - you poll your inputs once a second to see if they have changed. You can cut down this interval to reduce the delay.

To avoid multiple counts with a single button press, you will need use timers to see how long it's been since you have acted on a button press, or use flags to see if you are dealing with the situation where the button was just pressed and you need to act on it, or the button is still pressed and you have already acted on it.

Here's a quick example of handling a single button using flags to make sure we only count once per button press:

void loop()
{
  static byte player_one_up_pressed = 0;

  if (digital_read(player_one_up_pin))
  {
     if (!player_one_up_pressed)
     {
         player_one_up_pressed = 1;
         player_one_score++;
     }
  }
  else // player_one_up_pin is 0
  {
    player_one_up_pressed = 0;
  }

  display_score();
  delay(200);  // debounce delay
}

The only magic here is the static directive, which tells the compiler to keep the value of this variable between function calls. Normally variables are automatic, which means they are allocated and initialized when the function starts, then go away when the function returns.

Another way is to use interrupts instead of polling, but the ATmega8/168 only has two interrupts (which leaves you short since you want to count up and down for each player for a total of 4 inputs).

There's another issue known as debounce, so some sort of delay is desirable so that you don't get multiple hits as the signal "settles" to a steady state (usually happens when the signal goes positive).

-j