for loop, increment index of an integer

Hi!

I’m building a clock based on 7 seg displays.

What I’m haveing problems with is makeing a for loop that helps me cut down on a lot of code.
I want a loop to like this:

Snippet:

 int dataPin = 2;
int data0[] = { HIGH, LOW, LOW, HIGH, HIGH, HIGH, HIGH};//7 segmen codes for the number 1 and 2
int data1[] = { LOW, LOW, HIGH, LOW, LOW, HIGH, LOW};

int value = 0;

void setup(){
    pinMode(dataPin, OUTPUT);
}//setup


void loop() {
for (int i=0; i < 2; i++){
   for (value = 6; value > -1; value--){// value referes to the bit inside data (array)
      

digitalWrite(dataPin, data+i[value]);//want the i to increment and refere to data0 and data1

 Serial.begin (9600);
Serial.println (data+i[value]);// for debugging
   }//inner for
}//outer for

}//loop

I know this doesn’t work, but hopefully someone does know of a method how to do it.

This is the error i get:
sketch_apr25b:16: error: ‘data’ was not declared in this scope
sketch_apr25b:16: error: invalid types ‘int[int]’ for array subscript

I have tried this, but i didn’t solve my problem:

   String g_data = String("data");
  for (int j=0; j <2; j++){
    String g_digit = String(j);
 String digit = String(g_data + g_digit);
Serial.println (digit); //prints data0
Serial.println (digit[value]); //prints d, as in the 1 element of digit. but I want it to print a "1" (HIGH), as in the first element of data0.
}//for

data+i doesn't mean what you think it means. That means, take the value of data (which is a pointer to first array value) and add i to it. The problem is, you have a data0 and a data1, not a data so the compiler errors when it sees data.

data+i is functionally equivalent to &data[0+i].

What you need is a multidimensional array:

data[][] = { { HIGH, LOW, LOW, HIGH, HIGH, HIGH, HIGH}, {LOW, LOW, HIGH, LOW, LOW, HIGH, LOW} }

If you really want to cut the code down, I would consider finding a seven segment library and just using that, then you would just have to do something like sevenseg.write(someNumber). It's a pretty basic library that I'm sure tons have made, so I'm sure it's not difficult to find.

Thanks for your reply. I just tried out your suggestion and got the following error: sketch_apr25b:3: error: expected constructor, destructor, or type conversion before '=' token

Can't quite see your code.

Sorry,AWOL.

Here is what i tried:

 int dataPin = 2;
//int data0[] = { HIGH, LOW, LOW, HIGH, HIGH, HIGH, HIGH};//7 segmen codes for the number 1 and 2
//int data1[] = { LOW, LOW, HIGH, LOW, LOW, HIGH, LOW};
data[][] = {  { HIGH, LOW, LOW, HIGH, HIGH, HIGH, HIGH}, {LOW, LOW, HIGH, LOW, LOW, HIGH, LOW} };
int value = 0;

void setup(){
    pinMode(dataPin, OUTPUT);
}//setup


void loop() {
for (int i=0; i < 2; i++){
   for (value = 6; value > -1; value--){// value referes to the bit inside data
      

//digitalWrite(dataPin, data[i][value]);//want the i to increment and refere to data0 and data1

 Serial.begin (9600);
Serial.println (data[i][value]);// for debugging
   }//inner for
}//outer for




}//loop

but got the error: sketch_apr25b:3: error: expected constructor, destructor, or type conversion before ‘=’ token

data has neither type, nor dimension.

ahol: Thanks for your reply. I just tried out your suggestion and got the following error: sketch_apr25b:3: error: expected constructor, destructor, or type conversion before '=' token

You didn't tell the compiler what type of data is the array. Is it a byte, int, char, long, etc...

aha, the type was a glitch. But for the array dimension i now remeber. I'ts a long time ago I programmed something simular in C++. :)

But, still when I print I get something weird.

 int data[2][7] = {  { HIGH, LOW, LOW, HIGH, HIGH, HIGH, HIGH}, {LOW, LOW, HIGH, LOW, LOW, HIGH, LOW} }
  Serial.println (data[0][0]);// for debugging

It prints: #, would have expected: 0

Any ideas?

ahol: aha, the type was a glitch. But for the array dimension i now remeber. I'ts a long time ago I programmed something simular in C++. :)

But, still when I print I get something weird.

 int data[2][7] = {  { HIGH, LOW, LOW, HIGH, HIGH, HIGH, HIGH}, {LOW, LOW, HIGH, LOW, LOW, HIGH, LOW} }
  Serial.println (data[0][0]);// for debugging

It prints: #, would have expected: 0

Any ideas?

You're missing a semicolon at the end of the array declaration. Otherwise, the code is fine. If you're having issues, it will be somewhere in the rest of your code/setup.

Only call Serial.begin() once, in setup().

Why are you using 2 byte ints to store one byte values? The data array's type should be uint8_t or byte.

Solved!

PaulS, using byte fix all my problems, thank you!

Everybody else, thanks for your replies.