SHARE
TWEET

Counter SG

a guest Dec 3rd, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. //define bit-patterns for 7 segment display
  3. const int  valuesTable[10] = {
  4.                       B11111001, //1
  5.                       B10100100, //2
  6.                       B10110000, //3
  7.                       B10011001, //4
  8.                       B10010010, //5
  9.                       B10000011, //6
  10.                       B11111000, //7
  11.                       B10000000, //8
  12.                       B10011000, //9
  13.                       B11000000  //0
  14.                     };
  15.  
  16. const int dataPin = 8;   //74HC595  pin 8 DS
  17. const int latchPin = 9;  //74HC595  pin 9 STCP
  18. const int clockPin = 10; //74HC595  pin 10 SHCP
  19.  
  20.  
  21. const int display1 = 7; // displays numbered from left to right
  22. const int display2 = 6;
  23. const int display3 = 5;
  24. const int display4 = 4;
  25.  
  26. int buttonPin = 3;     // the number of the pushbutton pin
  27.  
  28. int countedNumber = 0;
  29. int currentButtonState = 0;        // current state of the button
  30. int previousButtonState = 0;       // previous button state
  31.  
  32. void setup() {
  33.   Serial.begin(9600);
  34.  
  35.   // declare button pin as input
  36.   // latch, clock, data (part of 74HC595, connected to counter) are output
  37.   // each display is an output  
  38.  
  39.   pinMode(buttonPin, INPUT_PULLUP);
  40.  
  41.   pinMode(latchPin,OUTPUT);
  42.   pinMode(clockPin,OUTPUT);
  43.   pinMode(dataPin,OUTPUT);
  44.  
  45.   pinMode(display1, OUTPUT);
  46.   pinMode(display2, OUTPUT);
  47.   pinMode(display3, OUTPUT);
  48.   pinMode(display4, OUTPUT);
  49. }
  50.  
  51.  
  52. void allDisplaysOff() {
  53.   // turn all cathode drivers off so that no leading zeros show
  54.   digitalWrite(display1, LOW);
  55.   digitalWrite(display2, LOW);
  56.   digitalWrite(display3, LOW);
  57.   digitalWrite(display4, LOW);  
  58. }
  59.  
  60.  
  61. // function controls the numbers on display
  62. void displayDigit(int whichDisplay, byte valueToDisplay) {
  63.  
  64.   digitalWrite(latchPin, LOW); // turn off shift register pins while shifting bits
  65.  
  66.   allDisplaysOff(); // turn off all cathode drivers
  67.  
  68.   shiftOut(dataPin,clockPin, LSBFIRST, valueToDisplay); //shift the bits out
  69.  
  70.   digitalWrite(latchPin, HIGH); // set latch high to set segments
  71.  
  72.   digitalWrite(whichDisplay, HIGH); // drive on cathode low to durn on display
  73.  
  74.   delay(100);
  75.  
  76. }
  77.  
  78.  
  79. void segmentDisplay(int number){
  80.   int d1, d2, d3, d4; // temp values for thousands, hundreds, tens, ones
  81.  
  82.  
  83.   d1 = (int) (number / 1000); // get thousands
  84.   number = number - (d1 * 1000);  // subtract thousands from number
  85.  
  86.   d2 = (int) (number / 100); // get hundreds
  87.   number = number - (d2 * 100); // subtract hundreds from number
  88.  
  89.   d3 = (int) (number / 10); // get tens
  90.  
  91.   d4 = number - (d3 * 10); // subtract tens from number to obtain d4
  92.  
  93.   displayDigit(display1, byte(valuesTable[d1])); // display thousands
  94.   displayDigit(display2, byte(valuesTable[d2])); // display hundreds
  95.   displayDigit(display3, byte(valuesTable[d3])); // display tens
  96.   displayDigit(display4, byte(valuesTable[d4])); // display ones
  97.  
  98.  
  99. }
  100.  
  101. // function keeps track of number of button presses, saves in variable called countedNumber
  102. void buttonCount() {
  103.  
  104.   currentButtonState = digitalRead(buttonPin);      //read state of current pushbutton value, being either low or high
  105.  
  106.   //off is 1, on is 0 for PULLUP
  107.  
  108.   if(currentButtonState != previousButtonState){  // if previous button state and current button state are not equal
  109.     if(currentButtonState == 0) {                 // and if current button state is ON
  110.       countedNumber++;                            // increase counted number by 1
  111.      
  112.       if (countedNumber > 9999) {                 // if counted number exceeds 9999, reset number to 0
  113.         countedNumber = 0;
  114.       }
  115.  
  116.       if (countedNumber < 0) {                    // if counted number is less than 0, make it equal 0
  117.         countedNumber = 0;
  118.  
  119.       segmentDisplay(countedNumber);
  120.      
  121.       }
  122.     }
  123.   }
  124. previousButtonState = currentButtonState; // save current state as last state for next time through the loop
  125.  
  126. delay(200);
  127.  
  128. }
  129.  
  130.  
  131. void loop() {
  132.  
  133. buttonCount();
  134. Serial.print("Current Button State: ");
  135. Serial.print(currentButtonState);
  136. Serial.print('\t');
  137. Serial.print("Counted Number: ");
  138. Serial.print(countedNumber);
  139. Serial.println('\t');
  140.  
  141.  
  142.  
  143. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top