Ws2812 mit Taster Ändern?

Hallo zusammen.

ich bin kompletter Anfänger in Sachen Arduino.
Spiele schon ne weile mit Arduino und Neos herum..
Habe mir diesen code zusammengebastelt aus Ankleitungen im netz.
Ich möchte mir aktuell eine Lampe bauen die per Taster die Farbe wechselt. Soweit funktioniert alles nur bekomme ich es nicht hin dass die Lampe anfangs mit einer Farbe leuchtet. Erst beim drücken des Tasters kommt die erste Farbe.
Ist es möglich dass es sobald Strom da ist die Leds leuchten und mit Taster dann in andre Farbe geändert werden kann.
Ich möchte sagen wir 8 Farben haben die ich durchschalten kann sobald die 8. erreicht ist soll wieder die 1. leuchten.
Ich hoffe ihr versteht wie ich das gern umsetzen würde.

#include <Adafruit_NeoPixel.h>

#define BUTTON_PIN   2    // Digital IO pin connected to the button.  This will be
                          // driven with a pull-up resistor so the switch should
                          // pull the pin to ground momentarily.  On a high -> low
                          // transition the button press logic will execute.

#define PIXEL_PIN    9   // Digital IO pin connected to the NeoPixels.

#define PIXEL_COUNT 4   // number of neopixel (change this accordingly)

// Parameter 1 = number of pixels in strip,  neopixel stick has 8
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_RGB     Pixels are wired for RGB bitstream
//   NEO_GRB     Pixels are wired for GRB bitstream, correct for neopixel stick
//   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
//   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

bool oldState = HIGH;
int showType = 0;

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  
  // Get current button state.
  bool newState = digitalRead(BUTTON_PIN);

  // Check if state changed from high to low (button press).
  if (newState == LOW && oldState == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState = digitalRead(BUTTON_PIN);
    if (newState == LOW) {
      showType++;
      if (showType > 9)
        showType=0;
      startShow(showType);
    }
  }

  // Set the last button state to the old state.
  oldState = newState;
}

void startShow(int i) {
  switch(i){
   
    case 0: colorWipe(strip.Color(255, 200, 0), 10);  //
            break;
    case 1: colorWipe(strip.Color(255, 0, 0), 10);  // Red
            break;
    case 2: colorWipe(strip.Color(0, 255, 0), 10);  // Green
            break;
    case 3: colorWipe(strip.Color(0, 0, 255), 10);  // Blue
            break;
    case 4: colorWipe(strip.Color(32, 253, 24), 10); // Hellgrn
            break;
    case 5: colorWipe(strip.Color(213,0,255), 10); // LILA
            break;
    case 6: colorWipe(strip.Color(  225,127, 16), 10); // gelb
            break;
    
  }
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}



// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

Fügen Sie alles, was Sie einmal am Anfang tun möchten, in die Funktion setup() ein.

Und wenn Sie hier in einem englischsprachigen Forum weitermachen, haben Sie die Freundlichkeit, die googke-Übersetzung einmal für uns zu erledigen, anstatt von uns allen zu erwarten, dass sie es alle tun...

a7

Hallo,
füge im setup() einen Funktionsaufruf startShow(int i), mit der gewünschten Farbe zum Start, ein.

1 Like

I should take my own advice, google helped me shpreken:

Add whatever you want to do at the beginning in the setup () function.

And if you keep going here in an English language forum you have the kindness of doing the googke translation for us once instead of expecting all of us to do it all...

Better than I expected, not bad.

Anything you want to do once can be put in the setup() function.

Like setting the initial colours of the LED strip.

a7

Sorry aber bin glaub zu blöd.. oder steh grad voll aufm Schlauch.

Haha, nice.

a7

Im Setup:

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

Folgende Zeile einfügen:

startShow(showType);

Danke habe es grade versucht aber immer noch keine Fareb am anfang nur nach Tastendruck :. was Mache ich falsch :exploding_head:

Dann zeige doch bitte mal deinen geänderten Code.

#include <Adafruit_NeoPixel.h>

#define BUTTON_PIN   2    // Digital IO pin connected to the button.  This will be
                          // driven with a pull-up resistor so the switch should
                          // pull the pin to ground momentarily.  On a high -> low
                          // transition the button press logic will execute.

#define PIXEL_PIN    9   // Digital IO pin connected to the NeoPixels.

#define PIXEL_COUNT 4   // number of neopixel (change this accordingly)

// Parameter 1 = number of pixels in strip,  neopixel stick has 8
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_RGB     Pixels are wired for RGB bitstream
//   NEO_GRB     Pixels are wired for GRB bitstream, correct for neopixel stick
//   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
//   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

bool oldState = HIGH;
int showType = 0;

void setup() {
  
  pinMode(BUTTON_PIN, INPUT_PULLUP);

  startShow(colorWipe(strip.Color(255, 200, 0));
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
 }

void loop() {
 
  // Get current button state.
  bool newState = digitalRead(BUTTON_PIN);

  // Check if state changed from high to low (button press).
  if (newState == LOW && oldState == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState = digitalRead(BUTTON_PIN);
    if (newState == LOW) {
      showType++;
      if (showType > 9)
        showType=0;
      startShow(showType);
    }
  }

  // Set the last button state to the old state.
  oldState = newState;
}

void startShow(int i) {
  switch(i){
   
    case 0: colorWipe(strip.Color(255, 200, 0), 10);  //
            break;
    case 1: colorWipe(strip.Color(255, 0, 0), 10);  // Red
            break;
    case 2: colorWipe(strip.Color(0, 255, 0), 10);  // Green
            break;
    case 3: colorWipe(strip.Color(0, 0, 255), 10);  // Blue
            break;
    case 4: colorWipe(strip.Color(32, 253, 24), 10); // Hellgrn
            break;
    case 5: colorWipe(strip.Color(213,0,255), 10); // LILA
            break;
    case 6: colorWipe(strip.Color(  225,127, 16), 10); // gelb
            break;
    
  }
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}



// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

Sorry ich komm mir grad blöd vor aber komm nicht drauf was ich falsch mach..

startShow(colorWipe(strip.Color(255, 200, 0));

soltest du erst nach

strip.begin();
strip.show();

ausführen.

strip.begin();

"startet" deinen LED-Streifen. Erst danach kannst du ihm Einstellungen übergeben.

Edit: Auch macht es keinen Sinn deiner Funktion "startShow" einen Farbewert zu übergeben. Die Funktion erwartet einen Wert von 0-6.

1 Like

ICh danke dir jetzt funzt es :star_struck:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.