element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
element14's The Ben Heck Show
  • Challenges & Projects
  • element14 presents
  • element14's The Ben Heck Show
  • More
  • Cancel
element14's The Ben Heck Show
Forum 4 digit Button Press Counter
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join element14's The Ben Heck Show to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Suggested Answer
  • Replies 10 replies
  • Answers 1 answer
  • Subscribers 32 subscribers
  • Views 1327 views
  • Users 0 members are here
  • 7segmentdisplay
  • arduino
Related

4 digit Button Press Counter

jezt48@gmail.com
jezt48@gmail.com over 9 years ago

I am trying to make a button press counter. I currently am using an arduino as I have lots of them lying around and I cant afford any thing else. I have a four digit 7 segment display rigged up to an arduino uno. I need as I get easily get mad at work and I want to discretely find out how many times people make me mad throughout the day. At the moment it just reads the serial port/ RXTX lines but I need it to count button presses. Can someone help me out?

 

My code is:

 

[code]

int aPin = 2;  //                    

int bPin = 3;  //       

int cPin = 4;  //       

int dPin = 5;  //   

int ePin = 6;  //       

int fPin = 7;  //        

int gPin = 8;  //        

int GND1 = 9;  //      

int GND2 = 10; //  

int GND3 = 11; //    

int GND4 = 12; //      

int num;      

int dig1 = 0;

int dig2 = 0;

int dig3 = 0;

int dig4 = 0;

int DTime = 4;

 

 

 

 

void setup()

{

  pinMode(aPin, OUTPUT);

  pinMode(bPin, OUTPUT);

  pinMode(cPin, OUTPUT);

  pinMode(dPin, OUTPUT);

  pinMode(ePin, OUTPUT);

  pinMode(fPin, OUTPUT);

  pinMode(gPin, OUTPUT);

  pinMode(GND1, OUTPUT);

  pinMode(GND2, OUTPUT);

  pinMode(GND3, OUTPUT);

  pinMode(GND4, OUTPUT);

  Serial.begin(9600);

}

void loop()

{

  digitalWrite( GND1, HIGH);

  digitalWrite( GND2, HIGH);

  digitalWrite( GND3, HIGH);

  digitalWrite( GND4, HIGH);

 

 

if (Serial.available() > 0)

{

  num = Serial.parseInt();

  Serial.println(num);

  dig1 = num / 1000;

  num = num - (dig1 * 1000);

  dig2 = num / 100;

  num = num - (dig2 * 100);

  dig3 = num / 10;

  dig4 = num - (dig3 *10);

}

 

 

  digitalWrite( GND4, LOW);    //digit 4

  pickNumber(dig4);

  delay(DTime);

  digitalWrite( GND4, HIGH);

 

  digitalWrite( GND3, LOW);    //digit 3

  pickNumber(dig3);

  delay(DTime);

  digitalWrite( GND3, HIGH);

 

  digitalWrite( GND2, LOW);   //digit 2

  pickNumber(dig2);

  delay(DTime);

  digitalWrite( GND2, HIGH);

 

  digitalWrite( GND1, LOW);   //digit 1

  pickNumber(dig1);

  delay(DTime);

  digitalWrite( GND1, HIGH);

 

 

}

 

void pickNumber(int x){

   switch(x){

     case 1: one(); break;

     case 2: two(); break;

     case 3: three(); break;

     case 4: four(); break;

     case 5: five(); break;

     case 6: six(); break;

     case 7: seven(); break;

     case 8: eight(); break;

     case 9: nine(); break;

     default: zero(); break;

   }

}

 

 

void clearLEDs()

{ 

  digitalWrite(  2, LOW); // A

  digitalWrite(  3, LOW); // B

  digitalWrite(  4, LOW); // C

  digitalWrite(  5, LOW); // D

  digitalWrite(  6, LOW); // E

  digitalWrite(  7, LOW); // F

  digitalWrite(  8, LOW); // G

}

 

 

void one()

{

  digitalWrite( aPin, LOW);

  digitalWrite( bPin, HIGH);

  digitalWrite( cPin, HIGH);

  digitalWrite( dPin, LOW);

  digitalWrite( ePin, LOW);

  digitalWrite( fPin, LOW);

  digitalWrite( gPin, LOW);

}

 

 

void two()

{

  digitalWrite( aPin, HIGH);

  digitalWrite( bPin, HIGH);

  digitalWrite( cPin, LOW);

  digitalWrite( dPin, HIGH);

  digitalWrite( ePin, HIGH);

  digitalWrite( fPin, LOW);

  digitalWrite( gPin, HIGH);

}

 

 

void three()

{

  digitalWrite( aPin, HIGH);

  digitalWrite( bPin, HIGH);

  digitalWrite( cPin, HIGH);

  digitalWrite( dPin, HIGH);

  digitalWrite( ePin, LOW);

  digitalWrite( fPin, LOW);

  digitalWrite( gPin, HIGH);

}

 

 

void four()

{

  digitalWrite( aPin, LOW);

  digitalWrite( bPin, HIGH);

  digitalWrite( cPin, HIGH);

  digitalWrite( dPin, LOW);

  digitalWrite( ePin, LOW);

  digitalWrite( fPin, HIGH);

  digitalWrite( gPin, HIGH);

}

 

 

void five()

{

  digitalWrite( aPin, HIGH);

  digitalWrite( bPin, LOW);

  digitalWrite( cPin, HIGH);

  digitalWrite( dPin, HIGH);

  digitalWrite( ePin, LOW);

  digitalWrite( fPin, HIGH);

  digitalWrite( gPin, HIGH);

}

 

 

void six()

{

  digitalWrite( aPin, HIGH);

  digitalWrite( bPin, LOW);

  digitalWrite( cPin, HIGH);

  digitalWrite( dPin, HIGH);

  digitalWrite( ePin, HIGH);

  digitalWrite( fPin, HIGH);

  digitalWrite( gPin, HIGH);

}

 

 

void seven()

{

  digitalWrite( aPin, HIGH);

  digitalWrite( bPin, HIGH);

  digitalWrite( cPin, HIGH);

  digitalWrite( dPin, LOW);

  digitalWrite( ePin, LOW);

  digitalWrite( fPin, LOW);

  digitalWrite( gPin, LOW);

}

 

 

void eight()

{

  digitalWrite( aPin, HIGH);

  digitalWrite( bPin, HIGH);

  digitalWrite( cPin, HIGH);

  digitalWrite( dPin, HIGH);

  digitalWrite( ePin, HIGH);

  digitalWrite( fPin, HIGH);

  digitalWrite( gPin, HIGH);

}

 

 

void nine()

{

  digitalWrite( aPin, HIGH);

  digitalWrite( bPin, HIGH);

  digitalWrite( cPin, HIGH);

  digitalWrite( dPin, HIGH);

  digitalWrite( ePin, LOW);

  digitalWrite( fPin, HIGH);

  digitalWrite( gPin, HIGH);

}

 

 

void zero()

{

  digitalWrite( aPin, HIGH);

  digitalWrite( bPin, HIGH);

  digitalWrite( cPin, HIGH);

  digitalWrite( dPin, HIGH);

  digitalWrite( ePin, HIGH);

  digitalWrite( fPin, HIGH);

  digitalWrite( gPin, LOW);

}

[/code]

  • Sign in to reply
  • Cancel

Top Replies

  • shabaz
    shabaz over 9 years ago +1
    Hi Chris, I don't use Arduino enough to help you with the entire code, you'll still have to try to code it, but I can help to suggest how to start off. The current code is not very clean . Check out this…
  • beacon_dave
    beacon_dave over 9 years ago +1
    Which pin is your button connected to as you'll need to set up an input and probably a bit of debounce code to tame it. This tutorial may help you: Tutorial 02 for Arduino: Buttons, PWM, and Functions…
  • shabaz
    0 shabaz over 9 years ago

    Hi Chris,

     

    I don't use Arduino enough to help you with the entire code, you'll still have to try to code it, but I can help to

    suggest how to start off. The current code is not very clean. Check out this link: Arduino Playground - SevenSegmentLibrary

    With that, you can shrink your code down (to perhaps a dozen lines total) and make it usable.

    If you really want to learn how to implement from scratch then it is worth reading up on arrays and loops

    and look-up tables.

     

    The underlying code mentioned at the link above does all that for you in the background.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • beacon_dave
    0 beacon_dave over 9 years ago

    Which pin is your button connected to as you'll need to set up an input and probably a bit of debounce code to tame it.

     

    This tutorial may help you:

    Tutorial 02 for Arduino: Buttons, PWM, and Functions

    Also on YouTube:

    https://www.youtube.com/watch?v=_LCCGFSMOr4

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • balearicdynamics
    0 balearicdynamics over 9 years ago

    Hello Chris,

     

    your code architecture in line of principle seems well written and almost readable, so we can start from a good point. I suppose that the meaning of your variables assignment is related with the name. So please give us the following information for a better understanding:

     

    First of all, it will be very helpful to know the circuit you are doing. What is the counter display you are using? How did you connected it ?

    Second point - that sounds a bit weird is the GNDn series of variables you set. how are these connected to ? Are you just sending the counter result to the serial? How are the buttons (or single button?) connected?

     

    Then with a more clear schematics in mind I think that we can find the issue solution easily.

     

    Thank you Enrico.

     

    BTW there are some optimisations in the code that can be applied for a better readability and efficiency but IMHO this will be considered in a second moment.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • jezt48@gmail.com
    0 jezt48@gmail.com over 9 years ago in reply to beacon_dave

    Hi Dave,

     

    I currently don't have the button connected as it didn't work but I might have to put it on pin 13 as it is the last digital pin left.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • jezt48@gmail.com
    0 jezt48@gmail.com over 9 years ago in reply to balearicdynamics

    The model number of the 4 digit 7 segment display is '5461as'. The segment anodes are on pins 2 through 8 and the common cathode of each digit are on 9 through 12. 0 and 1 have my Bluetooth module connected just for testing and it (the Bluetooth module) does not require any code in this configuration. I did not make this code I found it here: Arduino 4 digit 7 segment display - All . I don't know what it means by GNDn. I am sending the number to the display via the serial monitor and my Bluetooth module, and I would like the button to be connected to pin 13.

     

    Thanks for your help everyone I know that you will find it!

    Chris Toms

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • balearicdynamics
    0 balearicdynamics over 9 years ago in reply to jezt48@gmail.com

    Hello Chris,

     

    well, as you says it sounds strange the GND series of pins. I have not checked in depth the features of this device but it works in a very similar way than a series of four separate 7 segments. Take a look to this two parts article I wrote in past on electroschematics: Arduino Daisy Chain Shift Register with 74HC595 - Part 1

    It's time to update it and publish a more complete version on Element14, it seems.

     

    To reduce the number of digital pins used on the Arduino board I have used four daisy-chained shift registers (one for every 7-segments digit) but despite this addressing difference the hardware connection and the schematics + pcb maybe helpful for you.

     

    About the code of the example you mention instead, consider that this display has probably an internal latch with shift register so you should send every digit in sequence. As to every pin corresponds a different LED, you compose the number (or any other possible design with 7 digits) then you should address the digit in the desired position. Every digit on the component has a separate GND. If you see the GND1-4 pins are not really the GND we expect but are the four address pin of every digit. As far as I see in the code, the sequence to set a specific digit is to set the corresponding GNDn (the selector pin) to LOW, write the digital states on the 7 segments then set the selector pin to HIGH. Just like an address enable switch. After the operation, I suppose, as far the address signal is high the 7 segments configuration remain latched in the device.

     

    Last about the code, was just the use of a lot of constants while it was possible to make it easier and in a more flexible way with the use of arrays.

     

    Enrico

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • beacon_dave
    0 beacon_dave over 9 years ago in reply to jezt48@gmail.com

    In case you are not aware you can use the analog inputs for digital IO as well, if you need extra connectivity.

    https://www.arduino.cc/en/Tutorial/AnalogInputPins

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • beacon_dave
    0 beacon_dave over 9 years ago in reply to jezt48@gmail.com

    The GNDn is just likely to be referring to the common pins of each digit in the LED display module.

     

    You really need to get hold of a data sheet for that display. I suspect that it may just be a passive display and not a display module with built in logic.

     

    If so, it is using a technique more commonly known as 'multiplexing' and relies on 'persistence of vision'. Basically you cycle through the digits one at a time, turning them on by enabling the GNDn/common and driving the 7 segments to make up the number you want to display. You then disable the GNDn/common then move onto the next digit. If you were to do this in slow motion you would see each digit turn on and off as the code looped. If you do this repeatedly and fast enough though, to the eye it looks like all digits are continually lit.

     

    However with this design you are at risk of damaging your Arduino due to the collective currents which can end up flowing through the four common pins. LED's typically require around 15-20mA to display at full brightness, however your Atmel processor can only sink/source a relatively small amount of current, usually around 40mA or so. If you display an '8' then you are driving all 7 LED segments so the there will be around 105-140mA total current at the GND/common pins which is likely to exceed that of your Atmel processor. If driving at 15-20mA per segment then you need to consider a transistor or some other driver device on these 4 pins that can safely handle the currents. Alternatively you need to multiplex through each segment so as you are only ever drawing 20mA at a time. Or failing that limit the current through each segment LED to around 5.5mA but that will make them rather dimly lit, especially when combined with the multiplexing.

     

    Here is a rather good animated gif of how the multiplex works in practice and also shows the transistors on the GNDn/common pins:

    http://hackyourmind.org/public/images/display7seg_anim.gif

     

    5461-AS datasheet:

    www.ledtoplite.com/uploadfile/2014/0807/20140807053741105.pdf

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • beacon_dave
    0 beacon_dave over 9 years ago in reply to jezt48@gmail.com

    Another link that you may find of use:

    led - How to use a common anode 7-segment, 4-digit display? - Arduino Stack Exchange

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • hamzahassan
    0 hamzahassan over 7 years ago

    Hi Chris

     

    Is there any way to decode the output of 4 digit seven segment.Basically, I want to convert the 4 digit 7 segment pattern into decimal number.Please help if  you have any idea .

     

    Thanks

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube