【问题标题】:issue with Arduino using FastLed with IRremote change mode使用带有 IRremote 更改模式的 FastLed 的 Arduino 问题
【发布时间】:2021-07-07 19:07:07
【问题描述】:

您好,我正在尝试构建一个 arduino 项目来控制 LED 灯条并在按下不同按钮时更改光模式

我使用的代码是

#include <IRremote.h> //include the library
#include <FastLED.h>

// How many leds in your strip?
#define NUM_LEDS 70

#define DATA_PIN 3
#define CLOCK_PIN 13
#define Button_0 0xFF9867
#define Button_1 0xFFA25D
#define Button_2 0xFF629D
#define Button_3 0xFFE21D
#define Button_4 0xFF22DD
#define Button_5 0xFF02FD
#define Button_6 0xFFC23D
#define Button_7 0xFFE01F
#define Button_8 0xFFA857
#define Button_9 0xFF906F

int receiver = 7; //initialize pin 13 as recevier pin. 
IRrecv irrecv(receiver); //create a new instance of receiver
decode_results results;
CRGB leds[NUM_LEDS];
void setup() {
 Serial.begin(9600);
 LEDS.addLeds<WS2812,DATA_PIN,RGB>(leds,NUM_LEDS);
 LEDS.setBrightness(84);
 irrecv.enableIRIn(); //start the receiver
}

void loop() {
 if (irrecv.decode(&results)) { //if we have received an IR signal

 Serial.println (results.value, HEX); //display HEX results 
 irrecv.resume(); //next value
if (results.value == Button_0) {
    RunningLightSlow();
  }
else if(results.value == Button_1) {
    RainbowLights();
  
  }  
//  switch (results.value) {
//    
//    case Button_0: RunningLightSlow(); break;
//    case Button_1: RainbowLights(); break;
//    }


 }
 }

// LIGHTSHOW 0 starts --------------------------------------------------------------------------------------------------

 void fadeall() { for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250); } }

 void RunningLightSlow() {
  while(1) {
     static uint8_t hue = 0;
  Serial.print("x");
  // First slide the led in one direction
  for(int i = 0; i < NUM_LEDS; i++) {
    // Set the i'th led to red
    leds[i] = CHSV(hue++, 255, 255);
    // Show the leds
    FastLED.show();
    // now that we've shown the leds, reset the i'th led to black
    // leds[i] = CRGB::Black;
    fadeall();
    // Wait a little bit before we loop around and do it again
    delay(10);
  }
  Serial.print("x");

  // Now go in the other direction.
  for(int i = (NUM_LEDS)-1; i >= 0; i--) {
    // Set the i'th led to red
    leds[i] = CHSV(hue++, 255, 255);
    // Show the leds
    FastLED.show();
    // now that we've shown the leds, reset the i'th led to black
    // leds[i] = CRGB::Black;
    fadeall();
    // Wait a little bit before we loop around and do it again
    delay(10);
  }
    }
  }
// LIGHTSHOW 0 ends --------------------------------------------------------------------------------------------------------


//  LIGHTSHOW 1 STARTS ------------------------------------------------------------------------------------------------------

void RainbowLights() {
   randomSeed(millis());

  int wait=random(10,30);
  int dim=random(4,6);
  int max_cycles=8;
  int cycles=random(1,max_cycles+1);

while(1) {
    rainbowCycle(wait, cycles, dim);
  }

  }

  void rainbowCycle(int wait, int cycles, int dim)
{
  Serial.println("Let's make a rainbow.");
  //loop several times with same configurations and same delay
  for(int cycle=0; cycle < cycles; cycle++)
  {
    byte dir=random(0,2);
    int k=255;

    //loop through all colors in the wheel
    for (int j=0; j < 256; j++,k--)
    {
      
      if(k<0)
      {
        k=255;
      }
      
      //Set RGB color of each LED
      for(int i=0; i<NUM_LEDS; i++)
      {
        CRGB ledColor = wheel(((i * 256 / NUM_LEDS) + (dir==0?j:k)) % 256,dim);        
        leds[i]=ledColor;
      }

      FastLED.show();
      FastLED.delay(wait);
    }
  }
}

CRGB wheel(int WheelPos, int dim)
{
  CRGB color;
  if (85 > WheelPos)
  {
   color.r=0;
   color.g=WheelPos * 3/dim;
   color.b=(255 - WheelPos * 3)/dim;;
  } 
  else if (170 > WheelPos)
  {
   color.r=WheelPos * 3/dim;
   color.g=(255 - WheelPos * 3)/dim;
   color.b=0;
  }
  else
  {
   color.r=(255 - WheelPos * 3)/dim;
   color.g=0;
   color.b=WheelPos * 3/dim;
  }
  return color;
}

现在我面临的问题是,当我按下一个按钮(例如 0)时,它会运行函数 RunningLightSlow();我已经编写了无限循环代码,因为我想要无限光显示,当我按下按钮 1 时它不会改变光模式,

根本原因是一旦函数进入无限循环,它就永远不会接收到 ir 信号,因此永远不会改变光模式

【问题讨论】:

    标签: arduino arduino-uno fastled


    【解决方案1】:

    从您的程序中删除所有无限循环,并在 loop() 中创建一个简单的结构,您可以在其中检查 IR 信号并在按下最后一个按钮时更新“状态”变量。

    void loop() {
    
      // create a function or add here whatever is needed to read the IR code
      irCode = readIrCode();
    
      // check if the new code is identical to a previous code
      if (lastIrCode != irCode) {
    
        // memorize the current code
        lastIrCode = irCode;
    
        / act according to user selection
        switch (irCode) {
          case Button_0:
            doThis(); // this could be your RunningLightSlow()
            break;
          case Button_1:
            doThat(); // maybe RainbowLights()
            break;
          default:
            // for cases you have not handled above
            doWhatever();
        }
      }
    }
    

    不要忘记从运行灯的函数中删除无限循环,显然您需要声明变量并设置正确的调用。

    【讨论】:

    • 如果我从 RunningLightSlow() 函数中删除无限循环,那么我的 LED 灯带将在完成一次迭代后停止,我想继续调用 RunningLightSlow() 直到我按下另一个按钮
    • 查看答案,它让您知道您的函数将被一次又一次地调用,直到读取新的 IR 代码。
    • 你能分享一个示例代码吗?无法建立你所说的
    • 添加到答案的代码示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多