//Pin Definitions
//For the shift register
int data = 2;
int clock = 3;
int latch = 4;
//Used for single LED manipulation
int ledState = 0;
const int ON = HIGH;
const int OFF = LOW;
//The respective anode pins for each digit, first being left, fourth being right
int first = 9;
int second = 10;
int third = 11;
int fourth = 12;
//The dec equivalents for 0 - 9 in 7 segments
int number[]={126,12,182,158,204,218,250,14,254,222};
//The current value I want to show
int numToDisplay = 0;
int thou = 0;
int hund = 0;
int tens = 0;
int ones = 0;
/*
* setup() – this function runs once when you turn your Arduino on
* We set the three control pins to outputs
*/
void setup()
{
pinMode(data, OUTPUT);
pinMode(clock, OUTPUT);
pinMode(latch, OUTPUT);
pinMode(first, OUTPUT);
pinMode(second, OUTPUT);
pinMode(third, OUTPUT);
pinMode(fourth, OUTPUT);
}
void loop()
{
/Abuse integer division to get the relevant digits.
thou = numToDisplay / 1000;
hund = (numToDisplay - thou * 1000) / 100;
tens = (numToDisplay ) / 10; //<< I started just working on 00 - 99 to test, hence this.
ones = (numToDisplay % 10);
if (numToDisplay == 99) //Same here.
numToDisplay = 0;
//<<Snipped out code for 100-9999, as this code had the same issues that 10-99 does.
if(numToDisplay > 9) { //If two digits
for (int i = 0; i < 500; i++) { //Delay loop to force display to 'delay' with two digits. Also tried using delay(x) but had similar (or same) issues.
digitalWrite(third, HIGH);
updateLEDs(number[tens]);
digitalWrite(third, LOW);
digitalWrite(fourth,HIGH);
updateLEDs(number[ones]);
digitalWrite(fourth, LOW);
}
}
if(numToDisplay < 10) {
for (int i = 0; i < 500; i++) {
digitalWrite(fourth,HIGH);
updateLEDs(number[ones]);
digitalWrite(fourth, LOW);
}
}
numToDisplay++;
}
//following code stolen from shift register example with the Arduino I bought.
/*
* updateLEDs() - sends the LED states set in ledStates to the 74HC595
* sequence
*/
void updateLEDs(int value){
digitalWrite(latch, LOW); //Pulls the chips latch low
shiftOut(data, clock, MSBFIRST, value); //Shifts out the 8 bits to the shift register
digitalWrite(latch, HIGH); //Pulls the latch high displaying the data
}
/*
* updateLEDsLong() - sends the LED states set in ledStates to the 74HC595
* sequence. Same as updateLEDs except the shifting out is done in software
* so you can see what is happening.
*/
void updateLEDsLong(int value){
digitalWrite(latch, LOW); //Pulls the chips latch low
for(int i = 0; i < 8; i++){ //Will repeat 8 times (once for each bit)
int bit = value & B10000000; //We use a "bitmask" to select only the eighth
//bit in our number (the one we are addressing this time through
value = value << 1; //we move our number up one bit value so next time bit 7 will be
//bit 8 and we will do our math on it
if(bit == 128){digitalWrite(data, HIGH);} //if bit 8 is set then set our data pin high
else{digitalWrite(data, LOW);} //if bit 8 is unset then set the data pin low
digitalWrite(clock, HIGH); //the next three lines pulse the clock pin
delay(1);
digitalWrite(clock, LOW);
}
digitalWrite(latch, HIGH); //pulls the latch high shifting our data into being displayed
}
//These are used in the bitwise math that we use to change individual LEDs
//For more details http://en.wikipedia.org/wiki/Bitwise_operation
int bits[] = {B00000001, B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000};
int masks[] = {B11111110, B11111101, B11111011, B11110111, B11101111, B11011111, B10111111, B01111111};
/*
* changeLED(int led, int state) - changes an individual LED
* LEDs are 0 to 7 and state is either 0 - OFF or 1 - ON
*/
void changeLED(int led, int state){
ledState = ledState & masks[led]; //clears ledState of the bit we are addressing
if(state == ON){ledState = ledState | bits[led];} //if the bit is on we will add it to ledState
updateLEDs(ledState); //send the new LED state to the shift register
}