Skip to content

Instantly share code, notes, and snippets.

@ElectricCoffee
Last active August 29, 2015 13:57
Show Gist options
  • Save ElectricCoffee/9400145 to your computer and use it in GitHub Desktop.
Save ElectricCoffee/9400145 to your computer and use it in GitHub Desktop.
#define CLOCK_PIN 2
#define LATCH_PIN 3
#define DATA_PIN 4
#define OPEN_LATCH digitalWrite(LATCH_PIN, 0)
#define CLOSE_LATCH digitalWrite(LATCH_PIN, 1)
byte digits[16];
void setup() {
pinMode(LATCH_PIN, OUTPUT);
digits[0x0] = B00111111; // read right-to-left, not left-to-right
digits[0x1] = B00000110; // it's connected as so: Dot G F E D C B A
digits[0x2] = B01011011; // so this line for example has segment G E D B and A lit, the rest are off
digits[0x3] = B01001111;
digits[0x4] = B01100110;
digits[0x5] = B01101101;
digits[0x6] = B01111101;
digits[0x7] = B00000111;
digits[0x8] = B01111111;
digits[0x9] = B01101111;
digits[0xA] = B01110111;
digits[0xB] = B01111100;
digits[0xC] = B00111001;
digits[0xD] = B01011110;
digits[0xE] = B01111001;
digits[0xF] = B01110001;
}
void loop() {
for(int j = 0; j < 0xFF; j++) {
int firstDigit = j / 0x10;
int secondDigit = j % 0x10;
OPEN_LATCH;
shiftOut(8, digits[secondDigit], DATA_PIN, CLOCK_PIN);
shiftOut(8, digits[firstDigit], DATA_PIN, CLOCK_PIN);
CLOSE_LATCH;
delay(1000);
}
}
// the "bits" parameter was added for easy expandability;
// but I haven't made anything yet that fully utilises it
// so it's sorta just there as a leftover that I'm too lazy to edit
void shiftOut(int bits, byte input, int dataPin, int clockPin) {
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
int pinState;
digitalWrite(clockPin, 0);
digitalWrite(dataPin, 0);
for(int i = bits -1; i >= 0; i--) {
digitalWrite(clockPin, 0);
if(input & (1 << i))
pinState = 1;
else
pinState = 0;
digitalWrite(dataPin, pinState);
digitalWrite(clockPin, 1);
digitalWrite(dataPin, 0);
}
digitalWrite(clockPin, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment