使用for循环作为延迟的替代方式?

huangapple go评论65阅读模式
英文:

Using for-loops as an alternative to delays?

问题

只是询问,如果将for循环函数用作计时器而不是延迟,是否也会在主循环中对其他代码行造成不必要的干扰。

我想要基于Arduino构建一个尾灯系统,该系统与遥控器的油门信号相连接。
英文:

Just asking if the for-loop function used as a timer instead of a delay, is also causing an unwanted disturbance in the main loop for other code lines.

void okLED() {
  for (long i = 0; i < 150000; i++) {
    digitalWrite(LED_BUILTIN, HIGH);
  }
  for (long j = 0; j < 150000; j++) {
    digitalWrite(LED_BUILTIN, LOW);
  }
}

void slowLED() {
  for (long i = 0; i < 25000; i++) {
    digitalWrite(LED_BUILTIN, HIGH);
  }
  for (long j = 0; j < 20000; j++) {
    digitalWrite(LED_BUILTIN, LOW);
  }
  for (long k = 0; k < 25000; k++) {
    digitalWrite(LED_BUILTIN, HIGH);
  }
  for (long l = 0; l < 110000; l++) {
    digitalWrite(LED_BUILTIN, LOW);
  }
}

Something like this.

I want to build a rear light system based on an Arduino interconnected with the throttle signal from the RC control.

答案1

得分: 4

实际嵌入式系统在Arduino人工世界之外几乎从不使用繁忙延迟/忙等待循环来控制时间。这种忙等待方式存在严重问题,因为它们会阻塞程序执行,并导致100%的CPU利用率和100%的电流消耗,没有任何好的原因。

在专业环境中使用的替代方法涉及硬件定时器。可以使用专用定时器硬件外设或专用实时时钟硬件外设。这些可以通过轮询标志或中断来使用。

对于特定的LED控制,定时器外设的“输出比较”功能可以在定时器运行完毕时自动激活/停用GPIO引脚。PWM功能也非常常见,特别是为了减少电流消耗或在RGB情况下启用多种颜色。

在任何情况下,这些各种硬件功能都允许以与主程序并行的方式控制LED。

英文:

Real embedded systems outside the artificial world of Arduino pretty much never use busy-delays/busy-wait loops as a means to control timing. Such busy-waits are deeply problematic since they stall program execution and cause 100% CPU utilization with 100% current consumption, for no good reasons.

Alternatives used in a professional context involve hardware timers. Either the dedicated timer hardware peripherals, or a dedicated real-time clock hardware peripheral. These can be used either by polling a flag or by interrupts.

For controlling LEDs specifically, "output compare" features of a timer peripheral can be used to automatically activate/deactivate a GPIO pin when a timer runs out. PWM features are also very common, particularly for reducing current consumption or to enable multiple colors in case of RGB.

In either case, any of these various hardware features allow the LED to be controlled in a concurrent manner at the side of the main program.

答案2

得分: 1

以下是翻译好的内容:

无论您是使用繁忙的for循环还是繁忙等待的delay()函数,两者都不好。

学习不延迟的闪烁:https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay

要了解更高级的协作多任务技术,请参阅我的答案:如何进行高分辨率、基于时间戳的、非阻塞的、单线程的协作多任务

要读取RC PWM控制信号,这有点复杂,但我有一个旧的示例在这里:https://github.com/ElectricRCAircraftGuy/PWM_Reader2_WORKS_PERFECTLY_Hayden_car_lights。

这是核心代码。它以与油门设置成比例的速度闪烁LED。

YouTube视频:

使用for循环作为延迟的替代方式?

英文:

Whether you're using a busy for loop or the busy-wait delay() function, both are bad.

Study Blink without delay: https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay

For more advanced cooperative multitasking techniques, see my answer here: How to do high-resolution, timestamp-based, non-blocking, single-threaded cooperative multi-tasking

For reading the RC PWM control signal, that's complicated, but I have an old example here: https://github.com/ElectricRCAircraftGuy/PWM_Reader2_WORKS_PERFECTLY_Hayden_car_lights.

Here's the core code. It blinks an LED at a rate proportional to the throttle setting.

YouTube video:

使用for循环作为延迟的替代方式?

huangapple
  • 本文由 发表于 2023年6月15日 14:11:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479575.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定