我想在 Arduino 的程序中同时 运行 两个函数
I want to run two function simultaneously in program In Arduino
void a(){
delay(3000)
# other statement
}
void b(){
delay(3000)
# other statement
}
现在我想 运行 这些函数并行,但是当我调用第一个函数时,它应该取消其他函数的延迟时间和函数 b 的其他功能,反之亦然。我的目标是 运行 它并行。
由于真正的 Arduino 没有 运行 操作系统,您必须自己进行某种多任务处理。避免编写阻塞函数。您的函数 void a()
应类似于:
void a() {
static unsigned long lastrun;
if (millis() - lastrun >= 3000) {
lastrun=millis();
// non-blocking code to be executed once every 3 sec
}
}
由于 Arduino 是用 C++ 编写的,请随意创建一个计时器 class 为您执行此操作(并避免静态或全局变量)但这可能超出了您的问题。
void a(){
delay(3000)
# other statement
}
void b(){
delay(3000)
# other statement
}
现在我想 运行 这些函数并行,但是当我调用第一个函数时,它应该取消其他函数的延迟时间和函数 b 的其他功能,反之亦然。我的目标是 运行 它并行。
由于真正的 Arduino 没有 运行 操作系统,您必须自己进行某种多任务处理。避免编写阻塞函数。您的函数 void a()
应类似于:
void a() {
static unsigned long lastrun;
if (millis() - lastrun >= 3000) {
lastrun=millis();
// non-blocking code to be executed once every 3 sec
}
}
由于 Arduino 是用 C++ 编写的,请随意创建一个计时器 class 为您执行此操作(并避免静态或全局变量)但这可能超出了您的问题。