LED Programmierbar Lauflicht

@spes91
hätte da einen Vorschlag.
Das "Lauflicht" umgebaut nach "BlinkWithoutDelay",
Die Kombination Pixel und Buttons ergeben ein eigenes Objekt.
Jedes Objekt kümmert sich selber um das ganze Time-Management.

nur mal als Beispiel mit 4 Stripes:

// https://forum.arduino.cc/t/led-programmierbar-lauflicht/917480
// by noiasca
// 2021-10-23

#include <Adafruit_NeoPixel.h>

class Strip {
    uint8_t actual = 0;                // for strips up to 255 pixels
    uint16_t interval = 500;           // speed of running light
    uint16_t runningSeconds = 30;      // running time when startetd
    uint32_t onColor = pixels.Color(0, 150, 0);  // color for pixels which are on
    uint32_t offColor = 0x00;          // color for pixels which are off
    uint32_t previousMillis = 0;       // time management for movement
    uint32_t startMillis = 0;          // time management for total running time
    bool isRunning = false;            // flag if running light is activ
    const byte numPixel;               // how many pixels has this strip
    const byte pixelPin;               // on which GPIO are the pixels connected
    const byte buttonPin;              // which button operates this pixels
    Adafruit_NeoPixel pixels;          // the pixels hardware (using the Adafruit library)

  public:
    Strip (byte numPixel, byte pixelPin, byte buttonPin) :
      numPixel (numPixel),
      pixelPin (pixelPin),
      buttonPin (buttonPin),
      pixels(numPixel, pixelPin, NEO_GRB + NEO_KHZ800)
    {}

    void begin()
    {
      pixels.begin();
      pixels.clear();
      pinMode(buttonPin, INPUT_PULLUP);
    }

    void start()
    {
      if (isRunning == false)
      {
        actual = 0;
        isRunning = true;
        startMillis = millis();
        update();
      }
    }

    void update()
    {
      uint32_t currentMillis = millis();
      // movements
      if (isRunning && (currentMillis - previousMillis > interval))
      {
        previousMillis = currentMillis;
        pixels.setPixelColor(actual, offColor);
        actual++;
        if (actual >= numPixel)
        {
          actual = 0;
        }
        pixels.setPixelColor(actual, onColor);
      }
      // check if time has passed - switch off
      if (isRunning && (currentMillis - startMillis > runningSeconds * 1000UL))
      {
        isRunning = false;
        pixels.setPixelColor(actual, offColor);
      }
      pixels.show();   // Send the updated pixel colors to the hardware.

      //check the button - switch on
      if (!isRunning && digitalRead(buttonPin) == LOW)
      {
        start();
      }
    }
};

// numPixels, pixelPin, buttonPin
Strip strip[] {
  {16, 12, A0},
  {16, 11, A1},
  {16, 10, A2},
  {16,  9, A3}
};

void setup() {
  Serial.begin(115200);
  for (auto &i : strip)
  {
    i.begin();
  }
  strip[0].start();          // start once on startup as "on test"
}

void loop() {
  for (auto &i : strip)
  {
    i.update();             // check if change is necessary & read button
  }
}

kann man nun natürlich noch erweitern und setter machen um unterschiedliche Farben, Geschwindigkeiten etc. zu definieren, aber ich wollte das Beispiel auf 100 Zeilen begrenzen.

2 Likes