Go Down

Topic: How to reduce digitalWrite lines? (Read 400 times) previous topic - next topic

joostbudding

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

Grumpy_Mike


aarg

#2
Mar 26, 2020, 10:12 pm Last Edit: Mar 26, 2020, 10:13 pm by 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.
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

ToddL1962

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

joostbudding

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.

RayLivingston

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.

ToddL1962

#6
Mar 26, 2020, 10:35 pm Last Edit: Mar 26, 2020, 10:36 pm by ToddL1962
Code: [Select]

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

aarg

#7
Mar 26, 2020, 10:37 pm Last Edit: Mar 26, 2020, 10:40 pm by aarg
My motivation for this is only to simplify my code.
The simplest way, is the canonical way:
Code: [Select]
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. :) Maybe you should go there and see how they are doing it.
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

ToddL1962

Alternatively:

Code: [Select]


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

joostbudding

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.

Deva_Rishi

Quote
Never, ever, ever use lowercase (or even uppercase) single characters for a variable name.
I think lowercase can be ok for counters
Code: [Select]
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.
To 'Correct' you have to be Correct. (and not be condescending..)

aarg

I think lowercase can be ok for counters
Code: [Select]
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.
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

szawus

#12
Mar 27, 2020, 01:17 am Last Edit: Mar 27, 2020, 02:08 am by szawus
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:

Code: [Select]

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


Second - more flexible and better IMHO:

Code: [Select]

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.

aarg

@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.
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

szawus

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

Go Up