quick question about arrays :)

i have created an array with different pins connected to my 7 segment display.

array looks like this: int SevenSeg1 = {aPin1,bPin1,cPin1,dPin1,ePin1,fPin1,gPin1};

for(int i = 0; i<7; i++){

digitalWrite(SevenSeg, HIGH);

}

How can i select only some of the pins from my array to be HIGH in the for loop? For exaplme aPin,cPin and gPin and the rest LOW.

How about a second array ?

(But make it byte sized to save precious RAM. Same goes for your current one. Better still, leave it in flash, where it really belongs

Some simple 7 segment code:

https://gist.github.com/delta-G/43685615ed0e5a6e527e

Clunky, but probably works.

#define PINA    4
#define PINB    5
#define PINC    6
#define PIND    7
#define PINE    8
#define PINF    9
#define PING   10


void setup() {
  // put your setup code here, to run once:
  unsigned char sevenSeg[] = {PINA, PINB, PINC, PIND, PINE, PINF, PING};

  memset(sevenSeg, LOW, sizeof(sevenSeg));
  digitalWrite(sevenSeg[PINA], HIGH);
  digitalWrite(sevenSeg[PINC], HIGH);
  digitalWrite(sevenSeg[PING], HIGH);
}

void loop() {

}
memset(sevenSeg, LOW, sizeof(sevenSeg));

So once you've replaced all the pin numbers with 0 how is it useful anymore?

digitalWrite(sevenSeg[PING], HIGH);

PING is #defined as 10. The array sevenSeg only has 7 elements. So you're way out of bounds there.

My bad. Should have just used a loop to read and set them.

#define PINA    4
#define PINB    5
#define PINC    6
#define PIND    7
#define PINE    8
#define PINF    9
#define PING   10

#define ELEMENTCOUNT(x)  (sizeof(x) / sizeof(x[0])

void setup() {
  // put your setup code here, to run once:
  unsigned char sevenSeg[] = {PINA, PINB, PINC, PIND, PINE, PINF, PING};
  int i;

  for (i = 0; i < ELEMENTCOUNT(sevenSeg); i++) {
    digitalWrite(sevenSeg[i], LOW);
  }
  digitalWrite(sevenSeg[PINA], HIGH);
  digitalWrite(sevenSeg[PINC], HIGH);
  digitalWrite(sevenSeg[PING], HIGH);
}

void loop() {

}

I still think two arrays are simpler and more flexible.

or you can use logic... this produces an inverted output so you need to invert the digitalWrite parameter unless the segments are active low...

  bool segA = (c==1 or c==4);
  bool segB = (c==5 or c==6);
  bool segC = (c==2);
  bool segD = (segA or c==7);
  bool segE = (c==4 or (c&1));
  bool segF = (c==1 or c==2 or c==3 or c==7);
  bool segG = (c==0 or c==1 or c==7);

I’ll offer two ways to associate a HIGH or LOW value to a pin. The first is to create a structure with pin numbers and values. You can initialize an array of the structure with the pin numbers you are using and set their values by indexing into the array.

You could also have an array of bit numbers and store their HIGH/LOW value in a bit mast. This only costs you one byte to store all the values since there are only 7 pins in use.

typedef struct pinVal {
  byte pinNum;
  byte pinVal;
} 
sevenseg;

// pin numbers and values
sevenseg sevenSeg1[] = {
  3,LOW,4,LOW,5,LOW,6,LOW,7,LOW,8,LOW,9,LOW};

// pin numbers
byte sevenSeg2[] = {
  3,4,5,6,7,8,9};

// bit mask for values
byte sevenSeg2Mask = 0;

void setup()
{
  Serial.begin(9600);

  // set some pins to HIGH
  sevenSeg1[0].pinVal = HIGH;
  sevenSeg1[2].pinVal = HIGH;
  sevenSeg1[6].pinVal = HIGH;

  // print pin number and value
  for(int i = 0; i<7; i++){

    Serial.print(sevenSeg1[i].pinNum);
    Serial.print(":  ");
    Serial.println(sevenSeg1[i].pinVal);

  }

  // set bits in mask to turn on pins
  sevenSeg2Mask |= 0x1;
  sevenSeg2Mask |= 0x4;
  sevenSeg2Mask |= 0x40;

  // print pin number and value based on bit in mask
  for(int i = 0; i<7; i++){

    Serial.print(sevenSeg2[i]);
    Serial.print(":  ");
    Serial.println((sevenSeg2Mask >> i) & 1 );

  }
}


void loop()
{

}

So the first way costs 2 bytes per pin per digit for a total of 14 bytes times 10 digits or 140 bytes.

The second way costs 7 bytes for the pin numbers and 10 bytes for the bit masks for a total of 17 bytes.

I think I know which one I'd be using. It's the same one I linked to earlier.