C++ 带串行通信的淡入式LED
我正在进行串行通信,并尝试制作一个led淡入效果, 这是我的LED功能,它面临延迟问题,显然是环路的。有谁能提出更好的逻辑或解决方案来解决这个问题,而不必在LED中获得延迟C++ 带串行通信的淡入式LED,c++,arduino,serial-port,arduino-uno,fastled,C++,Arduino,Serial Port,Arduino Uno,Fastled,我正在进行串行通信,并尝试制作一个led淡入效果, 这是我的LED功能,它面临延迟问题,显然是环路的。有谁能提出更好的逻辑或解决方案来解决这个问题,而不必在LED中获得延迟 void loop() { // serial communication while(Serial.available() > 0 ){ int inByte = Serial.read(); Serial.print(inByte); if (inBy
void loop() {
// serial communication
while(Serial.available() > 0 ){
int inByte = Serial.read();
Serial.print(inByte);
if (inByte > 0) {
// if byte is 255, then the next 3 bytes will be new rgb value
if (inByte == 255) {
setColors = true;
colorSetCounter = 0;
} else if (setColors) {
switch (colorSetCounter) {
case 0:
redVal = inByte;
break;
case 1:
greenVal = inByte;
break;
case 2:
blueVal = inByte;
setColors = false;
receiveNotes = true;
fill_solid(leds, NUM_LEDS, CRGB::Black);
FastLED.show();
break;
}
colorSetCounter++;
} else if (receiveNotes) {
controlLeds(inByte);
}
}
}
}
void controlLeds (int note) {
note -= 1;
if (!leds[note]) {
leds[note].red = redVal;
leds[note].green = greenVal;
leds[note].blue = blueVal;
}
else {
for(int i =0; i<=255; i++){
leds[note].fadeToBlackBy(i);
FastLED.show();
if(!leds[note]){
break;
}
}
}
FastLED.show();
}
void循环(){
//串行通信
while(Serial.available()>0){
int inByte=Serial.read();
串行打印(以字节为单位);
如果(字节内>0){
//如果字节为255,则接下来的3个字节将是新的rgb值
如果(字节内==255){
setColors=true;
colorSetCounter=0;
}else if(设置颜色){
开关(颜色设置计数器){
案例0:
redVal=inByte;
打破
案例1:
greenVal=inByte;
打破
案例2:
blueVal=inByte;
setColors=false;
receiveNotes=true;
实心填充(LED、数字LED、CRGB::黑色);
fasted.show();
打破
}
colorSetCounter++;
}否则,如果(收到通知){
对照组(inByte);
}
}
}
}
无效受控项(内部注释){
注-=1;
如果(!LED[注意]){
LED[注]。红色=红色;
LED[注]。绿色=绿色值;
发光二极管[注]。蓝色=蓝色;
}
否则{
对于(int i=0;i,您需要像Scheff提到的那样编写非阻塞代码。您可以在函数中使用静态
变量,而不是全局变量。它会记住该函数每次调用的值
下面是一个使用millis()
执行此操作的示例。如果发生某个串行事件,并且不会阻塞代码的其余部分,则我的代码会使LED熄灭:
const int ledPin = 13;
int setValue = 0;
unsigned long lastTime = 0;
const unsigned long refreshTime = 200;
char buffer1[8];
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
if (millis() - lastTime > refreshTime)
{
lastTime = millis();
fadeLED(setValue);
}
}
void fadeLED(int fadeValue)
{
static int currentValue = 0;
if (fadeValue > currentValue) {
currentValue++;
}
if (fadeValue < currentValue) {
currentValue--;
}
analogWrite(ledPin, currentValue);
}
void serialEvent()
{ Serial.readBytesUntil('\n', buffer1, 8);
switch (setValue)
{
case 0:
setValue = 255;
break;
case 255:
setValue = 0;
break;
default:
break;
}
}
const int ledPin=13;
int setValue=0;
无符号长lastTime=0;
常数无符号长刷新时间=200;
char-buffer1[8];
无效设置(){
引脚模式(LED引脚,输出);
}
void循环(){
如果(毫秒()-lastTime>refreshTime)
{
lastTime=millis();
衰减(设定值);
}
}
无效渐减(整数渐减值)
{
静态int currentValue=0;
如果(fadeValue>currentValue){
currentValue++;
}
如果(fadeValue
您需要像Scheff提到的那样编写非阻塞代码。您可以在函数中使用静态
变量,而不是全局变量。它会记住该函数每次调用的值
下面是一个使用millis()
执行此操作的示例。如果发生某个串行事件,并且不会阻塞代码的其余部分,则我的代码会使LED熄灭:
const int ledPin = 13;
int setValue = 0;
unsigned long lastTime = 0;
const unsigned long refreshTime = 200;
char buffer1[8];
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
if (millis() - lastTime > refreshTime)
{
lastTime = millis();
fadeLED(setValue);
}
}
void fadeLED(int fadeValue)
{
static int currentValue = 0;
if (fadeValue > currentValue) {
currentValue++;
}
if (fadeValue < currentValue) {
currentValue--;
}
analogWrite(ledPin, currentValue);
}
void serialEvent()
{ Serial.readBytesUntil('\n', buffer1, 8);
switch (setValue)
{
case 0:
setValue = 255;
break;
case 255:
setValue = 0;
break;
default:
break;
}
}
const int ledPin=13;
int setValue=0;
无符号长lastTime=0;
常数无符号长刷新时间=200;
char-buffer1[8];
无效设置(){
引脚模式(LED引脚,输出);
}
void循环(){
如果(毫秒()-lastTime>refreshTime)
{
lastTime=millis();
衰减(设定值);
}
}
无效渐减(整数渐减值)
{
静态int currentValue=0;
如果(fadeValue>currentValue){
currentValue++;
}
如果(fadeValue
如果我没记错的话,Arduino的主程序是在循环中运行的。(我强烈认为这是有原因的。)因此,您可能需要创建一个更新函数来增加或减少LED的强度,而不是为LED淡入制作一个嵌套的a循环。该函数的变量必须获得LED的总使用寿命-也许,全局变量是有意义的(是的,尽管全局变量通常被认为是不好的样式)。此外,可能需要涉及延迟,例如存储上次更新的时间,并在执行下一次更新之前检查是否已过一段时间。您能否详细说明“延迟问题”?是的,当同时发生关闭事件时,每个音符都会导致延迟,因此,如果我有4个音符打开然后关闭,循环将一个接一个地淡出所有打开的事件,阻止其他事件。什么是。FadeToBlacky(I)
确切地说是吗?您还提到了一个串行端口,但我在代码中没有看到它的引用。@潜伏者刚刚更新了代码,请随意看一看:)如果我没记错,Arduino的主程序在循环中运行。(我强烈认为这是有原因的。)因此,您可能需要创建一个更新函数来增加或减少LED的强度,而不是为LED淡入制作一个嵌套的a循环。该函数的变量必须获得LED的总使用寿命-也许,全局变量是有意义的(是的,尽管全局变量通常被认为是不好的样式)。此外,可能需要涉及延迟,例如存储上次更新的时间,并在执行下一次更新之前检查是否已过一段时间。您能否详细说明“延迟问题”?是的,当同时发生关闭事件时,每个音符都会导致延迟,因此,如果我有4个音符打开然后关闭,循环将一个接一个地淡出所有打开的事件,阻止其他事件。什么是。FadeToBlacky(I)
确切地说是吗?您还提到了一个串行端口,但我在您的代码中没有看到它的引用。@潜伏者刚刚更新了代码,请随意查看:)非常感谢,我会给您的