CrossRoads: Did you look at the code I posted in #32?
I did read it, however I assumed that each register "linked" but needed at least 3 to 4 wires each. As for the code, I'm not too hot at the moment reading code. I loosely understand what you've written in principle, however I have to actually write this up and apply it in context to fully understand it. I'll order the shift registers, mock up 15 small led displays with it, then write the code to learn how to do it.
Below for instance is an example of the code I've written for a MM:SS timer. You'll look at it and think it's quite long winded. But self taught, so far it's the best I can do.
/* COMMON ANODE TIMER (with start, pause and reset):
This Arduino code drives 4 separate 7 segment common ANODE LED displays
Via a pushbutton switch, it starts a timer display from 00:00
(mm:ss) to 59:59. The button also pauses the timer.
Holding the button in for greater than 2 seconds resets
There is a built-in debounce to limit bouncing of pulses via the pushbutton.
Includes an LDR to control the intensity of the digits
Create by Chris in Feb 2015
*/
//--------------------------------------------------------------------------------------------
int A = 1; // Set to 0 (LOW) for common cathode. Set to 1 for common anode digits
int B = 0; // Set to 1 (HIGH) for common cathode. Set to 0 for common anode digits
int ldrIn = 14; // Analog input pin A0. Attach to first leg of LDR and 10K to GND
int aValue = 0; // A variable used in controlling the light intensity value
int dValue = 0; // A variable used in controlling the light intensity value
int buttonChange = 15; // Analog in pin (A1) used to pause, restart and reset timer
// Other end of led to GND via a 10K resistor
int state = LOW; // The current state of the output pin
int reading; // The current reading of the input pin
int previous = HIGH; // The previous reading from the input pin
int check = 0; // Used to pause the clock when the button is preassed - whilst reset
int delayMp = 2; // Used to delay the muliplexing
int sec_ones = 0; // Used to indicate what number the digit is set to
int sec_tens = 0; // Used to increment the tens unit
int min_ones = 0; // Used to increment the min_ones units
int min_tens = 0; // Used to increment the min_tens units
long time1 = 0; // Used in delaying for debouncing of pushbutton
long time2 = 0; // Used in delaying the increment clock time
long secondMicro = 996000; // One second increment clock time
long debounce = 500; // Debounce time - longer than usual so it works for me
unsigned long currentMillis = 0; //
unsigned long previousMillis = 0; //
int stepMultiplex = 1; // Variable used to step through each segment
int reset = 2000; // Duration required for button press to reset time
int firstTime = 1; // Used in recording button press duration
unsigned long startTime = 0; // Used in recording button press duration
unsigned long pressTime = 0; // Used in recording button press duration
// the following array is used to define each led common
byte commons [4] = {6,9,10,11}; // pins to each digit common (these are PWM outputs)
// the following array is used to define each led segment for number 0 to 9
byte sevenSegmentPin[7] = {2,3,4,5,7,8,12};
byte sevenSegment[10][7] = {
{B,B,B,B,B,B,A}, // this is 0
{A,B,B,A,A,A,A}, // this is 1
{B,B,A,B,B,A,B}, // this is 2
{B,B,B,B,A,A,B}, // this is 3
{A,B,B,A,A,B,B}, // this is 4
{B,A,B,B,A,B,B}, // this is 5
{B,A,B,B,B,B,B}, // this is 6
{B,B,B,A,A,A,A}, // this is 7
{B,B,B,B,B,B,B}, // this is 8
{B,B,B,A,A,B,B}, // this is 9
};
void setup() //--------------------------------------------------
{
Serial.begin(9600);
for (byte i=0; i<7; i++)
pinMode(sevenSegmentPin[i],OUTPUT); // this sets pins 2,3,4,5,6,8 and 12 as outputs
for(byte i=0; i<4; i++)
pinMode(commons[i], OUTPUT); // this sets pins 6,9, 10 and 11 as outputs
for (byte i=0; i<7; i++) {
digitalWrite(sevenSegmentPin[i], sevenSegment[0][i]); // this sets pins 2,3,4,5,6 and 8 as 0
}
}
// This is code for displaying the incremental digit for the "sec_ones"
void sevenSegWrite_1(byte sec_ones) {
for (byte i=0; i<7; i++) {
digitalWrite(sevenSegmentPin[i], sevenSegment[sec_ones][i]);
}
}
// This is code for displaying the incremental digit for the "sec_tens"
void sevenSegWrite_2(byte sec_tens) {
for (byte i=0; i<7; i++) {
digitalWrite(sevenSegmentPin[i], sevenSegment[sec_tens][i]);
}
}
// This is code for displaying the incremental digit for the "min_ones"
void sevenSegWrite_3(byte min_ones) {
for (byte i=0; i<7; i++) {
digitalWrite(sevenSegmentPin[i], sevenSegment[min_ones][i]);
}
}
// This is code for displaying the incremental digit for the "min_tens"
void sevenSegWrite_4(byte min_tens) {
for (byte i=0; i<7; i++) {
digitalWrite(sevenSegmentPin[i], sevenSegment[min_tens][i]);
}
}
void loop() //--------------------------------------------------
{
aValue = analogRead(ldrIn); // Read the LDR analogue value on analog pin and
// store the value between 0 and 1023
// Serial.print("0l to 1023 value = ");
// Serial.println(aValue);
aValue = constrain (aValue, 20, 300); // Constrain the value between 900 and 1010
dValue = map (aValue, 20, 300, 0, 255); // The digital pin outputs values from 0 to 255
// so convert the value received in the above line
reading = digitalRead(buttonChange); // Check the state of the button
currentMillis = millis(); // Set variable for multiplexing timing
// Used to toggle starting and pausing the clock...
if (reading == HIGH && previous == LOW && millis() - time1 > debounce) {
if (state == HIGH) {
state = LOW;
time1 = millis ();
}
else {
state = HIGH;
time1 = millis();
}
}
previous = reading;
// Used to check if the button is pressed long enough to reset the time
if (reading == HIGH) {
if (firstTime == 1) {
startTime = millis();
firstTime = 0;
}
pressTime = millis () - startTime;
}
else if (firstTime == 0) {
firstTime = 1;
}
if (pressTime > 2000) {
state = LOW;
sec_ones=0;
sec_tens=0;
min_ones=0;
min_tens=0;
time1 = 0;
}
//-------------------------------------------------------------------
if(stepMultiplex == 1 && (currentMillis - previousMillis) >= delayMp) {
digitalWrite(commons [0],B); // Turn off the previous digit once time expires
previousMillis=currentMillis;
sevenSegWrite_1(sec_ones);
analogWrite(commons [3],dValue); // Turn on the sec_one digit
stepMultiplex=2;
}
if(stepMultiplex == 2 && (currentMillis - previousMillis) >= delayMp) {
digitalWrite(commons [3],B); // Turn off the previous digit once time expires
previousMillis=currentMillis;
sevenSegWrite_2(sec_tens);
analogWrite(commons [2],dValue); // Turn on the sec_tens digit
stepMultiplex=3;
}
if(stepMultiplex == 3 && (currentMillis - previousMillis) >= delayMp) {
digitalWrite(commons [2],B); // Turn off the previous digit once time expires
previousMillis=currentMillis;
sevenSegWrite_3(min_ones);
analogWrite(commons [1],dValue); // Turn on the min_one digit
stepMultiplex=4;
}
if(stepMultiplex == 4 && (currentMillis - previousMillis) >= delayMp) {
digitalWrite(commons [1],B); // Turn off the previous digit once time expires
previousMillis=currentMillis;
sevenSegWrite_4(min_tens);
analogWrite(commons [0],dValue); // Turn on the min_tens digit
stepMultiplex=1;
}
// The next few lines are used to advance the clock
if (micros() - time2 > secondMicro && state == HIGH) {
check=1; //this is used to pause the clock while the button is pressed
if (check == 1 & reading == LOW) {
sec_ones++;
check=0;
}
if(sec_ones >9) {
sec_ones=0;
sec_tens++;
}
if(sec_tens>5) {
sec_tens=0;
min_ones++;
}
if(min_ones>9) {
min_ones=0;
min_tens++;
}
if(min_tens>5) {
sec_ones=0;
sec_tens=0;
min_ones=0;
min_tens=0;
}
time2 = micros();
}
}