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.
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
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
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...
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.