Reputation:
I am trying to get to figure out how to get this print function to work outside of the function the variable is in.
int altitude = 0;
void setup() {
Serial.begin(9600);
simulateAltitude();
}
void loop() {
Serial.println(altitude); // This does not work.
}
int simulateAltitude() {
int a = 0;
while ( a == 0 ) {
altitude += 1;
Serial.println(altitude); // This does work.
delay(1);
}
}
My biggest problem is the void loop() is not getting the value of altitude from the while loop in the int simulateAltitude function. This is being used on an Arduino UNO micro-controller board using C.
I am aware it is an infinite loop, it is for testing purposes only.
Upvotes: 0
Views: 1024
Reputation: 881383
It's not printing the altitude from within loop()
because loop()
is never actually being called.
Remember this about Arduino. The setup()
function is called once at boot time and, once it returns, the loop()
function is called over and over again.
With the way you have it, your setup()
function calls simulateAltitude()
which goes into an infinite loop, so it never returns. It does not run simulateAltitude()
and loop()
concurrently.
You might be better off looking at something like:
void loop() {
Serial.println(altitude);
increaseAltitude();
}
int increaseAltitude() {
altitude += 1;
delay(1);
}
Upvotes: 2
Reputation: 11531
There are two problems in this code. The first is that simulateAltitude
will never be called, so altitude
will never update. The second problem is that neither a
nor altitude
are actually initialized.
Upvotes: 0
Reputation: 4465
You have 2 problems here:
First of all, you need to initialize a
and altitude
. Give them initial values (say, 0
).
Second, you didn't setup your Serial Monitor. Add this line to your setup function:
Serial.begin(9600); //9600 is more common but you can set other update frequencies
Upvotes: 0