# Modifying the Properties of Values within an Array

I'm attempting to program a seven-segment, 4-digit number display using arrays, and I would love to be able to modify the properties (eg: HIGH or LOW) of each value within the array that I have assigned the pin values to without having to reference each value separately. For example, I have this:

``````/*
a = Top Horizontal
b = Top-Right
c = Bottom-Right
d = Bottom Horizontal
e = Bottom-Left
f = Top-Left
g = Middle Horizontal
dp = Decimal Point
*/
//Array denotes DP-a-b-c-d-e-f-g
int digitalDisplay[] = {
17, 11, 10, 16, 15, 14, 12, 13 };
int cathode[] = {
0, 1, 2, 3 };
``````

And I really wish I could define something that would allow me to just define 1 as on and 0 as off, then assign those properties to each value within the arrays.

Barring something that simple, is there a better way to program these things? It's a common cathode, 7-segment (technically 8 since there is a decimal place after each digit), 4-digit led display, identical to this 10 PCS LD 5461AS 4 Digit 0.56" RED 7 SEGMENT LED DISPLAY DIP 12 COMMON CATHODE|display sheet|display shelfdisplay lvds - AliExpress. If anyone has any tips at all for programming these, I would certainly appreciate it. I feel like I'm just banging my head against the wall with this thing.

Currently, I'm wondering if it might be easiest to use pointers to reference each value, but that doesn't really make it any easier or clearer to write.

I would seem to make sense to me to reorganize your segment orders to something like:

``````/*
a = Top Horizontal
b = Top-Right
c = Bottom-Right
d = Bottom Horizontal
e = Bottom-Left
f = Top-Left
g = Middle Horizontal
dp = Decimal Point
*/
#define SEGMENTOFFSET 10   // The lowest pin reference for a display segment

int digitalDisplay[] = {10, 11, 12, 13, 14, 15, 16, 17};
int cathode[] = { 0, 1, 2, 3 };
``````

which would allow you to do something like:

``````void setup()
{
int i;

for (j = 0; j <  sizeof(cathode) / sizeof(cathode[0]); j++) {
// whatever you want to do with each segment...
for (i = 0, j = 0; i < sizeof(digitalDisplay) / sizeof(digitalDisplay[0]); i++) {
pintMode(i + SEGMENTOFFSET, OUTPUT)
digitalWrite(i + SEGMENTOFFSET, LOW);
}
}
}
``````

This would require you to reorder your arrays, but I think you get the idea.

Have you seen this:-
http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

Nice, Grumpy!

You're the man, Mike! But I do have one or two more questions. I think I understand your tutorial (I'm still learning so some of it is still concept that I am shaky with), and I was wondering if it would then be possible to simultaneously define whether each digit on a 4-digit display should be on and/or off, or would I be better using a separate array for the common cathodes, as I am currently doing, to define their state? Ideally, this will eventually be drawing value from other code already written to output equivalent values onto the 4-digit display. In this endeavor, it will be necessary to display different values to each of the 4 segments most of the time. What would be the best way to go about displaying different numbers on each digit?

EDIT: Also, thank you so much for saving me so much code. I was about to define a separate function for each possible number and just call those functions when needed. As you can imagine, that would have taken dozens of lines of code just to define.

What would be the best way to go about displaying different numbers on each digit?

It depends on how you wire up the display. There are two ways to do this:-

1. Directly, that is each segment on each display has its own output pin. This is best done with a shift register sort of wiring.
2. Multiplexed - all the a segments on all the displays are connected together, the same goes for the b, c ... and so on. Then the common connection on each display is individually switch on, and when it does the segments are set to the value for that number. This means there is only one display on at a time but if you change what is being displayed quickly it looks like they are all on at once.

From a coding standpoint, which do you think would work better? From what I've read, it seems like the shift register is the way to go, but I have no idea what I'm doing in regards to shift registers, whereas with multiplexing, which is how I currently have it wired, I'm not sure how to alternate the number as well as the digit fast enough (from a coding standpoint, how to keep looping through the digits while keeping them on their respective number display), while changing those values from time to time.

I feel like this whole thing would be much easier if I just switched to an LCD panel and output the numbers there instead, which is a viable solution in my project. Would it make more sense, if I'm drawing values from an existing function, to simply display those values on an LCD display, rather than trying to force this 4-digit display to work?

From a coding standpoint, which do you think would work better?

I would go with the shift registers as it is a set and forget interface.
With multiplexing you have to keep feeding it with software which reduces what else you can do with the processor's CPU cycles.

(from a coding standpoint, how to keep looping through the digits while keeping them on their respective number display)

The normal way is to have a timer trigger and interrupt and have the interrupt service routine (ISR )put the next pattern up. You change the number by changing the pattern in the four memory bytes the ISR uses for updating the display.