#include <Adafruit_NeoPixel.h>

#define PIN 11           // Pin where NeoPixel data is connected
int NUMPIXELS = 16;      // Default number of NeoPixels (this will be updated after user input)

Adafruit_NeoPixel strip(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  Serial.begin(9600); // Start serial communication
  Serial.println("Enter the number of NeoPixels:");
  while (Serial.available() == 0) {
    // Wait for the user to input a number
  }

  // Read the number of pixels
  NUMPIXELS = Serial.parseInt();
  strip.updateLength(NUMPIXELS);  // Update NeoPixel strip length
  strip.begin();      // Initialize the NeoPixel strip
  strip.show();       // Initialize all pixels to 'off'
  
  Serial.println("Chose a color (like 128,255,0):");
}

void loop() {
  if (Serial.available() > 0) {
    // Read the RGB values from the serial input
    String colorString = Serial.readStringUntil('\n');
    int r, g, b;
    
    // Parse the RGB values (assumes format like "12, 233, 95")
    int firstComma = colorString.indexOf(',');
    int secondComma = colorString.indexOf(',', firstComma + 1);
    
    if (firstComma != -1 && secondComma != -1) {
      r = colorString.substring(0, firstComma).toInt();
      g = colorString.substring(firstComma + 1, secondComma).toInt();
      b = colorString.substring(secondComma + 1).toInt();

      // Set all NeoPixels to the received color
      for (int i = 0; i < NUMPIXELS; i++) {
        strip.setPixelColor(i, r, g, b);
      }
      strip.show();  // Update the NeoPixels with the new color
      Serial.print("Showing (");
      Serial.print(r);
      Serial.print(", ");
      Serial.print(g);
      Serial.print(", ");
      Serial.print(b);
      Serial.println(")");
      Serial.println("Enter a new color:");
    }
  }
}