You can call as many functions from the loop function as you like. In your example you could choose which one to call based on the value of your x variable. If you want to be able to share variables between these functions, you will either have to pass them as parameters, probably as references, or more simply, make them global (declare them outside any function).
Yes, you can define functions and call them from other functions, including loop().
If a variable is defined outside of any function it is "global" and shared across all functions in that file. If its value is set in one function the new value will be seen in other functions.
void loop() {
if (x == 1) {
nextchunkofcode(); //call next loop}
//main code here
}
}
void nextchunkofcode() {
//another chunk of code
a = 1;
b = 2;
}
Hmmm... Thanks for your replies! I was planning something like this:
void setup() {
int x = 0;
int a = 0;
int b = 0;
int y = 0;
}
void loop() {
if (x == 1) {
nextchunkofcode(); //call next loop}
//main code here
if y == some value obtained from nextchunkofcode() //use from nextchunkofcode()
}
}
void nextchunkofcode() {
//another chunk of code
a = 1;
b = 2;
y = analogRead(pin);
}
Would this possibly work?
Not with the variables declared inside setup(). They are scoped only to setup(). Move them to before setup() making them global in scope. Global means available everywhere.