array help

my goal is to make a scrolling 4 channel data graph using a tft display. Got the display test programs working with my leonardo but having programming problems, stuck using arrays. I plan on using an array for each data channel’s y position, shifting the data one channel to the left each update.

BUT I can’t get my array test debug program to work! I keep getting errors that the array was not defined in this scope in my serial.print commands. Is there a problem using serial.print with arrays??

here is my test program:

void setup() {
int ch1[6] = {1,2,3,4,5,6}; //this will be data channel 1
int ch2[6] = {11,12,13,14,15,16}; //this will be data channel 2
int ch3[6] = {21,22,23,24,25,26}; //this will be data channel 3
int ch4[6] = {31,32,33,34,35,36}; //this will be data channel 4

Serial.begin(9600);
while (!Serial) ; // while the serial stream is not open, do nothing:
uint16_t time = millis();
time = millis() - time;

Serial.println(time, DEC); //checking to make sure serial is working
delay(500);
}

void loop(){
Serial.println (""); // serial print intiation values each loop
Serial.print (“i=0”);
Serial.print (" ch1[0]=");
Serial.print (ch1[0]);
Serial.print (" ch2[0]=");
Serial.print (ch2[0]);
Serial.print (" ch3[0]=");
Serial.print (ch3[0]);
Serial.print (" ch4[0]=");
Serial.print (ch4[0]);
delay(1000);
drawData(); //shifts data left by one in arrays
delay(5000);
}

void drawData (){ // move data left then get and draw new data
for(int i=0; i<5; i++) { // 0-5 columns, old data left on right
Serial.println(“i=”);
Serial.print(i);
ch1*=ch1[i+1];*

  • Serial.print(“ch1=”);*
    _ Serial.print(ch1*);_
    _ ch2=ch2[i+1];
    Serial.print(“ch2=”);
    Serial.print(ch2);
    ch3=ch3[i+1];
    Serial.print(“ch3=”);
    Serial.print(ch3);
    ch4=ch4[i+1];
    Serial.print(“ch4=”);
    Serial.print(ch4);
    delay(1000);
    }
    }
    [/color]*_

and the ERROR report:
arrayTest_02.ino: In function ‘void loop()’:
arrayTest_02:21: error: ‘ch1’ was not declared in this scope
arrayTest_02:23: error: ‘ch2’ was not declared in this scope
arrayTest_02:25: error: ‘ch3’ was not declared in this scope
arrayTest_02:27: error: ‘ch4’ was not declared in this scope
arrayTest_02.ino: In function ‘void drawData()’:
arrayTest_02:37: error: ‘ch1’ was not declared in this scope
arrayTest_02:40: error: ‘ch2’ was not declared in this scope
arrayTest_02:43: error: ‘ch3’ was not declared in this scope
arrayTest_02:46: error: ‘ch4’ was not declared in this scope
arrayTest_02.ino: At global scope:
arrayTest_02:67: error: expected declaration before ‘}’ token

Move the declarations of ch1 etc. out of “setup ()” and place them before any function definitions. That way they are global variables.

Then you'll have to work out what you meant by this

ch1=ch1[i+1];

You can't make an entire array equal to one of it's members :slight_smile: I'm sure you'll sort it.

There are essentially three types of scope:

  1. Global – a data item defined outside of a function has global scope and is “visible and lives” everywhere in the source file. It is accessible (usable) at all points in the source file from its definition to the end of the file.

  2. Function – a data item defined within a function is accessible from its point of definition to the closing brace of the function. It cannot be accessed outside of the function in which it is defined.

  3. Block – a data item is defined within a statement block, like a while, for, if, etc. The item is accessible from its point of definition to the closing brace of the statement block. It is not accessible outside the statement block.

Consider the following example:

int myArray[10];   // Global scope -- defined outside of any function or statement block

void setup() {
   int x;   // Function scope -- defined within a function block.

   if (x == 0) {
      int y;   // Block scope -- defined within an if statement block

      for (int i = 0; i < myArray[0]; i++) {   // i has block scope 
      // more code...
      }   // End of for block -- Variable i goes out of scope here (it dies)
   }   // End of if block -- Variable y goes out of scope here
}    // End of setup() -- Variable x goes out of scope here

void loop() {
   // some code
}  End of source file -- Variable myArray[] goes out of scope here

Your arrays are defined with function scope in setup() and, therefore, are not accessible in loop().

KenF:
Then you'll have to work out what you meant by this

ch1=ch1[i+1];

You can't make an entire array equal to one of it's members :slight_smile: I'm sure you'll sort it.

Which is why we ask people to use code tags.

Thanks to all of you for your help, I have so much too learn! So variables defined in setup have scope limited to setup, wow. Please advise, where exactly is the best place to declare variables you want to function globally??

Another Q: is there any way to initialize every member of an array to the same value, eg =0 without a loop?

good thing it didn't even get to my other oops ch1=ch1[i+1];, sigh embarassing.

thanks all

Declare the array outside of any function to make it global. Note that setup() and loop() are functions.

You can give array elements values when you declare the array.
int anArray = {0,0,0,0,0,0,0};

AWOL:
Which is why we ask people to use code tags.

Ah! the old bbcode italic trap!

Doh!

Strickly speaking, you are using data definitions, not declarations. A declaration creates an attribute list for the data item, but does not allocate storage for it. Examples:

int myCube(int a);  // Called a function prototype. Allows the compiler to do type checking
extern int x;   // Variable x is defined in another source file, but the attribute list lets me use it in this file

A definition also creates an attribute list for the data item, but allocates storage for it:

int myCube(int a) {
   return a * a * a;
}

int x;

In these examples, both an attribute list is created, but also storage is allocated for the data items.

Another Q: is there any way to initialize every member of an array to the same value, eg =0 without a loop?

Another way:

memset(myArray, 0, sizeof(myArray));

This standard library function sets the content of myArray[] to zero.

groundfungus:
Declare the array outside of any function to make it global. Note that setup() and loop() are functions.

You can give array elements values when you declare the array.
int anArray = {0,0,0,0,0,0,0};

If you specify the size of the array, you can default initialize it:

int anArray[7] = {};

In fact any omitted value is defaulted:

int anArray[7] = {1, 2 };

//Results in the same array as:
int anArray[7] = {1, 2, 0, 0, 0, 0, 0};