Pages: [1]   Go Down
Author Topic: Can you call a variable from an object in another tab?  (Read 535 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 38
NerdArch
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Quote
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;
  }


Quote
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;
  }

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25868
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?)
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 38
NerdArch
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 38
NerdArch
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25868
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 38
NerdArch
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25868
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25868
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Germany
Offline Offline
Faraday Member
**
Karma: 56
Posts: 2983
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-wink Of course you can use both functions and global variables:
One tab:
Code:
char msg[] = "Hello World";
void sayHello()
{
  Serial.println(msg);
}
Another tab:
Code:
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.

Logged

Pages: [1]   Go Up
Jump to: