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
    About the element14 Community
  • 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 1409 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…
Parents
  • 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
  • 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
  • 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
Reply
  • 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
Children
No Data
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