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