Arduino und neopixel seriell wehrte senden

Hallo,

Bin neu in Sachen programmieren und Arduino und wollte nun mein LED Strip über die serielle schnittstelle wehrte zu zuweisen dass sollte ungefähr so aussehen: Rot, Green, Blau, Helligkeit
so in etwa sollte es beim arduino ankommen:
R=255, G=0, B=0, BR=150

hier ist mal der code denn ich so schon habe:

#include <Adafruit_NeoPixel.h>
#define PIN 3
#define NUMPIXELS 10
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);



void setup() {
  // initialize serial:
  Serial.begin(9600);
  pixels.begin();
  pixels.show();

}

void loop() {

  while (Serial.available() > 0) {


    int red = Serial.parseInt();

    int green = Serial.parseInt();

    int blue = Serial.parseInt();
    
    int BR = Serial.parseInt();


    if (Serial.read()) {

      red = 255 - constrain(red, 0, 255);
      green = 255 - constrain(green, 0, 255);
      blue = 255 - constrain(blue, 0, 255);
      BR = 255 - constrain(BR, 0, 255);

      pixels.setPixelColor(9, pixels.Color(red,green,blue));
      pixels.setBrightness(BR);
      pixels.show();



    }
  }
}

Danke schon mal im vorraus

Also es kommen ja wehrte an wenn ich z.B. 255,0,0,150 sende aber denn leuchtet die test LED blau und nicht rot

Und, was ist deine Frage?

Tipp:
Denke mal über CSV nach!
Und strtok()

na wenn ich jetzt z.B. 255,0,0,0 im Seriellen Monitor eingebe denn leuchtet die led blau wenn ich 0,255,0,0 eingebe leuchtet sie lila und wenn ich 0,0,255,0 eingebe leuchtet sie gelb und das mit der Helligkeit funktioniert garnicht

ich würde mal das "255-" weglassen wenn du die Farben wie übertragen erwartest, vielleicht auch mal RGB, GRB checken.

Mit deiner Einlesetechnik kommst du nicht weit.

Hier ein Beispiel das 4 durch Kommata getrennte Integers in einer Zeile einliest
(aus Bequemlichkeit mit sscanf statt strtok/atoi)

#include <Adafruit_NeoPixel.h>
#define PIN 3
#define NUMPIXELS 10
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  Serial.begin(250000);
  pixels.begin();
  pixels.show();
}

void loop() {
  serialHandler();
}

void processCmd(const char* buf)
{
  int red, green, blue, BR;
  if (sscanf(buf, "%d,%d,%d,%d", &red, &green, &blue, &BR) == 4) {
    red = constrain(red, 0, 255);
    green = constrain(green, 0, 255);
    blue = constrain(blue, 0, 255);
    BR =  constrain(BR, 0, 255);
    pixels.setPixelColor(9, pixels.Color(red, green, blue));
    pixels.setBrightness(BR);
    pixels.show();
  }
}

void serialHandler()
{
  const byte sCBMax = 30;
  static char sCBuffer[sCBMax];
  static byte buffIndex = 0;

  byte inChar;
  bool doCheck = false;

  while (Serial.available()) {
    inChar = Serial.read();
    if (inChar == 13) {
      doCheck = true;
    } else if (inChar != 10) {
      if ((buffIndex == 0) && isWhitespace(inChar)) {
        continue;
      }
      sCBuffer[buffIndex++] = inChar;
      doCheck = (buffIndex == (sCBMax - 1));
    }
    if (doCheck) {
      sCBuffer[buffIndex] = 0;
      doCheck = false;
      if (buffIndex != 0) {
        processCmd(sCBuffer);
        buffIndex = 0;
      } else {
        Serial.println();
      }
    }
  }
}

Eine Variante mit strtok und strtol (um Hex zu erlauben).
Einbuchstabige Kommandos für Farbe, Position und Helligkeit, Farbe darf zusätzlich noch eine Anzahl enthalten.

#include <Adafruit_NeoPixel.h>
#define PIN 3
#define NUMPIXELS 10
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  Serial.begin(250000);
  Serial.print(F("c rot, gruen, blau[, Anzahl]\nb Helligkeit\np Led\n"));
  pixels.begin();
  pixels.show();
}

void loop() {
  serialHandler();
}

void processCmd(const char* buf)
{
  static byte which = 0;
  int red, green, blue, num;
  char* erg;
  switch (*buf) {
    case 'c':
      erg = strtok((char*)buf + 1, ",");
      if (erg) {
        red = constrain(strtol(erg, NULL, 0), 0, 255);
        erg = strtok(NULL, ",");
        if (erg) {
          green = constrain(strtol(erg, NULL, 0), 0, 255);
          erg = strtok(NULL, ",");
          if (erg) {
            blue = constrain(strtol(erg, NULL, 0), 0, 255);
            erg = strtok(NULL, ",");
            if (erg) {
              num = constrain(strtol(erg, NULL, 0), 1, NUMPIXELS);
              while (num--) {
                pixels.setPixelColor(which + num, pixels.Color(red, green, blue));
              }
            } else {
              pixels.setPixelColor(which, pixels.Color(red, green, blue));
            }
            pixels.show();
          }
        }
      }
      break;
    case 'b':
      pixels.setBrightness(constrain(strtol(buf + 1, NULL, 0), 0, 255));
      pixels.show();
      break;
    case 'p':
      which = constrain(strtol(buf + 1, NULL, 0), 0, NUMPIXELS - 1);
      break;
  }
}

void serialHandler()
{
  const byte sCBMax = 30;
  static char sCBuffer[sCBMax];
  static byte buffIndex = 0;

  byte inChar;
  bool doCheck = false;

  while (Serial.available()) {
    inChar = Serial.read();
    if (inChar == 13) {
      doCheck = true;
    } else if (inChar != 10) {
      if ((buffIndex == 0) && isWhitespace(inChar)) {
        continue;
      }
      sCBuffer[buffIndex++] = inChar;
      doCheck = (buffIndex == (sCBMax - 1));
    }
    if (doCheck) {
      sCBuffer[buffIndex] = 0;
      doCheck = false;
      if (buffIndex != 0) {
        processCmd(sCBuffer);
        buffIndex = 0;
       }
    }
  }
}
c rot, gruen, blau[, Anzahl]
b Helligkeit
p Led