-1

当我从 BT 接收数据时,我试图循环点亮和熄灭 LED。但是当我通过 BT 发送“r”时,LED 灯条会打开和关闭并循环执行,但是当我发送“b”或“g”或“o”时,它不会改变或关闭,但会首先循环功能。

我需要一种方法来停止“for int”并更改为其他功能。

在我通过 BT 更改之前,所有功能都需要始终处于循环状态。

希望你能理解。

BT=蓝牙


#include "FastLED.h"                                          // FastLED library.



// Fixed definitions cannot change on the fly.
#define LED_DT 7                                             // Data pin to connect to the strip.
#define COLOR_ORDER GRB                                       // It's GRB for WS2812 and BGR for APA102.
#define LED_TYPE WS2812B                                       // Using APA102, WS2812, WS2801. Don't forget to change LEDS.addLeds.
#define NUM_LEDS 23                                           // Number of LED's.

struct CRGB leds[NUM_LEDS];                                   // Initialize our LED array.
int val;



void setup() {

  Serial.begin(9600);                                       // Initialize serial port for debugging.
  delay(1000);                                                // Soft startup to ease the flow of electrons.


  LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);  

} 

void loop () {                                                
  if (Serial.available())
  {
   val = Serial.read();
  
       if (val == 'r') // red
       {
         red();
       }
      
       if (val == 'g') // green
       {
        green();   
       }
        
        if (val == 'b') // blue
       {
         blue(); 
       }
        if (val == 'o') //off
       {
        FastLED.clear ();
       }
  }

}
void red() 
{
        
        fill_solid(leds, NUM_LEDS, CRGB::Red);
        FastLED.show();       // Power managed display
        FastLED.delay(1000);
        FastLED.clear ();
        FastLED.delay(1000); 

}

void green() 
{
        for(int i = 0; i < 1000; i++)
          {
        fill_solid(leds, NUM_LEDS, CRGB::Green);
        FastLED.show();       // Power managed display
        FastLED.delay(1000);
        FastLED.clear ();
        FastLED.delay(1000); 
      }

}


void blue() 
{
        for(int i = 0; i < 1000; i++)
          {
        fill_solid(leds, NUM_LEDS, CRGB::Blue);
        FastLED.show();       // Power managed display
        FastLED.delay(1000);
        FastLED.clear ();
        FastLED.delay(1000); 
      }

}
4

2 回答 2

0

您应该使用程序的状态,以便它可以继续测试按键,同时仍然闪烁 LED。此外,不要忘记show()更新的 LED,也在clear(). 例如

#define MAXBLINKS 1000
int blinkCount = 0;
int lastKey = 0;

void loop () {                                                

    if (Serial.available()) {
        // lastKey is kept because it is global
        // and not overwritten until a new key is pressed
        lastKey = Serial.read();
    }

    if (lastKey != 0) {

        switch (lastKey) {
        case 'r':
            blink(CRGB::Red);
            break;
        case 'g':
            blink(CRGB:Green);
            break;
        case 'b':
            blink(CRGB::Blue);
            break;
        }

        blinkCount++;

        // Stop blinking after MAXBLINKS,
        // or immediately for certain keys
        if (blinkCount > MAXBLINKS || lastKey == 'r') {
            blinkCount = 0;
            lastKey = 0;
        }

    }
}

// blink will take TWO seconds before it returns
// -- not sure CRGB is the right type
void blink(CRGB clr)
{
    fill_solid(leds, NUM_LEDS, clr);
    FastLED.show();
    FastLED.delay(1000);
    FastLED.clear();
    FastLED.show();
    FastLED.delay(1000); 
}
       

因为blink()这里需要两秒钟,这是您在按键生效之前可能需要等待的时间。

于 2021-11-18T10:00:39.237 回答
0

/////////////////

所以我想通了,我只是将监听器添加到每个函数中。

#include "FastLED.h"                                          // FastLED library.



// Fixed definitions cannot change on the fly.
#define LED_DT 7                                             // Data pin to connect to the strip.
#define COLOR_ORDER GRB                                       // It's GRB for WS2812 and BGR for APA102.
#define LED_TYPE WS2812B                                       // Using APA102, WS2812, WS2801. Don't forget to change LEDS.addLeds.
#define NUM_LEDS 46                                           // Number of LED's.

struct CRGB leds[NUM_LEDS];                                   // Initialize our LED array.
int val;


/////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {

  Serial.begin(9600);                                       // Initialize serial port for debugging.
  delay(1000);                                                // Soft startup to ease the flow of electrons.


  LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);  

} 
/////////////////////////////////////////////////////////////////////////////////////////////////
void loop () {                                                
  if (Serial.available())
  {
   val = Serial.read();
  
       if (val == 'r') // red
       {
         red();
       }
      
       if (val == 'g') // green
       {
        green();   
       }
        
        if (val == 'b') // blue
       {
         blue(); 
       }

       if (val == 't') // blue 3x
       {
         blue3x(); 
       }

       if (val == 'w') // white
       {
         white(); 
       }

       if (val == 'p') // rainbow
       {
         rainbow(); 
       }
  }

}

/////////////////////////////////////////////////////////////////////////////////////////////////

void red() 
{
        for(int i = 0; i < 1000; i++)
          {
        fill_solid(leds,23, CRGB::Red);
        FastLED.show();       // Power managed display
        FastLED.delay(100);
        FastLED.clear ();
        fill_solid(leds+23,23, CRGB::Red);
        FastLED.show();
        FastLED.delay(100);
        FastLED.clear ();
        FastLED.delay(100); 
        if (Serial.available())
            {
              val = Serial.read();
              if (val == 'C') // Cancel
                {
                  break;
               }
          }
      }
}

/////////////////////////////////////////////////////////////////////////////////////////////////

void white() 
{
        for(int i = 0; i < 1000; i++)
          {
        fill_solid(leds,23, CRGB::White);
        FastLED.show();       // Power managed display
        FastLED.delay(100);
        FastLED.clear ();
        fill_solid(leds+23,23, CRGB::White);
        FastLED.show();
        FastLED.delay(100);
        FastLED.clear ();
        FastLED.delay(100); 
        if (Serial.available())
            {
              val = Serial.read();
              if (val == 'C') // Cancel
                {
                  break;
               }
          }
      }
}

/////////////////////////////////////////////////////////////////////////////////////////////////

void green() 
{
        for(int i = 0; i < 1000; i++)
          {
        fill_solid(leds,23, CRGB::Green);
        FastLED.show();       // Power managed display
        FastLED.delay(100);
        FastLED.clear ();
        fill_solid(leds+23,23, CRGB::Green);
        FastLED.show();
        FastLED.delay(100);
        FastLED.clear ();
        FastLED.delay(100); 
        if (Serial.available())
            {
              val = Serial.read();
              if (val == 'C') // Cancel
                {
                  break;
               }
          }
      }
}

/////////////////////////////////////////////////////////////////////////////////////////////////

void blue() 
{
        for(int i = 0; i < 1000; i++)
          {
        fill_solid(leds,23, CRGB::Blue);
        FastLED.show();       // Power managed display
        FastLED.delay(100);
        FastLED.clear ();
        fill_solid(leds+23,23, CRGB::Blue);
        FastLED.show();
        FastLED.delay(100);
        FastLED.clear ();
        FastLED.delay(100); 
        if (Serial.available())
            {
              val = Serial.read();
              if (val == 'C') // Cancel
                {
                  break;
               }
          }
      }
}

/////////////////////////////////////////////////////////////////////////////////////////////////

void blue3x() 
{
        for(int i = 0; i < 1000; i++)
          {
        fill_solid(leds,23, CRGB::Blue);
        FastLED.show();       // Power managed display
        FastLED.delay(75);
        FastLED.clear ();
        FastLED.delay(75);
        fill_solid(leds,23, CRGB::Blue);
        FastLED.show();       // Power managed display
        FastLED.delay(75);
        FastLED.clear ();
        FastLED.delay(75);
        fill_solid(leds,23, CRGB::Blue);
        FastLED.show();       // Power managed display
        FastLED.delay(75);
        FastLED.clear ();
        FastLED.delay(75);
        
        fill_solid(leds+23,23, CRGB::Blue);
        FastLED.show();
        FastLED.delay(75);
        FastLED.clear ();
        FastLED.delay(75);
        fill_solid(leds+23,23, CRGB::Blue);
        FastLED.show();
        FastLED.delay(75);
        FastLED.clear ();
        FastLED.delay(75);
        fill_solid(leds+23,23, CRGB::Blue);
        FastLED.show();
        FastLED.delay(75);
        FastLED.clear ();
        FastLED.delay(75); 
        
        if (Serial.available())
            {
              val = Serial.read();
              if (val == 'C') // Cancel
                {
                  break;
               }
          }
      }
}

/////////////////////////////////////////////////////////////////////////////////////////////////

void rainbow()
{
        for(int i = 0; i < 1000; i++)
          {
        fill_rainbow(leds, NUM_LEDS, thishue, deltahue);
        if (Serial.available())
            {
              val = Serial.read();
              if (val == 'C') // Cancel
                {
                  break;
               }
          }
      }
}

我确信它可以真正优化,但我无法优化它。

我会尝试使用@mmixLinus脚本让它看起来更漂亮。

于 2021-11-18T17:42:11.853 回答