andrewb icon

digit_counter.ino

andrewb | PRO | 07/20/14 03:54:44 AM UTC | 0 ⭐ | 995 👁️ | Never ⏰ | []
C++ |

2.88 KB

|

None

|

0 👍

/

0 👎

/*****
 * Ingrediients:
 * Yourduino Nano
 * 74HC595 shift register (https://www.sparkfun.com/products/733)
 * 7-segment, 4-digit dispay (https://www.sparkfun.com/products/retired/12669)
 * Bunch of wires
 * Breadboard
 ***
 * On YouTube: https://www.youtube.com/watch?v=84Tbc-9MZ5E
 *****/
 
// Setup the 74HC595 SIPO pins
const int latchPin = 6;   // ST_CP (RCLK)
const int clockPin = 7;   // SH_CP (SRCLK)
const int dataPin = 5;    // DS (data serial)
 
// display pins
const int AD = 9;
const int BD = 10;
const int CD = 11;
const int DD = 12;
const int DEMAX = 4;
const int disp_elements[DEMAX] = {AD, BD, CD, DD};
int disp_digits[DEMAX] = {0,0,0,0};
int disp_count = 0;
 
// Binary vals for decimal numbers
const int NMAX = 16;
const int n[NMAX] = {0b11000000,  // 0
                     0b11111001,  // 1
                     0b10100100,  // 2
                     0b10110000,  // 3
                     0b10011001,  // 4
                     0b10010010,  // 5
                     0b10000010,  // 6
                     0b11111000,  // 7
                     0b10000000,  // 8
                     0b10010000,  // 9
                     0b00001000,  // A (10)
                     0b10000011,  // B (11)
                     0b11000110,  // C (12)
                     0b10100001,  // D (13)
                     0b10000110,  // E (14)
                     0b10001110}; // F (15)
 
// Time count - 1000milli = 1sec
const unsigned long MLEN = 1000;
unsigned long ct = 0;
 
// Debug digit counter
int x = 0;
 
void setup() {
  Serial.begin(9600);
  // set pins to output so you can control the shift register
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  
  // Sets display pins
  pinMode(AD, OUTPUT);
  pinMode(BD, OUTPUT);
  pinMode(CD, OUTPUT);
  pinMode(DD, OUTPUT);
  
  // Initialize time
  ct = millis() + MLEN;
  
  // debug console
  //Serial.begin(9600);
}
 
void loop() {
  if (ct <= millis()) {
    ct = millis() + MLEN;
    /* while (x < DEMAX) {
      Serial.print(disp_digits[x], DEC);
      Serial.print(" ");
      x++;
    }
    Serial.println(""); 
    x=0; */
    addUp();
  }
  
  // update one digit on the display per loop
  if (disp_count == DEMAX) { disp_count = 0; }
  changePattern(n[disp_digits[disp_count]]);
  digitalWrite(disp_elements[disp_count], HIGH);
  digitalWrite(disp_elements[disp_count], LOW);
  disp_count += 1;  
}
 
// Increment the display digits 
void addUp() {
  int carry = 1;
  for(int y=0;y<DEMAX;y++) {
    if (carry == 0) { return; }
    if (disp_digits[y] < 9) {
      disp_digits[y]++;
      carry=0;
    } else {
      disp_digits[y]=0;
    }
  }
}
 
// Pushes a binary value out to the shift register 
void changePattern(int n) {
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, n);
    digitalWrite(latchPin, HIGH);

Comments