FastLED 和 IRremote 库不能协同工作

问题描述 投票:0回答:1

我在使用 Arduino Mega 以及 FastLED 和 IRremote 库时遇到问题。我尝试在 arduino 上使用带有红外传感器的旧电视遥控器,我可以获得每个按钮的代码读数,这很好。当我尝试使用红外遥控器控制 LED 灯串时,问题就出现了。日志只显示 0。也许库存在不连续性并且它们不能一起使用?这是我的代码:

void setup() {
    IrReceiver.begin(IR_RECEIVE_PIN);
    Serial.begin(9600);
    delay( 3000 ); // power-up safety delay
    FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
    FastLED.setBrightness(  BRIGHTNESS );
    
    currentPalette = RainbowColors_p;
    currentBlending = LINEARBLEND;

}


void loop() {
    static uint8_t startIndex = 0;
    startIndex = startIndex + 1; /* motion speed */
    
    FillLEDsFromPaletteColors( startIndex);

  if (IrReceiver.decode()) {
    IrReceiver.resume();
    uint16_t number = IrReceiver.decodedIRData.command;
    Serial.println(number);
    ChangePalette(number);
    
      
    FastLED.show();
    FastLED.delay(1000 / UPDATES_PER_SECOND);
  }
}

ChangePalette()
的工作方式是从远程获取代码并使用 if 语句来更改 LED 的模式:

void ChangePalette(uint16_t number) {
  // power - 2
  // power 2 - 224
  // 1 - 4
  // 2 - 5
  // 3 - 6
  // 4 - 8
  // 5 - 9
  // 6 - 10
  // 7 - 12
  // 8 - 13
  // 9 - 14
  // 0 - 17
  // vol+ - 7
  // vol- - 11
  // ch+ - 18
  // ch- - 16
  // up - 96
  // down - 97
  // left - 101
  // right - 98
  if( number == 1)  { currentPalette = RainbowColors_p;         currentBlending = LINEARBLEND; }
  if( number == 2)  { currentPalette = RainbowStripeColors_p;   currentBlending = NOBLEND;  }
  if( number == 3)  { currentPalette = RainbowStripeColors_p;   currentBlending = LINEARBLEND; }
  if( number == 4)  { currentPalette = CloudColors_p;           currentBlending = LINEARBLEND; }
  if( number == 5)  { currentPalette = PartyColors_p;           currentBlending = LINEARBLEND; }
  if( number == 6)  { currentPalette = ForestColors_p;          currentBlending = LINEARBLEND; }
  if( number == 7)  { currentPalette = LavaColors_p;            currentBlending = LINEARBLEND; }
  if( number == 8)  { currentPalette = OceanColors_p;           currentBlending = LINEARBLEND; }
}

当我注释掉这两行时:

FastLED.show();
FastLED.delay(1000 / UPDATES_PER_SECOND);

遥控器工作得很好,我可以在控制台中看到代码。

其他人以前也遇到过这个问题吗?

谢谢!!

arduino led fastled
1个回答
0
投票

您应该查看的一件事是 IRRemote 的文档,因为有一个部分专门针对 FastLED 和类似库的问题。这与使用的计时器冲突有关

https://github.com/Arduino-IRremote/Arduino-IRremote#problems-with-neopixels-fastled-etc

当您使用 Neopixels(又名 WS2811/WS2812/WS2812B)或其他长时间阻塞中断(> 50 µs)的库时,IRremote 将无法正常工作。

无论您使用 Adafruit Neopixel lib 还是 FastLED,许多低端 CPU(例如基本 Arduino)上的中断都会被禁用超过 50 µs。反过来,这会在需要时停止 IR 中断处理程序的运行。另请参阅此视频。

一种解决方法是等待 IR 接收器空闲,然后再使用 if (IrReceiver.isIdle()) { strip.show();} 发送 Neopixel 数据。 这至少可以防止中断正在运行的红外传输,并且 - 取决于 Neopixel 的更新速率 - 可能会很好地工作。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.