Arduino 2-Digit 7-Segment Display with Buttons | Part 4

by fargasch75

in Arduino, Project

Your ads will be inserted here by

Easy AdSense.

Please go to the plugin admin page to paste your ad code.

This week we modify the original circuit and sketch to include two buttons, one to control each digit of the display.

Here’s what the setup looks like:

2-Digit 7-Segment Display

And here’s the complete sketch:

// www.TheElectronicsHobbyist.com/blog
// Natalia Fargasch Norman
// Dual seven-segment LED Display with buttons
// Common Anode digit 1 pin 10
// Common Anode digit 2 pin 5

//       CA1 G  F  A  B
//        |  |  |  |  |      -> pins and segments they control
//   ---------    ---------
//   |   A   |    |   A   |
//  F|       |B  F|       |B
//   |---G---|    |---G---|
//  E|       |C  E|       |C
//   |   D   |    |   D   |
//   ---------    ---------
//        |  |  |  |  |      -> pins and segments they control
//        D  DP E  C CA2         

// Segments that make each number when lit:
// 0 => -FEDCBA
// 1 => ----BC-
// 2 => G-ED-BA
// 3 => G--DCBA
// 4 => GF--CB-
// 5 => GF-DC-A
// 6 => GFEDC-A
// 7 => ----CBA
// 8 => GFEDCBA
// 9 => GF-DCBA

// Arduino digital pins used to light up
// corresponding segments on the LED display
#define A 3
#define B 2
#define C 6
#define D 8
#define E 7
#define F 4
#define G 5

// Pushbuttons connected to pins 9 and 10
#define BTN1 9
#define BTN2 10

// Pins driving common anodes
#define CA1 13
#define CA2 12

// Pins for A B C D E F G, in sequence
const int segs[7] = { A, B, C, D, E, F, G };

// Segments that make each number
const byte numbers[10] = { 0b1000000, 0b1111001, 0b0100100, 0b0110000, 0b0011001, 0b0010010,
0b0000010, 0b1111000, 0b0000000, 0b0010000 };

int digit1 = 0;
int digit2 = 0;

void setup() {
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(BTN1, INPUT);
  pinMode(BTN2, INPUT);
  pinMode(CA1, OUTPUT);
  pinMode(CA2, OUTPUT);
}

void loop() {

  // check button1
  int val1 = digitalRead(BTN1);
  if (val1 == HIGH) {
    digit1++;
    digit1 %= 10;
    delay(10);
  }

  // check button2
  int val2 = digitalRead(BTN2);
  if (val2 == HIGH) {
    digit2++;
    digit2 %= 10;
    delay(10);
  }

  // display number
  unsigned long startTime = millis();
  for (unsigned long elapsed=0; elapsed < 600; elapsed = millis() - startTime) {
    lightDigit1(numbers[digit1]);
    delay(5);
    lightDigit2(numbers[digit2]);
    delay(5);
  }
}

void lightDigit1(byte number) {
  digitalWrite(CA1, LOW);
  digitalWrite(CA2, HIGH);
  lightSegments(number);
}

void lightDigit2(byte number) {
  digitalWrite(CA1, HIGH);
  digitalWrite(CA2, LOW);
  lightSegments(number);
}

void lightSegments(byte number) {
  for (int i = 0; i < 7; i++) {
    int bit = bitRead(number, i);
    digitalWrite(segs[i], bit);
  }
}

Here’s a video of the Arduino 2-digit 7-segment display counter with buttons in action.

{ 12 comments… read them below or add one }

Eric

Can I ask why you are looping for 0.6 seconds (600 ms) on the “Display Number” part? And doesn’t that mean that you will keep getting “stuck” in this loop for 0.6 seconds each time, and that you will miss the button presses (unless they are holding it long enough for you to come out of the loop)?

Reply

Natalia

Yes, but I’ve found that keeping the current number lit for that amount of time allows us to better see what number is being displayed. The code is simple enough for beginners to understand, even if it comes at some performance cost.

Reply

computer

Hi natalia!!Immediatly question??
Design a logic circuit (?) that can convert the (five-bit) output of the full adder into appropriate values
for seven segment decoders in order to show the resulting sum on display devices as decimal values.
Examples:
+ if A=1111 (15) and B=0101 (5) then the values on the displays should be (15+5=20) Display1=2
and Display0=0.
+ if A=1010 (10) and B=1000 (8) then the values on the displays should be (10+8=18) Display1=1
and Display0=8.
+ if A=0010 (2) and B=0111 (7) then the values on the displays should be (2+7=9) Display1=0 and
Display0=9.
1

Reply

Natalia

Hi,
unfortunately I don’t do these circuits… yet. I’m only a beginner in electronics, and so far I’m most comfortable doing microcontroller programming… sorry.

Reply

ronald

hi! this helps a lot for a beginner like ,can i ask how to modify the code having only one button to control the count one at a time up to 99 count?

thanks very much!
best regards,
ronald

Reply

Natalia

Hi Ronald,

to have the system count up using just one button you will keep the units digit to be the modulus (%) of the current count by 10, and the tenths digit to be the integer division (/) of the current count by 10. Something like:

// check button
int val = digitalRead(BTN);
if (val == HIGH) {
count++;
unitsDigit = count % 10;
tensDigit = count / 10;
delay(10);
}

Reply

ronald

thanks Natalia, i will try your suggestions ….will post result after..

br,
ronald

Reply

ronald

wow it works like a charm ……
thank you very much Natalie! your the best…

br,
ronald

Reply

ronald

hi Natalie, I found out that after the count reach 99 ,it displays some number and none number characters ..how to modify this code so that when count reach 99 it reset to 00 and count again when button is press…

br,
ronald

Reply

Natalia

Hi Ronald,

the easiest way to do this is to instead of using the variable count itself as the number to display, use “count % 100″, which will yeld 0 when count is 100, 1 when count is 101, and so on. Kinda like you did using the modulo operation on the tens unit display.

So you can create another variable, say “display”, which will be “display = count % 100;”, and then instead of using count you use display. (as in “unitsDigit = display % 10;”) does that make sense? let me know how that works.

Reply

ronald

hi there,
i was able to experiment and add some to your given code and it works to my expectation …
int val = digitalRead(BTN);
if (val == HIGH) {
if (count<99) count++;
else count = 00;
unitsDigit = count % 10;
tenthsDigit = count / 10;
delay(10);

will try your suggestions if same result will came out.. I'am still on a learning stage
thanks a lot..
btw,
if you could suggest how to include alarm or buzzer (a ding dong i guest ^_^) once button press….

Reply

Natalia

Hi Ronald,

check http://www.theelectronicshobbyist.com/blog/phase-0-of-the-ipon-project/ for an example of how I used sound output in response to an input signal from a switch and a force sensor. I played a more elaborate sound, but you can output only a buzzing sound if you want (by not varying the frequency like I did).

Reply

Leave a Comment

Previous post:

Next post: