在 arduino/c++ 的间隔内使 LED 短时间闪烁 1 到 3 次
flash an LED 1 to 3 short times in an intervall with arduino/c++
我想让 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 是如何接线的,所以我不想假设那是如何完成的。不过实现起来非常简单。
我想让 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 是如何接线的,所以我不想假设那是如何完成的。不过实现起来非常简单。