First, what is your motivation for this? Speed? Elegance? You can simultaneously write pins with a port write. But the processor pins are not mapped 1:1 with the pin numbers. So you can only change together, the pins that are on the same processor port.
Never, ever, ever use lowercase (or even uppercase) single characters for a variable name.
aarg:
First, what is your motivation for this? Speed? Elegance? You can simultaneously write pins with a port write. But the processor pins are not mapped 1:1 with the pin numbers. So you can only change together, the pins that are on the same processor port.
Never, ever, ever use lowercase (or even uppercase) single characters for a variable name.
In this case it makes no sense, because it's a simple project. Each segment on the display is named by a letter (a - g). Normally i use other names for variabeles.
My motivation for this is only to simplify my code.
You could do it with a pre-processor macro. When you're done, be sure to enter it in the obfuscated c code competition, because that would be where it belongs...
// TBD: Fill in proper state sequence for each number 0-9
byte numbers7sd[10][7] =
{
{HIGH,LOW,LOW,HIGH,HIGH,HIGH,HIGH}, // Number 0
.
.
.
{HIGH,LOW,LOW,LOW,HIGH,HIGH,HIGH} // Number 9
};
// Function to display number
displayNumber(byte num)
{
digitalWrite(a, numbers7sd[num][0]);
digitalWrite(b, numbers7sd[num][1]);
digitalWrite(c, numbers7sd[num][2]);
digitalWrite(d, numbers7sd[num][3]);
digitalWrite(e, numbers7sd[num][4]);
digitalWrite(f, numbers7sd[num][5]);
digitalWrite(g, numbers7sd[num][6]);
}
when seen in isolation. However, that is probably not the simplest way if there are a lot more sequences like this (which I happen to know that there are, because I've seen this code that this came from hundreds of times now).
If you really want to simplify, you have to begin at the top and design a more modular, procedural approach. When you are finished with that, you will have re-written the 7 segment library. Maybe you should go there and see how they are doing it.
// TBD: Fill in proper state sequence for each number 0-9
byte pins7sd[7] = {a, b, c, d, e, f, g};
byte numbers7sd[10][7] =
{
{HIGH,LOW,LOW,HIGH,HIGH,HIGH,HIGH}, // Number 0
.
.
.
{HIGH,LOW,LOW,LOW,HIGH,HIGH,HIGH} // Number 9
};
// Function to display number
displayNumber(byte num)
{
for (int idx = 0; idx < 7; idx++)
{
digitalWrite(pins7sd[idx], numbers7sd[num][idx]);
}
}
Never, ever, ever use lowercase (or even uppercase) single characters for a variable name.
I think lowercase can be ok for counters for (uint8_t i=0; i<8; i++) digitalWrite(i, HIGH);
btw as long as you are using only 1 7-segment all is fine, but the moment you want more of them you will run out of pins fairly soon. Consider using a bitshifter.
Deva_Rishi:
I think lowercase can be ok for counters for (uint8_t i=0; i<8; i++) digitalWrite(i, HIGH);
btw as long as you are using only 1 7-segment all is fine, but the moment you want more of them you will run out of pins fairly soon. Consider using a bitshifter.
The salient feature in that is not that it is a counter - it's that its a local variable, not global, and that its scope does not exceed a few lines.
That is much safer and easier to maintain than a global variable with a letter name.
@szawus, your 'pinsDesiredBehavior()' is 'writeDigit_1()' or something, in the real code. As I said before, you can't look at simplicity in only local terms. It may be simple locally, but introduce complexity in how 'pinDesiredBehaviour()' must be used. When it is time to upgrade from one digit to multiple 7 segment digits, such simplifications become more and more problematic.
szawus:
Can you explain? Cause tbh I would think that this is a way to go...
Suppose I want to create a function to display a number, like
void displayDigit(byte value) {
What you've made is a series of 10 functions, like
writeDigit_0(), writeDigit_1(), writeDigit_2()
How would you incorporate those into your display function?