neopixel graag 2 verschillende kleuren

Projecten die niet passen in bovenstaande onderwerpen
Berichten: 44
Geregistreerd: 21 Dec 2012, 15:23
Woonplaats: Drenthe

neopixel graag 2 verschillende kleuren

Berichtdoor B.Meijer » 27 Jul 2022, 19:19

Hallo,

Ik heb een vraagje, ik ben met een lightpainting tool bezig voor de fotografie.
In een buisje heb ik 12 neopixels, maar ik krijg ze alleen maar in één kleur.
Ik spring door de kleuren met een knopje.
Graag zou ik led 1-8 bijv blauw willen en led 9-12 geel.

De sketch die ik nu gebruik heb ik gevonden en wat aangepast.
Ik heb wel iets kunnen vinden al R100G100B255P0P1P2P3P4P5P6P7P8P9P10 maar dat vul je in in je seriele monitor.
Het lijkt mij niet zo moeilijk maar ik kan niet precies vinden waar ik moet beginnen

Kan iemand mij op weg helpen om in een case 1 of 2 kleuren te krijgen op een andere manier geformuleerd dan nu.

case 14 en 16 vind ik niet handig voor deze tool die ga ik er uit halen.

groet,
Bert


Code: Alles selecteren
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
 #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif

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

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

#define PIXEL_COUNT 12  // Number of NeoPixels

// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)

boolean oldState = HIGH;
int     mode     = 0;    // Currently-active animation mode, 0-9

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

void loop() {
  // Get current button state.
  boolean 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) {      // Yes, still low
      if(++mode > 16) mode = 0; // Advance to next mode, wrap around after #8
      switch(mode) {           // Start the new animation...
        case 0:
          colorWipe(strip.Color(  0,   0,   0), 50);    // Black/off
          break;
        case 1:
          colorWipe(strip.Color( 0,  255,   0), 30);    // Red
          break;
        case 3:
          colorWipe(strip.Color(  0,   0,   0), 50);    // Black/off
          break;
        case 4:
          colorWipe(strip.Color(  255, 0,   0), 30);    // Green
          break;
        case 5:
          colorWipe(strip.Color(  0,   0,   0), 50);    // Black/off
          break;
        case 6:
          colorWipe(strip.Color(  0,   0, 255), 30);    // Blue
          break;
        case 7:
          colorWipe(strip.Color(  0,   0,   0), 50);    // Black/off
          break;
        //case 8:
          theaterChase(strip.Color( 102, 255, 0), 50); // Oranje Deze doet niet mee
          break;//
        case 8:
          colorWipe(strip.Color( 102, 255, 0), 30); // Oranje
          break;
        case 9:
          colorWipe(strip.Color(  0,   0,   0), 50);    // Black/off
          break;
        //case 10:
          theaterChase(strip.Color(127,   0,   0), 50); // Red  Deze doet niet mee
          break;//
        case 10:
          colorWipe(strip.Color(0,   255,   255), 30); // Fuchsia
          break;
        case 11:
          colorWipe(strip.Color(  0,   0,   0), 50);    // Black/off
          break;
        //case 12:
          theaterChase(strip.Color( 255,   0, 255), 30); // Fuchsia  Deze doet niet mee
          break;//
        case 12:
          colorWipe(strip.Color( 11,   88, 73), 30); // Donkerpaars
          break;
        case 13:
          colorWipe(strip.Color(  0,   0,   0), 50);    // Black/off
          break;
        case 14:
          rainbow(10);
          break;
        case 15:
          colorWipe(strip.Color(  0,   0,   0), 50);    // Black/off
          break;
        case 16:
          theaterChaseRainbow(30);
          break;
         
      }
    }
  }

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

// Fill strip pixels one after another with a color. Strip is NOT cleared
// first; anything there will be covered pixel by pixel. Pass in color
// (as a single 'packed' 32-bit value, which you can get by calling
// strip.Color(red, green, blue) as shown in the loop() function above),
// and a delay time (in milliseconds) between pixels.
void colorWipe(uint32_t color, int wait) {
  for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
    strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
    strip.show();                          //  Update strip to match
    delay(wait);                           //  Pause for a moment
  }
}

// Theater-marquee-style chasing lights. Pass in a color (32-bit value,
// a la strip.Color(r,g,b) as mentioned above), and a delay time (in ms)
// between frames.
void theaterChase(uint32_t color, int wait) {
  for(int a=0; a<10; a++) {  // Repeat 10 times...
    for(int b=0; b<3; b++) { //  'b' counts from 0 to 2...
      strip.clear();         //   Set all pixels in RAM to 0 (off)
      // 'c' counts up from 'b' to end of strip in steps of 3...
      for(int c=b; c<strip.numPixels(); c += 3) {
        strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
      }
      strip.show(); // Update strip with new contents
      delay(wait);  // Pause for a moment
    }
  }
}

// Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
void rainbow(int wait) {
  // Hue of first pixel runs 3 complete loops through the color wheel.
  // Color wheel has a range of 65536 but it's OK if we roll over, so
  // just count from 0 to 3*65536. Adding 256 to firstPixelHue each time
  // means we'll make 3*65536/256 = 768 passes through this outer loop: heb de 3*65536 veranderd in 6*
  for(long firstPixelHue = 0; firstPixelHue < 6*65536; firstPixelHue += 256) {
    for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
      // Offset pixel hue by an amount to make one full revolution of the
      // color wheel (range of 65536) along the length of the strip
      // (strip.numPixels() steps):
      int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
      // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
      // optionally add saturation and value (brightness) (each 0 to 255).
      // Here we're using just the single-argument hue variant. The result
      // is passed through strip.gamma32() to provide 'truer' colors
      // before assigning to each pixel:
      strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
    }
    strip.show(); // Update strip with new contents
    delay(wait);  // Pause for a moment
  }
}

// Rainbow-enhanced theater marquee. Pass delay time (in ms) between frames.
void theaterChaseRainbow(int wait) {
  int firstPixelHue = 0;     // First pixel starts at red (hue 0)
  for(int a=0; a<120; a++) {  // Repeat 30 times...
    for(int b=0; b<3; b++) { //  'b' counts from 0 to 2...
      strip.clear();         //   Set all pixels in RAM to 0 (off)
      // 'c' counts up from 'b' to end of strip in increments of 3...
      for(int c=b; c<strip.numPixels(); c += 3) {
        // hue of pixel 'c' is offset by an amount to make one full
        // revolution of the color wheel (range 65536) along the length
        // of the strip (strip.numPixels() steps):
        int      hue   = firstPixelHue + c * 65536L / strip.numPixels();
        uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGB
        strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
      }
      strip.show();                // Update strip with new contents
      delay(wait);                 // Pause for a moment
      firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames
    }
  }
}
Hou niet van de nachtdienst, maar wel van nachtfotografie

Advertisement

Berichten: 68
Geregistreerd: 04 Sep 2021, 08:31

Re: neopixel graag 2 verschillende kleuren

Berichtdoor RobGood » 27 Jul 2022, 22:00

Bert, kijk es naar Fastled.io.
Library voor neopixels, eenvoudig en 15 miljoen kleuren uit zo een pixel...
Rob

Berichten: 4064
Geregistreerd: 16 Okt 2013, 14:31
Woonplaats: s hertogenbosch

Re: neopixel graag 2 verschillende kleuren

Berichtdoor shooter » 28 Jul 2022, 09:47

je kunt dus elke pixel apart zijn eigen kleur geven, ofwel miljarden mogelijkheden, want je zet eerst de array helemaal klaar en dan doe je een show waardoor de hele matrix naar je leds wordt gezet en dat kan dus heel snel waardoor je een looplicht met allerlei kleuren kunt maken er zijn legio voorbeelden en als je iets speciaals zoekt dan stel je vraag maar en we lossen het op.
paul deelen
shooter@home.nl

Berichten: 44
Geregistreerd: 21 Dec 2012, 15:23
Woonplaats: Drenthe

Re: neopixel graag 2 verschillende kleuren

Berichtdoor B.Meijer » 28 Jul 2022, 12:37

shooter schreef:je kunt dus elke pixel apart zijn eigen kleur geven, ofwel miljarden mogelijkheden, want je zet eerst de array helemaal klaar en dan doe je een show waardoor de hele matrix naar je leds wordt gezet en dat kan dus heel snel waardoor je een looplicht met allerlei kleuren kunt maken er zijn legio voorbeelden en als je iets speciaals zoekt dan stel je vraag maar en we lossen het op.


Hoi Shooter,

Ik snap dat alles mogelijk is, maar ik heb niet veel nodig.
Het werkt goed zoals de code hierboven, 12 led blauw, daarna uit, 12 led rood, uit etc.
Maar ik zou graag 6 led rood en 6 led blauw, uit, 6led geel en 6led magenta, uit, etc.

Ik ben er vanmorgen weer met bezig gegaan, maar krijg het niet werkend. Waarschijnlijk nu een foutje in de codering die ik niet zie.
er branden nu constant 2 rode ledjes

De code is nog wel wat rommelig.


Code: Alles selecteren
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
 #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif

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

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

#define PIXEL_COUNT 12  // Number of NeoPixels

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRBW + NEO_KHZ800);
// Declare our NeoPixel strip object:
//Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)

boolean oldState = HIGH;
int     mode     = 0;    // Currently-active animation mode, 0-9

int PXL1[] = {0,1,2,3,4,6}; // array controlling the first 6 LEDs. Please notice the "LED 0" is the first one, not "LED 1"
int PXL2[] = {5,7,8,9,10,11}; // array controlling the last 6 LEDs.

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


  pixels.begin(); // This initializes the NeoPixel library.
  pixels.show();  // Initialize all pixels to 'off'
}

void loop(){

for(int i=0;i<6;i++) // Since each array has 6 LEDs, we are going to turn them sequentially on using this index.
 
/*
 * Notice that the lines below are just setting up the color of each pixel. This is not yet the command to turn them on. The
 * pixels.setPixelColor command is a very easy way to define the color of each pixel. The syntax is:
 * pixels.setPixelColor(x, pixels.Color(R,G,B)), where:
 * x = the pixel you want to define a color for. In this example, we are using the arrays we created for the 2 control groups, hence the PXL1[i] input.
 * R,G,B = the values of red, green, and blue on a RGB scale.
 */
 



{
  // Get current button state.
  boolean 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) {      // Yes, still low
      if(++mode > 16) mode = 0; // Advance to next mode, wrap around after #8
      switch(mode) {           // Start the new animation...
       
        case 0:
          pixels.setPixelColor(PXL1[i], pixels.Color(0,0,0)); // array number 1 is off
          pixels.setPixelColor(PXL2[i], pixels.Color(0,0,0)); // array number 2 is off
          break;
        case 1:
          pixels.setPixelColor(PXL1[i], pixels.Color(139,0,139)); // array number 1 is magenta
          pixels.setPixelColor(PXL2[i], pixels.Color(255,255,0)); // array number 2 is yellow
          break;
        case 3:
          pixels.setPixelColor(PXL1[i], pixels.Color(0,0,0)); // array number 1 is off
          pixels.setPixelColor(PXL2[i], pixels.Color(0,0,0)); // array number 2 is off
          break;
        case 4:
          pixels.setPixelColor(PXL1[i], pixels.Color(102,255,139)); // array number 1 is magenta
          pixels.setPixelColor(PXL2[i], pixels.Color(255,255,0)); // array number 2 is yellow
          break;
        case 5:
          pixels.setPixelColor(PXL1[i], pixels.Color(0,0,0)); // array number 1 is off
          pixels.setPixelColor(PXL2[i], pixels.Color(0,0,0)); // array number 2 is off
          break;
        case 6:
          pixels.setPixelColor(PXL1[i], pixels.Color(120,0,0)); // array number 1 is magenta
          pixels.setPixelColor(PXL2[i], pixels.Color(255,255,0)); // array number 2 is yellow
          break;
        case 7:
          pixels.setPixelColor(PXL1[i], pixels.Color(0,0,0)); // array number 1 is off
          pixels.setPixelColor(PXL2[i], pixels.Color(0,0,0)); // array number 2 is off
          break;
        //case 8:
         // theaterChase(strip.Color( 102, 255, 0), 50); // Oranje Deze doet niet mee
          break;//
        case 8:
          pixels.setPixelColor(PXL1[i], pixels.Color(139,0,139)); // array number 1 is magenta
          pixels.setPixelColor(PXL2[i], pixels.Color(255,255,0)); // array number 2 is yellow
          break;
        case 9:
          pixels.setPixelColor(PXL1[i], pixels.Color(0,0,0)); // array number 1 is off
          pixels.setPixelColor(PXL2[i], pixels.Color(0,0,0)); // array number 2 is off
          break;
        //case 10:
          //colorWipe(strip.Color( 0,  255,   0), 30);    // Red
          //theaterChase(strip.Color(127,   0,   0), 50); // Red  Deze doet niet mee
          break;//
        case 10:
          pixels.setPixelColor(PXL1[i], pixels.Color(139,0,139)); // array number 1 is magenta
          pixels.setPixelColor(PXL2[i], pixels.Color(255,255,0)); // array number 2 is yellow
          break;
        case 11:
          pixels.setPixelColor(PXL1[i], pixels.Color(0,0,0)); // array number 1 is off
          pixels.setPixelColor(PXL2[i], pixels.Color(0,0,0)); // array number 2 is off
          break;
        //case 12:
          //theaterChase(strip.Color( 255,   0, 255), 30); // Fuchsia  Deze doet niet mee
          break;//
        case 12:
         pixels.setPixelColor(PXL1[i], pixels.Color(139,0,139)); // array number 1 is magenta
          pixels.setPixelColor(PXL2[i], pixels.Color(255,255,0)); // array number 2 is yellow
          break;
        case 13:
          pixels.setPixelColor(PXL1[i], pixels.Color(0,0,0)); // array number 1 is off
          pixels.setPixelColor(PXL2[i], pixels.Color(0,0,0)); // array number 2 is off
          break;
        //case 14:
          //rainbow(10);
          break;
        //case 15:
          //colorWipe(strip.Color(  0,   0,   0), 50);    // Black/off
          break;
        //case 16:
         // theaterChaseRainbow(30);
          break;
         
     
      // Set the last-read button state to the old state.
      oldState = newState;
    }
   }
  }
 } 
}
Hou niet van de nachtdienst, maar wel van nachtfotografie

Berichten: 4064
Geregistreerd: 16 Okt 2013, 14:31
Woonplaats: s hertogenbosch

Re: neopixel graag 2 verschillende kleuren

Berichtdoor shooter » 29 Jul 2022, 09:49

pixels.setPixelColor(PXL1[i], pixels.Color(139,0,139)); // array number 1 is magenta
als je in de i een getal zet van een led maakt even niet uit welk cijfer je gebruikt dan gaat er een led anders reageren dus wordt magenta
en ik mis pixels.show.
paul deelen
shooter@home.nl

Terug naar Overige projecten

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 11 gasten