Up and Down Counter

Aim

UP and Down counter using seven segment display and push buttons.

Components required for the experiment

Arduino UNO Board, USB cable, LED (any colour), 220 ohm Resistors, and Two Push Button Switch.

Circuit Diagram

To practice, make the circuit diagram with the exact pin-to-pin connection as shown below. This will help you to complete the experiment successfully. Click on the image to enlarge.

Code

int A = 2;
int B = 3;
int C = 4;
int D = 5;
int E = 6;
int F = 7;
int G = 8;
int led = 13;

//Initialize the push buttons pins, push buttons states, and the counter
int switchUpPin = 10;
int switchDownPin = 11;
int counter = 0;
int buttonUpState = 0;
int lastButtonUpState = 0;
int buttonDownState = 0;
int lastButtonDownState = 0;

void setup()
{
Serial.begin(9600);
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
pinMode(D, OUTPUT);
pinMode(E, OUTPUT);
pinMode(F, OUTPUT);
pinMode(G, OUTPUT);
pinMode(led,OUTPUT);
 //Start with the deceimal point off
}

void loop()
{
//Getting the reads from the buttons
buttonUpState = digitalRead(switchUpPin);
buttonDownState = digitalRead(switchDownPin);
if(digitalRead(10) == 1)
{
digitalWrite(led, HIGH);
delay(100);
digitalWrite(led,LOW);
}
if(digitalRead(11) == 1)
{
digitalWrite(led, HIGH);
delay(100);
digitalWrite(led,LOW);
}
//Detecting button press and getting the button status
//Do this for the button up
if(buttonUpState != lastButtonUpState)
{
if (buttonUpState == HIGH)
{
//Reset the counter to -1
if(counter == 9)
{
counter = -1;
}
//Increase the counter by 1
counter++;
//Print the counter to the console and calling the function
Serial.println(counter);
changeNumber(counter);
//Delaying by 250 ms
delay(250);
}
else
{
Serial.println("OFF");
}
//Delay to avoid button bouncing
delay(50);
}
//Do this for the button down
if(buttonDownState != lastButtonDownState)
{
if(buttonDownState == HIGH)
{
//Set the counter to 10
if(counter == 0)
{
counter = 10;
}
//Decreases the counter by 1
counter--;
////Print the counter to the console and calling the function
Serial.println(counter);
changeNumber(counter);
//Delaying by 250 ms
delay(250);
}
else
{
Serial.println("OFF");
}
//Delay to avoid button bouncing
delay(50);
}
//Calling the function changeNumber with the arg counter
changeNumber(counter);
}
//The function to display the numbers
void changeNumber(int buttonPress)
{
switch (buttonPress)
{
//number 0
case 0:
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, LOW);
break;
//number 1
case 1:
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
break;
//number 2
case 2:
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, LOW);
digitalWrite(G, HIGH);
break;
//number 3
case 3:
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, HIGH);
break;
//number 4
case 4:
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
break;
//number 5
case 5:
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
break;
//number 6
case 6:
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
break;
//number 7
case 7:
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
break;
//number 8
case 8:
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
break;
//number 9
case 9:
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
break;
}
}

 

Code Explained

int A = 2;
int B = 3;
int C = 4;
int D = 5;
int E = 6;
int F = 7;
int G = 8;
int led = 13;

The code begins with connecting the seven points of the 7-segment LED to Arduino. We also define the led connection to pin 13 of Arduino.

int switchUpPin = 10;
int switchDownPin = 11;
int counter = 0;
int buttonUpState = 0;
int lastButtonUpState = 0;
int buttonDownState = 0;
int lastButtonDownState = 0;

Initialising push button pins as UP and DOWN buttons. Also, initialise the button states for both push buttons and set it to 0. These button states will store the high and low values of push buttons. We create a counter variable to fetch and store the value from both push buttons.

void setup()
{
Serial.begin(9600);
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
pinMode(D, OUTPUT);
pinMode(E, OUTPUT);
pinMode(F, OUTPUT);
pinMode(G, OUTPUT);
pinMode(led,OUTPUT);
 //Start with the deceimal point off
}

Initialize serial communication at 9600 baud rate. Setting up the 7 segment pins and LED connected to arduino as OUTPUT.

void loop()
{
//Getting the reads from the buttons
buttonUpState = digitalRead(switchUpPin);
buttonDownState = digitalRead(switchDownPin);

if(digitalRead(10) == 1)
{
digitalWrite(led, HIGH);
delay(100);
digitalWrite(led,LOW);
}

if(digitalRead(11) == 1)
{
digitalWrite(led, HIGH);
delay(100);
digitalWrite(led,LOW);
}
This is the main loop() function that runs repeatedly.  The value is stored in Up push button and Down push button whenever the push button is pressed. If Up push button connected to pin 10 of arduino is pressed, LED connected to pin 13 of arduino glows for 100 millisecond. The same mechanism works when Down push button, connected to pin 11 of arduino is pressed.

if(buttonUpState != lastButtonUpState)
{
if (buttonUpState == HIGH)
{
//Reset the counter to -1
if(counter == 9)
{
counter = -1;
}
//Increase the counter by 1
counter++;
//Print the counter to the console and calling the function
Serial.println(counter);
changeNumber(counter);
//Delaying by 250 ms
delay(250);
}
else
{
Serial.println("OFF");
}
//Delay to avoid button bouncing
delay(50);
}

When the Up push button is pressed, the 7-segment LED displays 1. It continues till the number 9 appears. If we continue to press the Up push button after 9, the program resets the counter to -1, which eventually displays 0 again if you press the Up push button. The value is also printed on the serial monitor screen.

if(buttonDownState != lastButtonDownState)
{
if(buttonDownState == HIGH)
{
//Set the counter to 10
if(counter == 0)
{
counter = 10;
}
//Decreases the counter by 1
counter--;
////Print the counter to the console and calling the function
Serial.println(counter);
changeNumber(counter);
//Delaying by 250 ms
delay(250);
}
else
{
Serial.println("OFF");
}
//Delay to avoid button bouncing
delay(50);
}

A similar process is followed when The push button is pressed. The counter starts a 9 when you first press a Down push button. As you continue to press the Down push button, number 1 will be displayed. For the next pressing of the Down push button, the code resets the 7-segment LED to 0.

void changeNumber(int buttonPress)
{
switch (buttonPress)
{
//number 0
case 0:
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, LOW);
break;
//number 1
case 1:
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
break;
//number 2
case 2:
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, LOW);
digitalWrite(G, HIGH);
break;
//number 3
case 3:
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, HIGH);
break;
//number 4
case 4:
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
break;
//number 5
case 5:
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
break;
//number 6
case 6:
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
break;
//number 7
case 7:
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
break;
//number 8
case 8:
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
break;
//number 9
case 9:
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
break;
}

This is the change number function, which helps the 7-segment LED display to show the exact number on the screen. 

These lines of code are used when a number 0 to 9 is supposed to display on the 7-segment LED.

Output

  

 

This exercise taught you how to use the Push button switch to control the LED. You have also learned the use of a serial monitor and checked the output. A serial monitor is widely used for debugging your program.