gpt4 book ai didi

C++/Arduino 闪光 LED 不同间隔

转载 作者:行者123 更新时间:2023-11-28 01:25:42 26 4
gpt4 key购买 nike

我在 Arduino 上有以下代码,我想按照以下规则使 LED 闪烁:

LED 应快速闪烁两次,然后暂停更长时间,以便实现以下节奏:

开-开----开-开----开-开...
打开应持续 125 毫秒,短停应持续 75 毫秒,长停顿应持续 500 毫秒。

这是我到目前为止所做的:

unsigned long flashOn = 125;
unsigned long flashOff = 75; //first pause must be a short one
unsigned long flashCount = 0;
void handleFrontFlash() {
byte beforeState = digitalRead(LED_Front);
flashOff = (flashCount % 2 > 0) ? 75 : 500;
digitalWrite(LED_Front, (millis() % (flashOn + flashOff)) < flashOn);
byte afterState = digitalRead(LED_Front);
if(beforeState == LOW and afterState == HIGH)
flashCount++;
}

我的想法是计算 LED 点亮的次数,并使用模数来确定循环是否完成。然而,双闪仅在LED每闪烁8次时实现,其余时间LED闪烁一次。
有人可以帮我吗?

最佳答案

一个你想要的例子,我使用简单的函数来轻松理解程序,我使用的是 LED_BUILTIN,它是 arduino uno 或 mega 的 led 13

unsigned long currentMillis;
unsigned long previousMillis = 0;
unsigned long interval;
int Compteur = 1;
int ledState = HIGH;
int state = 0;

void setup(){
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH); // i begin with HIGH during 125ms
interval = 125;
previousMillis = 0;
}

// Main loop
void loop() {
currentMillis = millis();
if (currentMillis - previousMillis >= interval) {

previousMillis = currentMillis;
switch (state) {
case 0:
ledState = LOW;
interval = 75;
state = 1;
break;
case 1:
ledState = HIGH;
interval = 125;
state = 2;
break;
case 2:
ledState = LOW;
interval = 500;
state = 3;
break;
case 3:
ledState = HIGH;
interval = 125;
state = 0;
Compteur++; // count the number of beginning new sequence
break;
default:
// statements
break;
}
digitalWrite(LED_BUILTIN, ledState);
}
}

关于C++/Arduino 闪光 LED 不同间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53978811/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com