I'm using a 4-digit 7-segment display for showing numbers. I've a few functions that display some letter/digit on the 7-segment like:
zero() // displays 0 on the 7-segment.
one() // displays 1 on the 7-segment.
two() // displays 2 on the 7-segment.
...
Now I've a number (say 1435) to be shown on the 7-segment display. My current algorithm is as follow:
Extract individual digits from the number 1435 (that's separate the digits as 1, 4, 3, 5). sds
To display these individual digits, I'm using 'ten' if-else conditions as follow:
So this implementation (for the number 1435) to be printed runs several if-else checks.
Problem number 2: This implementation is inefficient as when I need to increment/decrement that number (1435 to 1436 then to 1437 so on..), the number of if-else checks are also changed so the variation in numbers is not smooth. This means that as '0' is the first in if-else checks so it displays quickly. On the other hand '9' is the last in the if-else checks, so it has to undergo ten checks before it gets displayed. This makes implementation much slower as the digit to be displayed grows from 0 towards 9. How can Implement to solve the two problems?
Thanks in advance.
You can use pointers to functions in a table :
typedef void (*func)(); // type for functions
func functions[] = { zero, one, two, three, ... }
functions[3](); // example, will call three()
You have to extract the digit you want and use it as an index in the table....