|
#define dataPin 2 |
|
#define clockPin 3 |
|
#define latchPin 4 |
|
#define layer1 5 |
|
#define layer2 7 |
|
|
|
int nextLayer = 1; |
|
char currentDisplay[4] = "0000"; |
|
word result = 0; |
|
|
|
const word digits[] = { |
|
// digit 1 |
|
0b0000000000000000, 0b0000000000000000, // 0 left blank |
|
0b0000000000000000, 0b1001000000000000, // 1 |
|
0b0110000000000000, 0b1110000000000000, // 2 |
|
0b0000000000000000, 0b0000000000000000, // 3 not needed |
|
0b0000000000000000, 0b0000000000000000, // 4 not needed |
|
0b0000000000000000, 0b0000000000000000, // 5 not needed |
|
0b0000000000000000, 0b0000000000000000, // 6 not needed |
|
0b0000000000000000, 0b0000000000000000, // 7 not needed |
|
0b0000000000000000, 0b0000000000000000, // 8 not needed |
|
0b0000000000000000, 0b0000000000000000, // 9 not needed |
|
// digit 2 |
|
0b0001011000000000, 0b0000111000000000, // 0 |
|
0b0000000000000000, 0b0000110000000000, // 1 |
|
0b0001110000000000, 0b0000101000000000, // 2 |
|
0b0000110000000000, 0b0000111000000000, // 3 |
|
0b0000101000000000, 0b0000110000000000, // 4 |
|
0b0000111000000000, 0b0000011000000000, // 5 |
|
0b0001111000000000, 0b0000011000000000, // 6 |
|
0b0000000000000000, 0b0000111000000000, // 7 |
|
0b0001111000000000, 0b0000111000000000, // 8 |
|
0b0000111000000000, 0b0000111000000000, // 9 |
|
// digit 3 |
|
0b0000000000000000, 0b0000000000000000, // 0 left blank |
|
0b0000000011000000, 0b0000000000000000, // 1 |
|
0b0000000110000000, 0b0000000011100000, // 2 |
|
0b0000000111000000, 0b0000000011000000, // 3 |
|
0b0000000011000000, 0b0000000110000000, // 4 |
|
0b0000000101000000, 0b0000000111000000, // 5 |
|
0b0000000000000000, 0b0000000000000000, // 6 not needed |
|
0b0000000000000000, 0b0000000000000000, // 7 not needed |
|
0b0000000000000000, 0b0000000000000000, // 8 not needed |
|
0b0000000000000000, 0b0000000000000000, // 9 not needed |
|
// digit 4 |
|
0b0000000000101100, 0b0000000000011100, // 0 |
|
0b0000000000000000, 0b0000000000011000, // 1 |
|
0b0000000000111000, 0b0000000000010100, // 2 |
|
0b0000000000011000, 0b0000000000011100, // 3 |
|
0b0000000000010100, 0b0000000000011000, // 4 |
|
0b0000000000011100, 0b0000000000001100, // 5 |
|
0b0000000000111100, 0b0000000000001100, // 6 |
|
0b0000000000000000, 0b0000000000011100, // 7 |
|
0b0000000000111100, 0b0000000000011100, // 8 |
|
0b0000000000011100, 0b0000000000011100 // 9 |
|
}; |
|
|
|
|
|
void getDigits() { |
|
result = 0; |
|
for (int digit = 0; digit < 4; digit++) { |
|
result = result | digits[digit * 20 + (currentDisplay[digit] - 48) * 2 + (nextLayer - 1)]; |
|
} |
|
} |
|
|
|
void displayDigits() { |
|
if (nextLayer == 2) { |
|
getDigits(); |
|
digitalWrite(layer1, HIGH); |
|
digitalWrite(latchPin, LOW); |
|
shiftOut(dataPin, clockPin, MSBFIRST, (result >> 8) & 0xFF); |
|
shiftOut(dataPin, clockPin, MSBFIRST, (result >> 0) & 0xFF); |
|
digitalWrite(latchPin, HIGH); |
|
digitalWrite(layer2, LOW); |
|
nextLayer = 1; |
|
} else { |
|
getDigits(); |
|
digitalWrite(layer2, HIGH); |
|
digitalWrite(latchPin, LOW); |
|
shiftOut(dataPin, clockPin, MSBFIRST, (result >> 8) & 0xFF); |
|
shiftOut(dataPin, clockPin, MSBFIRST, (result >> 0) & 0xFF); |
|
digitalWrite(latchPin, HIGH); |
|
digitalWrite(layer1, LOW); |
|
nextLayer = 2; |
|
} |
|
} |
|
|
|
void setup() { |
|
pinMode(dataPin, OUTPUT); |
|
pinMode(clockPin, OUTPUT); |
|
pinMode(latchPin, OUTPUT); |
|
pinMode(layer1, OUTPUT); |
|
pinMode(layer2, OUTPUT); |
|
|
|
delay(100); |
|
} |
|
|
|
void loop(){ |
|
displayDigits(); |
|
delay(10); |
|
} |