Reputation: 1
\
I'm working on code have that simulate Knight rider leds. I want to control Led via bluetooth as i can switch off. but i tried several things but doesn't work . any help. */I'm working on code have that simulate Knight rider leds. I want to control Led via bluetooth as i can switch off. but i tried several things but doesn't work . any help
code:
#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");
}
}
Upvotes: 0
Views: 184
Reputation: 823
To turn off all LEDs I would do something like:
/**
* Turn off all LEDs
*/
void allBlack()
{
for (uint16_t indexPixel = 0; indexPixel < N_LEDS; indexPixel++)
{
strip.SetPixelColor(indexPixel, 0x000000);
}
strip.show();
}
I know is maybe not the scope of this question, but I recommend to test this library:
https://github.com/Makuna/NeoPixelBus Since it has a lot of good examples and it would be in my opinion a lot better not to use delay but give a certain time like it's shown on Makuna examples to have more control over the animation. I also have another animation examples in this small project that is triggered by UDP commands.
Upvotes: 1