# Counter 0 - 99 up'n down with 2 display 7 segments

We program an Arduino sketch that creates an up/down counter from 0 to 99 managed by 2 buttons, one for increasing and the other for decreasing, and the system prints the status of the count on 2 seven-segment displays. The circuit is shown in the figure and can also be implemented in practice.
The two buttons are mounted in pull-down with 10kOhm resistors. Arduino accepts a voltage of 5V for the high level and 0V for the low level on the digital pins. The display LEDs are protected with 220Ohm resistors. Arduino generates a voltage of 5V on the digital pins for the high level and supplies a current of about 40mA.

In the sketch below I found a software solution to button bouncing. For information on how to program the debounce of a button you can see my article: https://www.filipposfactory.com/index.php/8-arduino/37-switch-debouncing

Also for information on how to use the Arduino Uno port registers see this article of mine: https://www.filipposfactory.com/index.php/8-arduino/35-counter-0-9-with-7-segments-display-and-arduino-uno or you go to Arduino site: https://www.arduino.cc/en/Reference/PortManipulation.

int d, u, i=0;
// look-up table to encode numbers on display
byte numero [10]={B00111111, B00000110, B01011011, B01001111, B01100110,
B01101101, B01111101, B00000111, B01111111, B01101111 };

// boolean state variables:
// 1 = true = HIGH, 0 = false = LOW
int upnow=1;
int upbefore=1;
int downnow=1;
int downbefore=1;

void setup() {
// pinMode OUTPUT to digital pins from 0 to 7
DDRD = B11111111;
/* pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,INPUT);
pinMode(11,INPUT);
*/
DDRB = B11110011;
}

void loop() {
// Arduino reads the buttons in polling

// up button pressed
if((upnow==HIGH) && (upbefore==0))
{
if (i<99)
i++;
}
upbefore=upnow;

// down button pressed
if((downnow==HIGH) && (downbefore==0))
{
if (i>0)
i--;
}
downbefore=downnow;

// display management: encoding from decimal 0-99
// to 2 digits for 7 segment displays
d=i/10;
u=i-(d*10);

// print tens
digitalWrite(9,LOW);
digitalWrite(8,HIGH);
PORTD=numero[d];
delay(10);

// print units
digitalWrite(8,LOW);
digitalWrite(9,HIGH);
PORTD= numero[u];
delay(15);
}

The displays are of the common cathode type. The cathode (central pin, above or below) must then be grounded, while to illuminate each of the LEDs it is necessary to supply the relative pin with a positive voltage of about 1.2V. If an impulsive signal is applied to the common cathode, current can be saved by turning on the displays intermittently, but with a frequency such as to make the image appear stable; this technique, called duration modulation
pulse (PWM: Pulse Width Modulation) takes advantage of the properties of the human eye to present a certain persistence of the image on the retina and to possess a lower sensitivity towards impulsive signals.
Thus the respective LEDs of the two displays are connected together and to the same Arduino pin of PORTD (from 0 to 7). Two NPN type BJTs with a 1kOhm resistor connected to the base operate in an ON-OFF configuration. The bases are connected to pins 8 or 9 of the Arduino. When there is 0 voltage in the base, the relative transistor is off, while when I supply 5V, the transistor is on and so the dipslay to which its collector is connected is active.
The sketch is programmed so that to print the tens it sends a HIGH signal to the BJT connected to its cathode, LOW to the other BJT and on the PORTD pins it sends the tens digit; while a few milliseconds later it sends a HIGH signal to the BJT connected to the cathode of the display that displays the units, LOW to the BJT of the tens and on the pins of PORTD it sends the digit of the units; now wait a few milliseconds and this repeats cyclically.