problem with ARRAY's

I wrote the code below to control a 7-segment LED

my main questions is about using the LED variable

//Lets try and light up a 7 segment LED

int g = 2;                // LED connected to digital pin 1
int f = 1;                // LED connected to digital pin 2
int e = 3;                // LED connected to digital pin 3
int d = 4;                // LED connected to digital pin 4
int c = 5;                // LED connected to digital pin 5
int b = 6;                // LED connected to digital pin 6
int a = 7;                // LED connected to digital pin 7
int dot = 8;              //LED connected to the digital pin 8
int LED[]={0,1,1,0,0,0,0,0};              //This is the array that tells each pin in the LED to turn on
int val = 9;

void setup()                    // run once, when the sketch starts
{
  pinMode(g, OUTPUT);      // sets the digital pin as output
  pinMode(f, OUTPUT);      // sets the digital pin as output
  pinMode(e, OUTPUT);      // sets the digital pin as output
  pinMode(d, OUTPUT);      // sets the digital pin as output
  pinMode(c, OUTPUT);      // sets the digital pin as output
  pinMode(b, OUTPUT);      // sets the digital pin as output
  pinMode(a, OUTPUT);      // sets the digital pin as output
  pinMode(dot,OUTPUT);
  pinMode(13,OUTPUT);
}

void loop()                      // run over and over again
{
//  int val = 9;

  if (val==9) {
    
 int LED[] = {1, 1, 1, 0, 0, 1, 1, 0};

  }
  if (val==8) {
    int LED[] = {1, 1, 1, 1, 1, 1, 1, 0 };

  }
  if (val==7) {
    int LED[] = {1, 1, 1, 0, 0,0, 0, 0};

  }
  if (val==6) {
    int LED[] = {0, 0, 1, 1, 1, 1, 1, 0 };

  }
  if (val==5) {
    int LED[] = {1, 0, 1, 1, 0, 1, 1, 0};

  }
  if (val==4) {
    int LED[] = {0, 1, 1, 0, 1, 1, 1, 0};

  }
  if (val==3) {
    int LED[] = {1, 1, 1, 1, 0, 0, 1, 0};

  }
  if (val==2) {
    int LED[] = {1, 0, 1, 1, 0, 1, 1, 0};

  }
  if (val==1) {
    int LED[] = {0, 1, 1, 0, 0, 0, 0, 0 };
  }
  if (val==0) {
    int LED[] = {1, 1, 1, 1, 1, 1, 0, 0};
  }
  digitalWrite(g, LED[6]);   // sets the LED on
  digitalWrite(f, LED[5]);   // sets the LED on
  digitalWrite(e, LED[4]);   // sets the LED on
  digitalWrite(d, LED[3]);   // sets the LED on
  digitalWrite(c, LED[2]);   // sets the LED on
  digitalWrite(b, LED[1]);   // sets the LED on
  digitalWrite(a, LED[0]);   // sets the LED on
}

at this stage of my code development, I want to be able to change "val" and have it look in the correct "if" statement and make "LED" a new array. However now "LED" never changes from what I initialize it as. In the above case, my 7-segment LED stays as a "1", even though "val=9" and the 7-seg should show 9.

I've checked my hardware, I need some simple help.

That is an odd way of going about things. What you need is a two dimensional array.
LED[10][7]
Initialise that and put the number in the first array index and the led segment in the second.

Grumpy_Mike is right. You are probably best served by a 2D array. The reason your current example doesn't work is that when you write

if (val==0) {
    int LED[] = {1, 1, 1, 1, 1, 1, 0, 0};
  }

you are declaring a NEW array called LED that masks the global LED array for the duration of its lifespan, which is pretty short. (Any variables declared in between {} only live while that {} block is active. In your case, the new array called LED lives just long enough to take up space and be initialized and then it vanishes!) As soon as the right } is hit the array is destroyed. When you execute the series of digitalWrites, they always refer to the global LED array.

Mikal

Is there a way to over-write the array "LED" at different times in my code?

Yes indeed, there is. But see how much more elegant Grumpy_Mike's suggestion is:

//Lets try and light up a 7 segment LED

int g = 2; // LED connected to digital pin 1
int f = 1; // LED connected to digital pin 2
int e = 3; // LED connected to digital pin 3
int d = 4; // LED connected to digital pin 4
int c = 5; // LED connected to digital pin 5
int b = 6; // LED connected to digital pin 6
int a = 7; // LED connected to digital pin 7
int dot = 8; //LED connected to the digital pin 8
int LED[10][8] = {
{1, 1, 1, 1, 1, 1, 0, 0}, // 0
{0, 1, 1, 0, 0, 0, 0, 0}, // 1
{1, 0, 1, 1, 0, 1, 1, 0}, // 2
{1, 1, 1, 1, 0, 0, 1, 0}, // 3
{0, 1, 1, 0, 1, 1, 1, 0}, // 4
{1, 0, 1, 1, 0, 1, 1, 0}, // 5
{0, 0, 1, 1, 1, 1, 1, 0}, // 6
{1, 1, 1, 0, 0, 0, 0, 0}, // 7
{1, 1, 1, 1, 1, 1, 1, 0}, // 8
{1, 1, 1, 0, 0, 1, 1, 0} // 9
};

void setup() // run once, when the sketch starts
{
pinMode(g, OUTPUT); // sets the digital pin as output
pinMode(f, OUTPUT); // sets the digital pin as output
pinMode(e, OUTPUT); // sets the digital pin as output
pinMode(d, OUTPUT); // sets the digital pin as output
pinMode(c, OUTPUT); // sets the digital pin as output
pinMode(b, OUTPUT); // sets the digital pin as output
pinMode(a, OUTPUT); // sets the digital pin as output
pinMode(dot,OUTPUT);
pinMode(13,OUTPUT);
}

void loop() // run over and over again
{
int val = 9;
digitalWrite(g, LED[val][6]); // sets the LED on
digitalWrite(f, LED[val][5]); // sets the LED on
digitalWrite(e, LED[val][4]); // sets the LED on
digitalWrite(d, LED[val][3]); // sets the LED on
digitalWrite(c, LED[val][2]); // sets the LED on
digitalWrite(b, LED[val][1]); // sets the LED on
digitalWrite(a, LED[val][0]); // sets the LED on
}

Mikal

That helped a lot. I appreciate your time.

Elegant, maybe, but with barbarian memory usage. It'll work for this simple project. But later on, I'd recommend using PROGMEM storage for these kinds of lookup tables.

I'd recommend using individual bits for each segment, instead of a whole byte. Then you're down to only 10 bytes, and it won't matter if you use ram or progmem. You can try a fancy data structure to saves space. Hmm. I wonder which counts as simplest: progmem, bitmasks, or datastructures with sub-byte fields ?

typedef struct segments_ {
 unsigned char a:1;   // a one-bit sub-field of the structure.
 unsigned char b:1;
 unsigned char c:1;
 unsigned char d:1;
 unsigned char e:1;
 unsigned char f:1;
 unsigned char g:1;
 unsigned char dp:1;
} segments_t;

int g = 2;                // LED connected to digital pin 1
int f = 1;                // LED connected to digital pin 2
int e = 3;                // LED connected to digital pin 3
int d = 4;                // LED connected to digital pin 4
int c = 5;                // LED connected to digital pin 5
int b = 6;                // LED connected to digital pin 6
int a = 7;                // LED connected to digital pin 7
int dot = 8;              //LED connected to the digital pin 8

segments_t LED[10] = {
 {1, 1, 1, 1, 1, 1, 0, 0}, // 0
 {0, 1, 1, 0, 0, 0, 0, 0}, // 1
 {1, 0, 1, 1, 0, 1, 1, 0}, // 2
 {1, 1, 1, 1, 0, 0, 1, 0}, // 3
 {0, 1, 1, 0, 1, 1, 1, 0}, // 4
 {1, 0, 1, 1, 0, 1, 1, 0}, // 5
 {0, 0, 1, 1, 1, 1, 1, 0}, // 6
 {1, 1, 1, 0, 0, 0, 0, 0}, // 7
 {1, 1, 1, 1, 1, 1, 1, 0}, // 8
 {1, 1, 1, 0, 0, 1, 1, 0}  // 9
};

void setup()                    // run once, when the sketch start
{ // omitted for space...
}

void loop()                      // run over and over again
{
 int val = 9;
 digitalWrite(g, LED[val].g);   // sets the LED on
 digitalWrite(f, LED[val].f);   // sets the LED on
 digitalWrite(e, LED[val].e);   // sets the LED on
 digitalWrite(d, LED[val].d);   // sets the LED on
 digitalWrite(c, LED[val].c);   // sets the LED on
 digitalWrite(b, LED[val].b);   // sets the LED on
 digitalWrite(a, LED[val].a);   // sets the LED on
}