Go Down

Topic: Passing defined arrays into function, variables not declared in scope (Read 1 time) previous topic - next topic

psppb

Greetings,

I am having a lot of trouble passing arrays into functions. It keeps telling me that the arrays are not defined. They are in the setup() could someone tell me why?

Here are the errors:
BareMinimum.cpp: In function 'void loop()':
BareMinimum:20: error: 'Block_1' was not declared in this scope
BareMinimum:20: error: 'Block_2' was not declared in this scope
BareMinimum:20: error: 'Block_3' was not declared in this scope
BareMinimum:20: error: 'Block_4' was not declared in this scope

Here is the code:
Code: [Select]
void setup() {
  // put your setup code here, to run once:

#define Block_Size 16

#define S3_pin 53
#define S2_pin 51
#define S1_pin 49
#define S0_pin 47


int Block_1[Block_Size];
int Block_2[Block_Size];
int Block_3[Block_Size];
int Block_4[Block_Size];

}

void loop() {
  // put your main code here, to run repeatedly:
  CheckBlocks(Block_1, Block_2, Block_3, Block_4);
 
 
 
 
}

void CheckBlocks(int Block_1[], int Block_2[], int Block_3[], int Block_4[]){
  int quadrent = 0;
  for(int channel= 0; channel < 16; channel++){
       set_channel(channel); // turns on channel
       delay(1);
       Block_1[channel] = analogRead(A15);
  }// End for loop
  quadrent = 1;
  for(int channel= 0; channel < 16; channel++){
       set_channel(channel); // turns on channel
       delay(1);
       Block_1[channel] = analogRead(A14);
  }// End for loop ;
  quadrent = 2;
  for(int channel= 0; channel < 16; channel++){
       set_channel(channel); // turns on channel
       delay(1);
       Block_1[channel] = analogRead(A13);
  }// End for loop );
  quadrent = 3;
  for(int channel= 0; channel < 16; channel++){
       set_channel(channel); // turns on channel
       delay(1);
       Block_1[channel] = analogRead(A12);
  }// End for loop
}//end function

 



//This function activates the muxes
void set_channel(int channel) {
     switch(channel)
       {
        case 0:  digitalWrite(S0_pin, LOW);
                 digitalWrite(S1_pin, LOW);
                 digitalWrite(S2_pin, LOW);
                 digitalWrite(S3_pin, LOW);
                 break;
        case 1:  digitalWrite(S0_pin, HIGH);
                 digitalWrite(S1_pin, LOW);
                 digitalWrite(S2_pin, LOW);
                 digitalWrite(S3_pin, LOW);
                 break;
        case 2:  digitalWrite(S0_pin, LOW);
                 digitalWrite(S1_pin, HIGH);
                 digitalWrite(S2_pin, LOW);
                 digitalWrite(S3_pin, LOW);
                 break;
        case 3:  digitalWrite(S0_pin, HIGH);
                 digitalWrite(S1_pin, HIGH);
                 digitalWrite(S2_pin, LOW);
                 digitalWrite(S3_pin, LOW);
                 break;
        case 4:  digitalWrite(S0_pin, LOW);
                 digitalWrite(S1_pin, LOW);
                 digitalWrite(S2_pin, HIGH);
                 digitalWrite(S3_pin, LOW);
                 break;
        case 5:  digitalWrite(S0_pin, HIGH);
                 digitalWrite(S1_pin, LOW);
                 digitalWrite(S2_pin, HIGH);
                 digitalWrite(S3_pin, LOW);
                 break;
        case 6:  digitalWrite(S0_pin, LOW);
                 digitalWrite(S1_pin, HIGH);
                 digitalWrite(S2_pin, HIGH);
                 digitalWrite(S3_pin, LOW);
                 break;
        case 7:  digitalWrite(S0_pin, HIGH);
                 digitalWrite(S1_pin, HIGH);
                 digitalWrite(S2_pin, HIGH);
                 digitalWrite(S3_pin, LOW);
                 break;
       case 8:   digitalWrite(S0_pin, LOW);
                 digitalWrite(S1_pin, LOW);
                 digitalWrite(S2_pin, LOW);
                 digitalWrite(S3_pin, HIGH);
                 break;
        case 9:  digitalWrite(S0_pin, HIGH);
                 digitalWrite(S1_pin, LOW);
                 digitalWrite(S2_pin, LOW);
                 digitalWrite(S3_pin, HIGH);
                 break;
        case 10: digitalWrite(S0_pin, LOW);
                 digitalWrite(S1_pin, HIGH);
                 digitalWrite(S2_pin, LOW);
                 digitalWrite(S3_pin, HIGH);
                 break;
        case 11: digitalWrite(S0_pin, HIGH);
                 digitalWrite(S1_pin, HIGH);
                 digitalWrite(S2_pin, LOW);
                 digitalWrite(S3_pin, HIGH); 
                 break;
        case 12: digitalWrite(S0_pin, LOW);
                 digitalWrite(S1_pin, LOW);
                 digitalWrite(S2_pin, HIGH);
                 digitalWrite(S3_pin, HIGH);
                 break;
        case 13: digitalWrite(S0_pin, HIGH);
                 digitalWrite(S1_pin, LOW);
                 digitalWrite(S2_pin, HIGH);
                 digitalWrite(S3_pin, HIGH);
                 break;
        case 14: digitalWrite(S0_pin, LOW);
                 digitalWrite(S1_pin, HIGH);
                 digitalWrite(S2_pin, HIGH);
                 digitalWrite(S3_pin, HIGH);
                 break;
        case 15: digitalWrite(S0_pin, HIGH);
                 digitalWrite(S1_pin, HIGH);
                 digitalWrite(S2_pin, HIGH);
                 digitalWrite(S3_pin, HIGH);
                 break;       
        default: digitalWrite(S0_pin, LOW);
                 digitalWrite(S1_pin, LOW);
                 digitalWrite(S2_pin, LOW);
                 digitalWrite(S3_pin, LOW);   
                 break;
                 }//end case
}//end function



WizenedEE

Each one is defined as a local variable within setup, and the variables go out of "scope" (out of sight) when setup finishes.

Move the declarations of the arrays to the top of the file.

PaulS

Code: [Select]
void CheckBlocks(int Block_1[], int Block_2[], int Block_3[], int Block_4[]){

This isn't right, either. You need to tell the function how big the arrays are, or pass pointers, instead. If you pass pointers, you'll need some sentinel value in the array so the function knows when it reaches the end, or you'll need to pass an explicit size for each (or all) array(s).

WizenedEE


Code: [Select]
void CheckBlocks(int Block_1[], int Block_2[], int Block_3[], int Block_4[]){

This isn't right, either. You need to tell the function how big the arrays are, or pass pointers, instead. If you pass pointers, you'll need some sentinel value in the array so the function knows when it reaches the end, or you'll need to pass an explicit size for each (or all) array(s).
No, that's correct. It's just syntactic sugar around passing pointers. And he's using the constant 16 for the length, although that should be Block_Size. With just moving the void setup() { line, it compiles fine.

Go Up