# include <Adafruit_NeoPixel.h>
# define PIN 6
# define NLEDS  20

// stay on five seconds:
# define KMINUTE  10     // age every KMINUTE ms
# define PERIODS  500      // KMINUTEs to stay on

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NLEDS , PIN, NEO_GRB + NEO_KHZ800);

unsigned long ontime[NLEDS];
unsigned long elapsedtime;

// Button, debounce
const int buttonPin = 9;
int buttonState = HIGH;
const byte DEBOUNCE = 50;

// Colors
const uint32_t color1 = strip.Color(0, 0, 255);   //Blau
const uint32_t color2 = strip.Color(255, 0, 0);   //ROT
const uint32_t color3 = strip.Color(0, 255, 0);   // Grün
const uint32_t color4 = strip.Color(255, 168, 0); //ORANGE
const uint32_t color5 = strip.Color(255, 255, 0); // yellow
const uint32_t black = strip.Color(0, 0, 0);      // Ausschalten / black

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(LED_BUILTIN, OUTPUT);

  strip.begin();   //initialisieren
  strip.setBrightness(200);   //Golbale Helligkeit

  for (byte theLED = 0; theLED < NLEDS; theLED++)
    strip.setPixelColor(theLED, color2) ;  // set all LEDs red

  strip.show();  // Alles aus
}

void loop()
{
  unsigned long now = millis();
  unsigned char changed = false;    // has any pixel changed? not yet.

	static unsigned long lastAged = 0;
  static byte nOn = 0;

  static unsigned long lastDebounceTime = 0;
  static byte nextLED = 0;   // next LED to illuminute

// loop beating heart
  static unsigned char tog;
  digitalWrite(LED_BUILTIN, ++tog & 1);

// Buttonpress. maybe add an LED

  if (now - lastDebounceTime > DEBOUNCE) {
    int reading = digitalRead(buttonPin);
    if (reading != buttonState) {
      lastDebounceTime = now;
      buttonState = reading;

      if (buttonState == LOW) {
        ontime[nextLED] = PERIODS;		// now in KMINUTE ms steps
        if (nextLED > NLEDS) nextLED = 0;
        strip.setPixelColor(nextLED, color3);
        changed = true;
        nOn++;

        ++nextLED; if (nextLED == NLEDS) nextLED = 0;
      }
    }
  }

// age all the LED timers every KMINUTE ms
	if (now - lastAged > KMINUTE) {
	  lastAged = now;
	  for (byte theLED = 0; theLED < NLEDS; theLED++) {

      if (ontime[theLED] && ontime[theLED] < 100) {
        strip.setPixelColor(theLED, color5);
        changed = true;
      }

	    if (ontime[theLED] && !--ontime[theLED]) {
	      strip.setPixelColor(theLED, color2);  // LED goes red
	      changed = true;  // any change means we have to strip.show
        if (nOn && !--nOn) nextLED = 0;
	    }
    }
	}

  if (changed) // if anything did change the buffer we should
    strip.show();
}

/*
  Serial.begin(115200);
  Serial.println("task timer world!\n");


static unsigned int counta = 0;
Serial.print(counta++);
Serial.println(" see the button");
*/