Can you call a variable from an object in another tab?

I'm attempting to call a variable in void loop() that is declared in a function in another tab (photocellRead). Is this possible in Arduino 1.0? Am I making any glaring mistakes here? Thanks in advance.

void loop(){
photocellRead();

//frit actuators start
switch (solarPosition){
case 1:
digitalWrite(fritAct1, LOW);
digitalWrite(fritAct2, LOW);
break;
case 2:
analogWrite(fritAct1, 50); // change this latter to reflect PWM
analogWrite(fritAct2, 50);
break;
case 3:
analogWrite(fritAct1, 100); // change this latter to reflect PWM
analogWrite(fritAct2, 100);
break;
case 4:
analogWrite(fritAct1, 50); // change this latter to reflect PWM
analogWrite(fritAct2, 50);
break;
case 5:
digitalWrite(fritAct1, LOW);
digitalWrite(fritAct2, LOW);
break;
}

void loop(){
photocellRead();

//frit actuators start
switch (solarPosition){
case 1:
digitalWrite(fritAct1, LOW);
digitalWrite(fritAct2, LOW);
break;
case 2:
analogWrite(fritAct1, 50); // change this latter to reflect PWM
analogWrite(fritAct2, 50);
break;
case 3:
analogWrite(fritAct1, 100); // change this latter to reflect PWM
analogWrite(fritAct2, 100);
break;
case 4:
analogWrite(fritAct1, 50); // change this latter to reflect PWM
analogWrite(fritAct2, 50);
break;
case 5:
digitalWrite(fritAct1, LOW);
digitalWrite(fritAct2, LOW);
break;
}

You can reference a variable, but not call it, unless the variable is a function pointer.

(Is it just me, or are those two items of code identical?)

Sorry about the paste. Here is the other function.

void photocellRead(){

pinMode(interLight1, INPUT);
pinMode(interLight2, INPUT);
pinMode(interLight3, INPUT);

pinMode(exterLight1, INPUT);
pinMode(exterLight2, INPUT);
pinMode(exterLight3, INPUT);
pinMode(exterLight4, INPUT);
pinMode(exterLight5, INPUT);

int photoCell1 = analogRead(interLight1);
int photoCell2 = analogRead(interLight2);
int photoCell3 = analogRead(interLight3);

int avgInteriorLight = (photoCell1 + photoCell2 + photoCell3)/3;

int photoCell4 = analogRead(exterLight1);
int photoCell5 = analogRead(exterLight2);
int photoCell6 = analogRead(exterLight3);
int photoCell7 = analogRead(exterLight4);
int photoCell8 = analogRead(exterLight5);

Serial.println(" ");
Serial.println("Begin");
Serial.println("interior Light Sensor 1: ");
Serial.println(photoCell1, DEC);
Serial.println("interior Light Sensor 2: ");
Serial.println(photoCell2, DEC);
Serial.println("interior Light Sensor 3: ");
Serial.println(photoCell3, DEC);

Serial.println("interior Light Average Value: ");
Serial.println(avgInteriorLight, DEC);
Serial.println(" ");

Serial.println("Exterior Light Sensor 1: ");
Serial.println(photoCell4, DEC);
Serial.println("Exterior Light Sensor 2: ");
Serial.println(photoCell5, DEC);
Serial.println("Exterior Light Sensor 3: ");
Serial.println(photoCell6, DEC);
Serial.println("Exterior Light Sensor 4: ");
Serial.println(photoCell7, DEC);
Serial.println("Exterior Light Sensor 5: ");
Serial.println(photoCell8, DEC);

int photoArray[] = {photoCell4, photoCell5, photoCell6, photoCell7, photoCell8};

solarPosition = getIndexOfMaximumValue(photoArray, 5) + 1;

Serial.println(" ");
Serial.println("Solar position relative to array index: ");
Serial.println(solarPosition, DEC);
}

I don't know why, but the IDE wouldn't let me copy the other set and I hadn't noticed when first posted.

Regardless of whether the function are in different tabs or the same tab, any variable declared within a function can never be referenced in another function.
For that, it would need global/external scope.

AWOL:
Regardless of whether the function are in different tabs or the same tab, any variable declared within a function can never be referenced in another function.
For that, it would need global/external scope.

Bummer...thanks for the info.

Sorry, that should have read "directly referenced".
You can, of course pass pointers and references between functions.

1 Like

Another thing is it isn't necessary to set the pin mode of a pin you're going to do an analogRead on.

AWOL:
You can reference a variable, but not call it, unless the variable is a function pointer.

I think AWOL tries to fool you a bit :wink: Of course you can use both functions and global variables:
One tab:

char msg[] = "Hello World";
void sayHello()
{
  Serial.println(msg);
}

Another tab:

extern void sayHello(); // NOT required
extern char msg[]; // required

void setup() {
}

void loop() {
  if (strlen(msg) > 2) sayHello();
  delay(2000);
}

Notice the "extern" keyword !
Arduino IDE tries to be smart and secretly adds the first line by itself.

1 Like