Advertisement
andrewb

digit_counter.ino

Jul 19th, 2014
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. /*****
  2.  * Ingrediients:
  3.  * Yourduino Nano
  4.  * 74HC595 shift register (https://www.sparkfun.com/products/733)
  5.  * 7-segment, 4-digit dispay (https://www.sparkfun.com/products/retired/12669)
  6.  * Bunch of wires
  7.  * Breadboard
  8.  ***
  9.  * On YouTube: https://www.youtube.com/watch?v=84Tbc-9MZ5E
  10.  *****/
  11.  
  12. // Setup the 74HC595 SIPO pins
  13. const int latchPin = 6;   // ST_CP (RCLK)
  14. const int clockPin = 7;   // SH_CP (SRCLK)
  15. const int dataPin = 5;    // DS (data serial)
  16.  
  17. // display pins
  18. const int AD = 9;
  19. const int BD = 10;
  20. const int CD = 11;
  21. const int DD = 12;
  22. const int DEMAX = 4;
  23. const int disp_elements[DEMAX] = {AD, BD, CD, DD};
  24. int disp_digits[DEMAX] = {0,0,0,0};
  25. int disp_count = 0;
  26.  
  27. // Binary vals for decimal numbers
  28. const int NMAX = 16;
  29. const int n[NMAX] = {0b11000000,  // 0
  30.                      0b11111001,  // 1
  31.                      0b10100100,  // 2
  32.                      0b10110000,  // 3
  33.                      0b10011001,  // 4
  34.                      0b10010010,  // 5
  35.                      0b10000010,  // 6
  36.                      0b11111000,  // 7
  37.                      0b10000000,  // 8
  38.                      0b10010000,  // 9
  39.                      0b00001000,  // A (10)
  40.                      0b10000011,  // B (11)
  41.                      0b11000110,  // C (12)
  42.                      0b10100001,  // D (13)
  43.                      0b10000110,  // E (14)
  44.                      0b10001110}; // F (15)
  45.  
  46. // Time count - 1000milli = 1sec
  47. const unsigned long MLEN = 1000;
  48. unsigned long ct = 0;
  49.  
  50. // Debug digit counter
  51. int x = 0;
  52.  
  53. void setup() {
  54.   Serial.begin(9600);
  55.   // set pins to output so you can control the shift register
  56.   pinMode(latchPin, OUTPUT);
  57.   pinMode(clockPin, OUTPUT);
  58.   pinMode(dataPin, OUTPUT);
  59.  
  60.   // Sets display pins
  61.   pinMode(AD, OUTPUT);
  62.   pinMode(BD, OUTPUT);
  63.   pinMode(CD, OUTPUT);
  64.   pinMode(DD, OUTPUT);
  65.  
  66.   // Initialize time
  67.   ct = millis() + MLEN;
  68.  
  69.   // debug console
  70.   //Serial.begin(9600);
  71. }
  72.  
  73. void loop() {
  74.   if (ct <= millis()) {
  75.     ct = millis() + MLEN;
  76.     /* while (x < DEMAX) {
  77.       Serial.print(disp_digits[x], DEC);
  78.       Serial.print(" ");
  79.       x++;
  80.     }
  81.     Serial.println("");
  82.     x=0; */
  83.     addUp();
  84.   }
  85.  
  86.   // update one digit on the display per loop
  87.   if (disp_count == DEMAX) { disp_count = 0; }
  88.   changePattern(n[disp_digits[disp_count]]);
  89.   digitalWrite(disp_elements[disp_count], HIGH);
  90.   digitalWrite(disp_elements[disp_count], LOW);
  91.   disp_count += 1;  
  92. }
  93.  
  94. // Increment the display digits
  95. void addUp() {
  96.   int carry = 1;
  97.   for(int y=0;y<DEMAX;y++) {
  98.     if (carry == 0) { return; }
  99.     if (disp_digits[y] < 9) {
  100.       disp_digits[y]++;
  101.       carry=0;
  102.     } else {
  103.       disp_digits[y]=0;
  104.     }
  105.   }
  106. }
  107.  
  108. // Pushes a binary value out to the shift register
  109. void changePattern(int n) {
  110.     digitalWrite(latchPin, LOW);
  111.     shiftOut(dataPin, clockPin, MSBFIRST, n);
  112.     digitalWrite(latchPin, HIGH);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement