Arduino:如何重复任务直到收到新数据蓝牙

Arduino : How To repeat a task until new data received Bluetooth

当我从 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); 
      }

}

您应该使用程序的 状态 以便它可以继续测试按键,同时仍然使 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() 这里需要两秒钟,所以这是您可能需要等待的时间才能使按键生效。

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

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

#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脚本让它看起来更漂亮