Counter with 7-Segment-Display and two push buttons

When you press one of the push-buttons you increase the number shown on the 7-segment-display. When you press the other push-button you decrease the number shown on the 7-segment-display.

Material:

Video:

Hardware:

The segments for each digit are small LEDs and they are named from A to G like this:

Each 7-segment-display type has a different pinout, you should check the datasheet to use the correct pins!

We will use here the dual Display 3261B, with common anodes:

Dual 7-Segment-Display 3261B
Pin numbers from 1 to 10 and
Displays segments (LEDs) from A to G

We will control each of the cathodes (pin) from each LED or segment (A to G) with the Arduino to display the number we want:

To control the 7-segment-display with the Arduino we will use this time the library SevSeg. Here is more information about this arduino library:

https://www.arduino.cc/reference/en/libraries/sevseg/

Learn here how to install libraries into your Arduino IDE:

https://www.arduino.cc/reference/en/libraries/

There are many other libraries to control displays:

https://www.arduino.cc/reference/en/libraries/category/display/

To control the 7-segment-display with Multiplexing without a library see this blog:

Each cathode needs a resistor, otherwise the LED will burn! If you use resistors only on the anodes, the current will not be distributed the same for each cathode and the segments (LEDs) could not light up homogeneously.

Display’s segment ON (red) / OFF (white):

0 1 2 3 …

Connections to Arduino:

The push-buttons will be connected to pin 11 (counter up) and pin 12 (counter down).

The push-buttons are mechanical devices that require „debouncing“. The code has to check again the input after a certain time to make sure there is a real change of state on the push-button and not only noise or a „flickering“ from the last push. Learn more about debouncing push-buttons here:

https://docs.arduino.cc/built-in-examples/digital/Debounce

Code:

/**** Counter using a dual 7-Segment-Display 3261B (small) with common anodes and two push-buttons*****/
/***** MStronik.blog ***************************************************************************************/

#include "SevSeg.h"

SevSeg mv7seg;
// Define the cathode pins for the 7-Segment Display:
#define a 3 //pin 3 in SSD
#define b 9 //pin 9 in SSD
#define c 8 // pin 8 in SSD
#define d 6 //pin 6 in SSD
#define e 7 //pin 7 in SSD
#define f 4 //pin 4 in SSD
#define g 2 //pin 1 in SSD
#define DP 1 //pin 2 in SSD
// define the anodes:
int Dig1 = 5;
int Dig2 = 10;

byte Anode_Array[] = {Dig1,Dig2};
byte Seg_Array[] = {a,b,c,d,e,f,g,DP};
bool resistorsOnSegments = true;
byte hardwareConfig = COMMON_ANODE; 
byte numDigits = 2;

int PB1_Pin = 11; // Push Button to count up
int PB2_Pin = 12; // Push Button to count down
int PB1_Value, PB2_Value;
int PB1_State = 0;
int last_PB1Val = 0;
int PB2_State = 0;
int last_PB2Val = 0;
int counter;
bool flag_count;

unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 100;    // the debounce time; increase if the output flickers

void setup() {

  mv7seg.begin(hardwareConfig, numDigits, Anode_Array, Seg_Array,resistorsOnSegments);
  mv7seg.setBrightness(90);
  counter = 0;
  flag_count = false;
  pinMode(PB1_Pin, INPUT_PULLUP);
  pinMode(PB2_Pin, INPUT_PULLUP);

  Serial.begin(9600);
}

void loop() {
 
  PB1_Value = digitalRead(PB1_Pin);
  if (PB1_Value != last_PB1Val){
   lastDebounceTime = millis();
   last_PB1Val = PB1_Value;
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    
    if (PB1_State == LOW && PB1_Value == HIGH) {
        flag_count = true;
        counter++;
      }
    else if(PB1_State == HIGH && PB1_Value == LOW){
      flag_count = false;
    }
   PB1_State = PB1_Value;
  }
  //Read Button 2
 PB2_Value = digitalRead(PB2_Pin);
  if (PB2_Value != last_PB2Val){
   lastDebounceTime = millis();
   last_PB2Val = PB2_Value;
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    
    if (PB2_State == LOW && PB2_Value == HIGH) {
        flag_count = true;
        counter--;
      }
    else if(PB2_State == HIGH && PB2_Value == LOW){
      flag_count = false;
    }
   PB2_State = PB2_Value;
  }
  
  if ((counter > 99)||(counter < 0)){
    counter = 0;
  }
  
   static unsigned long timer = millis();
   if (millis() - timer >= 500) {
      timer += 300;
      mv7seg.setNumber(counter, 1);
    }
    mv7seg.refreshDisplay();  
  //Serial.println(counter);
}

Hinterlasse einen Kommentar

Diese Seite verwendet Akismet, um Spam zu reduzieren. Erfahre, wie deine Kommentardaten verarbeitet werden..