Skip to content

Instantly share code, notes, and snippets.

@westonal
Last active November 27, 2015 01:06
Show Gist options
  • Save westonal/b0f5d25dfeb462146760 to your computer and use it in GitHub Desktop.
Save westonal/b0f5d25dfeb462146760 to your computer and use it in GitHub Desktop.
Arduino Single Digit 7 segment display demo (using shift register)
//Pin connected to ST_CP of 74HC595
const int latchPin = 8;
//Pin connected to SH_CP of 74HC595
const int clockPin = 12;
////Pin connected to DS of 74HC595
const int dataPin = 11;
void setup() {
//set pins to output so you can control the shift register
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
//Seven segment:
//
// a
// f b
// g
// e c
// d DP
//
//
const int A = 1;
const int B = 2;
const int C = 4;
const int D = 8;
const int E = 16;
const int F = 32;
const int G = 64;
const int DP = 128;
const int digits[] = {
A|B|C|D|E|F, //0
B|C,
A|B|D|E|G,
A|B|C|D|G,
B|C|F|G,
A|C|D|F|G,
A|C|D|E|F|G,
A|B|C,
A|B|C|D|E|F|G,
A|B|C|F|G //9
};
const int letters[] = {
A|B|C|E|F|G, //A
F|E|D|C|G, //b
G|E|D, //c
E|D|C|G|B, //d
A|F|G|E|D, //E,
A|F|G|E, //F,
A|B|C|D|F|G, //g
C|E|F|G, //h
F|E, //I
B|C|D|E, //J
F|E|G, //K
F|E|D, //L
A|C|E, //M
E|G|C, //n
C|D|E|G, //o
A|B|E|F|G, //p
A|B|C|F|G, //q
E|G, //r
A|C|D|F|G, //s
D|E|F|G, //t
B|C|D|E|F, //U
C|D|E, //v
B|D|F, //w
B|C|G, //x
B|C|D|F|G, //y
B|G|E, //z
};
int andrea[] = {
0, //Space
letters['A' - 'A'],
letters['N' - 'A'],
letters['D' - 'A'],
letters['R' - 'A'],
letters['E' - 'A'],
letters['A' - 'A']|DP //A.
};
int alan[] = {
0, //Space
letters['A' - 'A'],
letters['L' - 'A'],
letters['A' - 'A'],
letters['N' - 'A'] | DP //n.
};
void setDigit(int d){
// take the latchPin low so
// the LEDs don't change while you're sending in bits:
digitalWrite(latchPin, LOW);
// shift out the bits:
shiftOut(dataPin, clockPin, MSBFIRST, d);
//take the latch pin high so the LEDs will light up:
digitalWrite(latchPin, HIGH);
}
void scrollDigits(int d1, int d2, int dtime1, int dtime2){
setDigit(d1);
delay(dtime1);
setDigit(
((d1 & B)<<4) | ((d1 & C)<<2) | //display RHS of first digit in LHS position and LHS of second digit in RHS position
((d2 & F)>>4) | ((d2 & E)>>2)
);
delay(dtime2);
}
void loopD() {
int n = -1;
int d = 0;
while(true) {
n=(n+1)%10;
d = digits[9-n];
setDigit(d | DP);
// pause before next value:
delay(100);
setDigit(d);
delay(900);
}
}
void progressLoop(unsigned long forMs) {
int n = A;
unsigned long until = millis() + forMs;
while(millis() < until) {
n<<=1;
if(n>F) n=A;
setDigit(n | DP);
delay(25);
setDigit(n);
delay(75);
}
}
void upDownLoop(unsigned long forMs) {
int pattern[] = {A,G,D,G};
int n=-1;
unsigned long until = millis() + forMs;
while(millis() < until) {
n=(n+1)%4;
setDigit(pattern[n]);
delay(100);
}
}
void displayPattern(int times, int len, int* pattern) {
for(int n = 0; n < len*times; n++) {
//setDigit(pattern[n%len]);
//delay(200);
scrollDigits(pattern[n%len], pattern[(n+1)%len], 200, 100);
}
}
void andreaDisplay(int times) {
displayPattern(times, 7, andrea);
}
void alanDisplay(int times) {
displayPattern(times, 5, alan);
}
void displayAlphabet() {
for(int n = 0; n < 26; n++) {
setDigit(letters[n]);
delay(1000);
}
}
void loop() {
int n = -1;
while(true) {
//displayAlphabet();
progressLoop(1200);
alanDisplay(1);
andreaDisplay(1);
upDownLoop(1200);
alanDisplay(1);
andreaDisplay(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment