C函数之间的全局变量

时间:2022-04-21 19:42:12

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.

我最大的问题是void loop()没有从int simulateAltitude函数中的while循环获取高度值。这是在使用C的Arduino UNO微控制器板上使用的。

I am aware it is an infinite loop, it is for testing purposes only.

我知道它是一个无限循环,它仅用于测试目的。

3 个解决方案

#1


2  

It's not printing the altitude from within loop() because loop() is never actually being called.

它不是从loop()内打印高度,因为实际上从未调用loop()。

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.

请记住这关于Arduino。 setup()函数在引导时被调用一次,一旦返回,就会一遍又一遍地调用loop()函数。

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.

使用它的方式,你的setup()函数调用simulateAltitude()进入无限循环,所以它永远不会返回。它不会同时运行simulateAltitude()和loop()。

You might be better off looking at something like:

你最好看一下像这样的东西:

void loop() {
    Serial.println(altitude);
    increaseAltitude();
}

int increaseAltitude() {
    altitude += 1;
    delay(1);
}

#2


0  

You have 2 problems here:

你有2个问题:

First of all, you need to initialize a and altitude. Give them initial values (say, 0).

首先,您需要初始化a和高度。给它们初始值(比如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

#3


0  

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.

这段代码有两个问题。第一个是永远不会调用simulateAltitude,因此高度永远不会更新。第二个问题是实际上没有初始化高度和高度。

#1


2  

It's not printing the altitude from within loop() because loop() is never actually being called.

它不是从loop()内打印高度,因为实际上从未调用loop()。

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.

请记住这关于Arduino。 setup()函数在引导时被调用一次,一旦返回,就会一遍又一遍地调用loop()函数。

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.

使用它的方式,你的setup()函数调用simulateAltitude()进入无限循环,所以它永远不会返回。它不会同时运行simulateAltitude()和loop()。

You might be better off looking at something like:

你最好看一下像这样的东西:

void loop() {
    Serial.println(altitude);
    increaseAltitude();
}

int increaseAltitude() {
    altitude += 1;
    delay(1);
}

#2


0  

You have 2 problems here:

你有2个问题:

First of all, you need to initialize a and altitude. Give them initial values (say, 0).

首先,您需要初始化a和高度。给它们初始值(比如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

#3


0  

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.

这段代码有两个问题。第一个是永远不会调用simulateAltitude,因此高度永远不会更新。第二个问题是实际上没有初始化高度和高度。