du kannst den Strip in so viele logische Abschnitte teilen wie du magst.
hier ein Beispiel mit einzelnen Blinkern, Lichtern und einem Lauflicht.
Einfach eine Klasse machen, die eine Referenz auf dein Strip Objekt nimmt:
/*
Use pixels in a strip as separate objects
by noiasca
2020-03-02 https://forum.arduino.cc/index.php?topic=668289.15
https://forum.arduino.cc/index.php?topic=668125
2020-12-31 https://forum.arduino.cc/index.php?topic=720583.0
*/
const byte ledPin = 2; // Which pin on the Arduino is connected to the NeoPixels?
const uint16_t ledCount = 8;
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel strip(ledCount, ledPin, NEO_GRB + NEO_KHZ800);
class BlinkLed {
private:
byte state = 1; // 0 off, 1 blink high , 2 blink low
unsigned long previousMillis;
uint16_t on = 180;
uint16_t off = 320; // 180/320 is according ECE
uint32_t colorOn = 0xAA0000;
uint32_t colorOff = 0x000000;
Adafruit_NeoPixel& obj;
const byte id;
public:
BlinkLed(Adafruit_NeoPixel& obj, byte id):
obj(obj),
id(id)
{}
void set(uint16_t _on, uint16_t _off) // modify on/off times during runtime
{
on = _on;
off = _off;
}
void setState(byte _state) // 0 switch off blinking; 1 Switch on blinking;
{
state = _state;
}
void setColor(uint32_t newColorOn, uint32_t newColorOff)
{
colorOn = newColorOn;
colorOff = newColorOff;
}
void tick()
{
if (state)
{
uint32_t currentMillis = millis();
if (state == 1 && currentMillis - previousMillis >= on) {
// save the last time you blinked the LED
obj.setPixelColor(id, colorOn);
obj.show();
state = 2;
previousMillis = currentMillis;
}
if (state == 2 && currentMillis - previousMillis >= off) {
// save the last time you blinked the LED
obj.setPixelColor(id, colorOff);
obj.show();
state = 1;
previousMillis = currentMillis;
}
}
}
};
class Scanner {
private:
byte state = 1; // 0 off, 1 blink high , 2 blink low
unsigned long previousMillis;
uint16_t on = 200;
uint32_t colorOn = 0xAA0000;
uint32_t colorOff = 0x000000;
int8_t direction = 1; // direction
Adafruit_NeoPixel& obj;
const byte id; // first pixel of this group
const byte leds; // how many leds
public:
Scanner(Adafruit_NeoPixel& obj, byte id, byte leds):
obj(obj),
id(id),
leds(leds)
{}
void set(uint16_t _on) // modify on/off times during runtime
{
on = _on;
}
void setState(byte _state) // 0 switch off blinking; 1 Switch on blinking;
{
state = _state;
}
void setColor(uint32_t newColorOn, uint32_t newColorOff)
{
colorOn = newColorOn;
colorOff = newColorOff;
}
void tick()
{
if (state)
{
uint32_t currentMillis = millis();
if (currentMillis - previousMillis >= on) {
// save the last time you blinked the LED
Serial.println(state);
obj.setPixelColor(id + state - 1, colorOff);
state = state + direction;
if (state > leds) {
state = leds - 1;
direction = -1;
}
else if (state < 1)
{
state = 2;
direction = 1;
}
obj.setPixelColor(id + state - 1, colorOn);
obj.show();
previousMillis = currentMillis;
}
}
}
};
BlinkLed turnsignalLeft(strip, 0); // name of strip object, pixelId in strip
BlinkLed blinkLedFront(strip, 1); // name of strip object, pixelId in strip
BlinkLed blinkLedRear(strip, 6); // name of strip object, pixelId in strip
BlinkLed turnsignalRight(strip, 7); // name of strip object, pixelId in strip
Scanner scanner(strip, 2, 4); // name of strip object, first pixelId, number of pixels for this group
void setup() {
Serial.begin(115200);
Serial.println(F("\nStart"));
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show(); // Turn OFF all pixels ASAP
strip.setBrightness(127); // Set BRIGHTNESS to about 1/5 (max = 255)
blinkLedFront.setColor(0x0000AA, 0x555533); // blink blue/white
blinkLedRear.setColor(0x0000AA, 0xFF0000); // blink blue/red
blinkLedRear.set(400, 200);
turnsignalRight.setColor(0xFF8000, 0x201000);
turnsignalRight.set(666, 666); // 1,5Hz +/-0,5Hz 30 % to 80 %
turnsignalLeft.setColor(0xFF8000, 0x201000);
turnsignalLeft.set(666, 666); // 1,5Hz +/-0,5Hz 30 % to 80 %
turnsignalLeft.setState(0); // switch off
}
void loop() {
blinkLedFront.tick();
blinkLedRear.tick();
scanner.tick();
turnsignalRight.tick();
turnsignalLeft.tick(); // is turned off, therefore not shown
}
Ausprobieren, code studieren!