c++ - 使用 arduino/c++ 每隔一段时间使 LED 闪烁 1 到 3 次

标签 c++ arduino

我想让 LED 每隔一段时间闪烁一次,很像旧的 BIOS-BEEPS,给我程序状态的视觉反馈,我已经让我的代码工作了 3 次短闪,但是对于每个额外的闪光灯我都必须手动添加代码。

我想,一定有更好的方法,但我想不通,因为函数不能在闪烁时使用延迟或循环,从而停止程序的其余部分...

因为我想在编程方面做得更好,所以我想让你问一下,有什么功能可以在不每次添加代码的情况下工作?

这是我的 1 到 3 次闪光的代码,我为您稍微清理了一下:

flag++ // gets incremented every 100ms by interrupt no matter what...

// function to flash an LED
// each flash should be 200ms long (2 increments of 'flag')
// 200ms LED on, 200ms LED off
// given is the number of flashes and the duration till it begins 
// to flash again. 
void blink(int how_many_times, int duration) {

  if (how_many_times >= 1) {  // blink once
    switch (flag) {
    case 2:  // 200ms
      LED_ON;
      break;
    case 4:
      LED_OFF;
      break;
    }
  }
  if (how_many_times >= 2) { // blink twice
    switch (flag) {
    case 6:
      LED_ON;
      break;
    case 8:
      LED_OFF;
      break;
    }
  }
  if (how_many_times >= 3) { // blink 3 times
    switch (flag) {
    case 10:
      LED_ON;
      break;
    case 12:
      LED_OFF;
      break;
    }
  }
  if(flag >= duration*10) { // *10 because duration is in 'seconds'
    flag=0;
  }
}

// to let it blink 3 times fast every 5 seconds:
blink(3, 5);

最佳答案

既然您想在编程方面做得更好,我建议您首先对变量进行更多描述。例如,flag 并没有真正描述该变量在做什么。它真的很像一个定时器,为什么不叫它 timer_100ms 呢?然后您就会确切地知道该变量的用途。

我还建议对 case 语句使用枚举,这样您想要完成的事情就更清楚了。例如:

/* This timer is incremented every 100ms by the interrupt. */
int timer_100ms;

void blink (int how_many_times, int duration)
{
    enum
    {
        ELAPSED_TIME_200MS = 2,
        ELAPSED_TIME_400MS = 4,
        /* Fill in other values */
        ELAPSED_TIME_1200MS = 12
    };

    if (how_many_times >= 1)
    {
        switch (timer_100ms)
        {
            case ELAPSED_TIME_200MS:
                LED_ON;
                break;

            case ELAPSED_TIME_400MS:
                LED_OFF;
                break;
        }
    }

    /* The rest of your code follows... */
}

这样做会使您的代码更具可读性。

话虽这么说,但为了使您的代码更具可扩展性 - 能够在不添加更多代码的情况下创建更多 LED 闪烁 - 我建议从以下内容开始:

void blink (int number_of_blinks, int delay_between_blinks_in_seconds)
{
    #define ELAPSED_TIME_200MS  2

    static int blink_count = 0;
    static int timer_prev_100ms = 0;

    /* If the delay has been satisfied, start blinking the LED. */
    if (timer_100ms >= (delay_between_blinks_in_seconds * 10))
    {
        /* Change the state of the LED every 200ms. */
        if ((timer_100ms - timer_prev_100ms) >= ELAPSED_TIME_200MS)
        {
            timer_prev_100ms = timer_100ms;

            if (led_is_on())
            {
                LED_OFF;
                blink_count++;
            }
            else
            {
                LED_ON;
            }
        }

        if (blink_count >= number_of_blinks)
        {
            timer_100ms      = 0;
            timer_prev_100ms = 0;
            blink_count      = 0;
        }
    }
}

显然,您需要添加 led_is_on() 函数来检查用于闪烁 LED 的引脚的状态。因为,我不知道你的 LED 是如何接线的,所以我不想假设那是如何完成的。不过实现起来非常简单。

关于c++ - 使用 arduino/c++ 每隔一段时间使 LED 闪烁 1 到 3 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31165566/

相关文章:

c++ - 试图减少几乎但不完全是整数类的速度开销

c++ - 跟踪集群组状态 C++

c++ - 递增if语句arduino

c++ - 使用外部方法中断循环

c++ - 相同代码在不同设备上的不同行为

c - 在 Arduino 中读取传感器时按下按钮

c++ - 从冒号分隔的文件中提取信息 - C++

c++ - 使用 C 字符串会给出警告 : "Address of stack memory associated with local variable returned"

c++ - 没有自定义删除器的空 unique_ptr 的析构函数是否微不足道

arduino - server.args() ESP8266 Arduino