如果出现状况,如何关闭LED灯条

时间:2019-06-16 17:07:32

标签: led esp32

\

我正在研究具有模拟Knight骑士led的代码。我想通过蓝牙控制Led,因为我可以关闭。 但是我尝试了几件事但没用。 任何帮助。 * /我正在编写具有模拟Knight骑士指示灯的代码。我想通过蓝牙控制Led,因为我可以关闭。 但是我尝试了几件事但没用。 任何帮助

代码:

#include "BluetoothSerial.h" 
#include <Adafruit_NeoPixel.h>
#define N_LEDS 8
#define PIN    23
Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_LEDS, PIN, NEO_GRB + 
NEO_KHZ800);

BluetoothSerial ESP_BT; 
int incoming;
int r;
int pos = 0, dir = 1; 

void setup() {
    strip.begin();
    Serial.begin(9600); 
    ESP_BT.begin("ESP32_LED_Control"); 
    Serial.println("Bluetooth Device is Ready to Pair");
}

void loop() {

    strip.setPixelColor(pos - 2, 0x100000); // Dark red
    strip.setPixelColor(pos - 1, 0x800000); // Medium red
    strip.setPixelColor(pos    , 0xFF3000); // Center pixel is brightest
    strip.setPixelColor(pos + 1, 0x800000); // Medium red
    strip.setPixelColor(pos + 2, 0x100000); // Dark red
    strip.show();
    delay(85);   // control speed

    for (r=-2; r<= 2; r++) strip.setPixelColor(pos+r, 0);

    pos += dir;
    if (pos < 0) {
        pos = 1;
        dir = -dir;
    } else if (pos >= strip.numPixels()) {
        pos = strip.numPixels() - 2;
        dir = -dir;
    }

    if (ESP_BT.available()) //Check if we receive anything from Bluetooth
    {
        incoming = ESP_BT.read(); //Read what we recevive 
        Serial.print("Received:"); Serial.println(incoming);
    }  

    if (incoming == 48)
    {
        //Should put here code that works on stop strip led//
        ESP_BT.println("LED turned OFF");
    }
}

1 个答案:

答案 0 :(得分:1)

要关闭所有LED,我将执行以下操作:

/**
* Turn off all LEDs
*/
void allBlack()
{
  for (uint16_t indexPixel = 0; indexPixel < N_LEDS; indexPixel++)
  {
     strip.SetPixelColor(indexPixel, 0x000000);
  }
  strip.show();
}

我知道这可能不在此问题的范围内,但我建议测试此库:

https://github.com/Makuna/NeoPixelBus 由于它有很多好的示例,我认为最好不要使用延迟,而是像Makuna示例中所示的那样留出一定的时间来更好地控制动画。 small project that is triggered by UDP commands中还有另一个动画示例。