Go Down

Topic: 7Seg Display through Shiftregister, Code optimization (Read 1 time) previous topic - next topic

inside-man

Hello can somebody have a look through the following code. It should be possible to control three separated 7Segment Displays with three shiftregisters. The code is working but very long and uncomfortable. Maybe someone has an example for a better solution. Suggestions are very appreciated. And the following code three times for three digits...

Thanx in advance.
regards inside-man


if (outputValue == 1){
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B00000110);
digitalWrite(LATCH_PIN, HIGH);
}
if (outputValue == 2){
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B01010011); //X111
digitalWrite(LATCH_PIN, HIGH);
}
if (outputValue == 3){
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B01000111);
digitalWrite(LATCH_PIN, HIGH);
}
if (outputValue == 4){
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B01100110);
digitalWrite(LATCH_PIN, HIGH);
}
if (outputValue == 5){
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B01101101);
digitalWrite(LATCH_PIN, HIGH);
}
if (outputValue == 6){
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B01111101);
digitalWrite(LATCH_PIN, HIGH);
}
if (outputValue == 7){
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B00000111);
digitalWrite(LATCH_PIN, HIGH);
}
if (outputValue == 8){
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B01111111);
digitalWrite(LATCH_PIN, HIGH);
}

PaulS

Create an array of output values.

int out[] = {B00000110, B01010011...};

then replace all of your code with:
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, out[outputValue-1]);
digitalWrite(LATCH_PIN, HIGH);

Graynomad

BTW, what character is this testing for

outputValue ==  8)


______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

CrossRoads

Interesting that 8 turned into a smiley - what happened  to 0 & 9 tho?
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up