Is this possible?

I have a project where 2 different sensors readings go into 2 different loops.

What I would like to do is have a 3rd loop that prints the data from the 2 loops.

But because they are not global variables the 3rd loop knows nothing about whats in loops 1 and 2. Is it possible to print out the data of loop 1 and 2 in loop 3? I have an example below.

void setup() {
Serial.begin(115200);

}

void loop() {
loop1();
loop2();
loop3();
}
void loop1(){
String test01 = "Fred";

}
void loop2(){
String test02 = "bob";

}
void loop3(){
Serial.println(test01);
delay(1000);
Serial.println(test02);
delay(1000);

}

But because they are not global variables

make them global...


Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

...or else use them as the functions that they are, and return the local values:

void setup() {
  Serial.begin(115200);
}

void loop() {
  loop3();
}

String loop1(){
  String test01 = "Fred";
  return test01;
}
String loop2(){
  String test02 = "bob";
   return test02;
}
void loop3(){
  Serial.println(loop1() );
  delay(1000);
  Serial.println(loop2() );
  delay(1000);
}

Reply #2 has very simple code. But what if the OP has to use some function in the loop? The String function shouldn't use functions. The proper answer is to make the variables global. Declair them outside any loop.

perfect thank you

ArnavPawarAA:
But what if the OP has to use some function in the loop? The String function shouldn't use functions.

What about it ? You could still return something. What do you mean by not using functions ? That’s a very weird statement.

The proper answer is to make the variables global.

there are multiple ways to do something. So ‘proper’ is probably excessive. That’s one low hanging way.

The fact that OP calls those functions loop seems to indicate he is merging two programs and usually the easy way is far from an optimal way (which requires understanding both codes and merging them in one loop).

(And we would not encourage using the String class without clear understanding of what happens in memory).

J-M-L:
make them global...


Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

you said make it global? I have done this but Void4 isn't printing from void1?

String test1;
String test2;
String test3;
void setup() {
   Serial.begin(115200); 

}

void loop() {
 Loop1();
 Loop2();
 Loop3();
 Loop4();

}
void Loop1(){
  String test1 = "Loop1";
}
void Loop2(){
  String test2 = "Loop2";
}
void Loop3(){
  String test3 = "Loop3";
}
void Loop4(){
  Serial.println(test1);
  delay(1000);
}

.

void Loop1(){
  String test1 = "Loop1";
}

test1 is only in scope in Loop1

Do not get used to use the "String" class, it's gonna cause you headaches. Use cstrings (char arrays) instead.

TheMemberFormerlyKnownAsAWOL:

void Loop1(){

String test1 = "Loop1";
}



test1 is only in scope in Loop1

In case the OP doesn't understand, String test1 is declaring a new local String variable inside Loop1() , which is completely separate from the global test1 variable. When the function ends, that local variable no longer exists, and any use of test1 in other parts of the code will refer to the global variable.

david_2018:
In case the OP doesn't understand, String test1 is declaring a new local String variable inside Loop1() , which is completely separate from the global test1 variable. When the function ends, that local variable no longer exists, and any use of test1 in other parts of the code will refer to the global variable.

Can you please show an example how I can make it work?

Just remove String from that line of code. When the compiler sees a variable type designation before a variable name, then it creates a new variable. Using just the variable name refers back to the previously declared variable, the compiler already knows what the type of the variable is.

void Loop1(){
  test1 = "Loop1";
}

Your a legend it's exactly what I want. Works perfectly.