How to reduce digitalWrite lines?

I would like to minimize my code. For a project I have made code for a 7-segment display. But now i have multiple lines for 1 number, like:

DigitalWrite(a, HIGH);
DigitalWrite(b, LOW);
DigitalWrite(c, LOW);
DigitalWrite(d, HIGH);
DigitalWrite(e, HIGH);
DigitalWrite(f, HIGH);
DigitalWrite(g, HIGH);

Is there a way to minimize this like digitalWrite(a,d,e,f,g , LOW , b,c, HIGH). A function maybe?

Thanks in advance for help

The secret is to use arrays.
http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

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.

Multi-dimensional arrays or use a seven segment LED library.

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...

Regards,
Ray L.

 // 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]);
 }

joostbudding:
My motivation for this is only to simplify my code.

The simplest way, is the canonical way:

DigitalWrite(a, HIGH);
DigitalWrite(b, LOW);
DigitalWrite(c, LOW);
DigitalWrite(d, HIGH);
DigitalWrite(e, HIGH);
DigitalWrite(f, HIGH);
DigitalWrite(g, HIGH);

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. :slight_smile: Maybe you should go there and see how they are doing it.

Alternatively:

 // 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]);
   }
 }

Thanks to all for the help. I can do something with this. It is not only to simplify it, but also to reduce my code.

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.

For me there are 2 ways:

First - assuming that those pins are in order like: 1 2 3 etc. and all pins you want to go low/high:

void loop()
{
    for (some_pin=1; some_pin=7; some_pin++)
        {
            digitalWrite(some_pin, HIGH)
        }
}

Second - more flexible and better IMHO:

void loop()
{
    crazy stuff
    pinsDesiredBehavior()
}

void pinsDesiredBehavior()
{
    DigitalWrite(pin_a, HIGH);
    DigitalWrite(pin_b, LOW);
    DigitalWrite(pin_c, LOW);
    DigitalWrite(pin_d, HIGH);
    DigitalWrite(pin_e, HIGH);
    DigitalWrite(pin_f, HIGH);
    DigitalWrite(pin_g, HIGH);
}

Indents for visual pleasure - no need to do it.

@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.

Can you explain? Cause tbh I would think that this is a way to go…

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?

Tbh I don't get what you wrote - probably due to my lack of something - knowledge maybe.

szawus:
Can you explain? Cause tbh I would think that this is a way to go...

With your method you need a separate function for each digit. Seems like a lot of code vs. using simple arrays.

I get it now. Thanks

szawus:
I get it now. Thanks

No prob.