int latchPin = 5; // Latch pin of 74HC595 is connected to Digital pin 5
int clockPin = 6; // Clock pin of 74HC595 is connected to Digital pin 6
int dataPin = 4;
int digitSelect2Pin = 7;
int digitSelect1Pin = 12;
int digitSelect3Pin = 11;
int digitSelect4Pin = 10;
// Data pin of 74HC595 is connected to Digital pin 4
int t = 0;
int s = 0;
int x = 0;
int f = 0;
byte leds1 = 0;
byte leds2 = 0;
byte leds3 = 0;
byte leds4 = 0;
int analogpin = A0;
int speed = 0; // Higher = slower count
// Variable to hold the pattern of which LEDs are currently turned on or off
int num1[10][7] = { { 1, 1, 1, 1, 1, 1, 0 }, // 0
{ 0, 1, 1, 0, 0, 0, 0 }, // 1
{ 1, 1, 0, 1, 1, 0, 1 }, // 2
{ 1, 1, 1, 1, 0, 0, 1 }, // 3
{ 1, 1, 1, 0, 0, 1, 0 }, // 4
{ 1, 0, 1, 1, 0, 1, 1 }, // 5
{ 1, 0, 1, 1, 1, 1, 1 }, // 6
{ 1, 1, 0, 0, 0, 1 }, // 7
{ 1, 1, 1, 1, 1, 1, 1 }, // 8
{ 1, 1, 1, 0, 0, 1, 1 } }; // 9
int num[16][1] = {
{ 63 }, // 0 Add 128 to turn decimal point
{ 48 }, // 1
{ 109 }, // 2
{ 121 }, // 3
{ 114 }, // 4
{ 91 }, // 5
{ 95 }, // 6
{ 49 }, // 7
{ 127 }, // 8
{ 115 }, // 9
{ 119 }, //A
{ 94 }, //b
{ 15 }, //C
{ 124 }, //D
{ 79 }, //E
{ 71 }, //F
};
int test[5][5]{ 1, 2, 3, 4, 5, 67, 8, 9, 10 };
/*
* setup() - this function runs once when you turn your Arduino on
* We initialize the serial connection with the computer
*/
void setup() {
// Set all the pins of 74HC595 as OUTPUT
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(digitSelect1Pin, OUTPUT);
pinMode(digitSelect2Pin, OUTPUT);
pinMode(digitSelect3Pin, OUTPUT);
pinMode(digitSelect4Pin, OUTPUT);
Serial.begin(9600);
digitalWrite(digitSelect1Pin, 0);
digitalWrite(digitSelect2Pin, 1);
digitalWrite(digitSelect3Pin, 1);
digitalWrite(digitSelect4Pin, 1);
}
void loop() {
d1();
}
void d1() {
for (x = 0; x < 10; x++) {
leds4 = num[x][0];
if (f < 9)
leds4 = 0;
if (x == 0)
leds4 = 0;
d2();
}
}
void d2() {
for (t = 0; t < 10; t++) {
leds3 = num[t][0];
if (f < 9)
leds3 = 0;
if (t == 0 && x == 0) {
leds3 = 0;
}
d3();
}
}
void d3() {
for (s = 0; s < 10; s++) {
leds2 = num[s][0];
if (f < 9)
leds2 = 0;
if (t == 0 && x == 0 && s == 0) {
leds2 = 0;
}
d4();
}
}
void d4() {
for (f = 0; f < 10; f++) {
leds1 = num[f][0];
speed = analogRead(A0);
if (speed == 0)
speed = 1;
//Serial.println(speed);
for (int w = 0; w < speed; w++) { //Dislplays the same output "speed" times Higher= slower
digitalWrite(digitSelect1Pin, 0);
updateShiftRegister1(); // this togles the digit on and off very fast so it looks like
digitalWrite(digitSelect1Pin, 1); //it is on all the time.
digitalWrite(digitSelect2Pin, 0);
updateShiftRegister2();
digitalWrite(digitSelect2Pin, 1);
digitalWrite(digitSelect3Pin, 0);
updateShiftRegister3();
digitalWrite(digitSelect3Pin, 1);
digitalWrite(digitSelect4Pin, 0);
updateShiftRegister4();
digitalWrite(digitSelect4Pin, 1);
} //w for loop
}
}
/*
* updateShiftRegister() - This function sets the latchPin to low, then calls the Arduino function
'shiftOut' to shift out contents of variable 'ledsx' in the shift register before putting the
'latchPin' high again.
*/
void updateShiftRegister1() {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds1);
digitalWrite(latchPin, HIGH);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, 0);
digitalWrite(latchPin, HIGH);
}
void updateShiftRegister2() {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds2);
digitalWrite(latchPin, HIGH);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, 0);
digitalWrite(latchPin, HIGH);
}
void updateShiftRegister3() {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds3);
digitalWrite(latchPin, HIGH);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, 0);
digitalWrite(latchPin, HIGH);
}
void updateShiftRegister4() {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds4);
digitalWrite(latchPin, HIGH);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, 0);
digitalWrite(latchPin, HIGH);
}