Pages: [1]   Go Down
Author Topic: 7Seg Display through Shiftregister, Code optimization  (Read 1426 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 92
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 == smiley-cool{
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B01111111);
digitalWrite(LATCH_PIN, HIGH);
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8474
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

BTW, what character is this testing for

outputValue ==  smiley-cool


______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 525
Posts: 26524
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

 Interesting that 8 turned into a smiley - what happened  to 0 & 9 tho?
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pages: [1]   Go Up
Jump to: