lichtshow (neopixel) und buzzer Musik gleichzeitig

Hallo zusammen

Ich benötige Hilfe im Zusammenhang mit einer Lichtshow und Buzzer Musik gleichzeitig.
Ist das überhaupt möglich?

Ausgangslage:
Ich möchte für meine Schwester einen Pullover mit licht (neopixel) und Musik Weihnachtsmusik machen.
beides voneinander getrennt funktioniert einwandfrei. Auch das zusammenführen ging ohne Probleme,
bis auf das Problem, dass Musik und licht voneinander getrennt laufen.
Ich stehe also voll auf dem Schlauch...

Ich bitte um eure HILFE.

Vielen Dank

Patrick

Anbei der Code:

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif

#define PIN 6

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 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)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(10, PIN, NEO_GRB + NEO_KHZ800);

/* 
We Wish You a Merry Christmas - Traditional Christmas song
Connect a piezo buzzer or speaker to pin 11 or select a new pin.
More songs available at https://github.com/robsoncouto/arduino-songs                                            
                                           
                                           Robson Couto, 2019
*/
#define NOTE_B0  31
#define NOTE_C1  33
....
#define NOTE_DS8 4978
#define REST      0

// change this to make the song slower or faster
int tempo = 140;

// change this to whichever pin you want to use
int buzzer = 9;

// notes of the moledy followed by the duration.
// a 4 means a quarter note, 8 an eighteenth , 16 sixteenth, so on
// !!negative numbers are used to represent dotted notes,
// so -4 means a dotted quarter note, that is, a quarter plus an eighteenth!!
int melody[] = {

// We Wish You a Merry Christmas
// Score available at https://musescore.com/user/6208766/scores/1497501

NOTE_C5,4, //1
NOTE_F5,4, NOTE_F5,8, NOTE_G5,8, NOTE_F5,8, NOTE_E5,8,
NOTE_D5,4, NOTE_D5,4, NOTE_D5,4,
NOTE_G5,4, NOTE_G5,8, NOTE_A5,8, NOTE_G5,8, NOTE_F5,8,
NOTE_E5,4, NOTE_C5,4, NOTE_C5,4,
NOTE_A5,4, NOTE_A5,8, NOTE_AS5,8, NOTE_A5,8, NOTE_G5,8,
NOTE_F5,4, NOTE_D5,4, NOTE_C5,8, NOTE_C5,8,
NOTE_D5,4, NOTE_G5,4, NOTE_E5,4,

.........

NOTE_A5,4, NOTE_A5,8, NOTE_AS5,8, NOTE_A5,8, NOTE_G5,8, //53
NOTE_F5,4, NOTE_D5,4, NOTE_C5,8, NOTE_C5,8,
NOTE_D5,4, NOTE_G5,4, NOTE_E5,4,
NOTE_F5,2, REST,4
};

// sizeof gives the number of bytes, each int value is composed of two bytes (16 bits)
// there are two values per note (pitch and duration), so for each note there are four bytes
int notes = sizeof(melody) / sizeof(melody[0]) / 2;

// this calculates the duration of a whole note in ms
int wholenote = (60000 * 4) / tempo;

int divider = 0, noteDuration = 0;

int var = 0;

void setup() {

strip.begin();
strip.setBrightness(50);
strip.show(); // Initialize all pixels to 'off'

}

void loop() {

while (var < 2) {

// iterate over the notes of the melody.
// Remember, the array is twice the number of notes (notes + durations)
for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {

 // calculates the duration of each note
 divider = melody[thisNote + 1];
 if (divider > 0) {
   // regular note, just proceed
   noteDuration = (wholenote) / divider;
 } else if (divider < 0) {
   // dotted notes are represented with negative durations!!
   noteDuration = (wholenote) / abs(divider);
   noteDuration *= 1.5; // increases the duration in half for dotted notes
 }

 // we only play the note for 90% of the duration, leaving 10% as a pause
 tone(buzzer, melody[thisNote], noteDuration * 0.9);

 // Wait for the specief duration before playing the next note.
 delay(noteDuration);

 // stop the waveform generation before the next note.
 noTone(buzzer);
}

var++;
}

// Some example procedures showing how to display to the pixels:
colorWipe(strip.Color(255, 0, 0), 50); // Green
//colorWipe(strip.Color(0, 255, 0), 50); // Red
//colorWipe(strip.Color(0, 0, 255), 50); // Blue
//colorWipe(strip.Color(0, 0, 0, 255), 50); // White RGBW
// Send a theater pixel chase in...
 theaterChase(strip.Color(127, 127, 127), 200); // White
 theaterChase(strip.Color(0, 127, 0), 200); // Red
 theaterChase(strip.Color(0, 0, 127), 200); // Blue

//rainbow(20);
//rainbowCycle(20);
theaterChase;
}

// 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(500);
}
}

void rainbow(uint8_t wait) {
uint16_t i, j;


for(j=0; j<256; j++) {
 for(i=0; i<strip.numPixels(); i++) {
   strip.setPixelColor(i, Wheel((i+j) & 255));
 }
 strip.show();
 delay(500);
}
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
uint16_t i, j;

for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
 for(i=0; i< strip.numPixels(); i++) {
   strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
 }
 strip.show();
 delay(wait);
}
}

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
for (int j=0; j<10; j++) {  //do 10 cycles of chasing
 for (int q=0; q < 3; q++) {
   for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
     strip.setPixelColor(i+q, c);    //turn every third pixel on
   }
   strip.show();


   delay(wait);


   for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
     strip.setPixelColor(i+q, 0);        //turn every third pixel off
   }
 }
}
}


//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
 for (int q=0; q < 3; q++) {
   for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
     strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
   }
   strip.show();


   delay(wait);


   for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
     strip.setPixelColor(i+q, 0);        //turn every third pixel off
   }
 }
}
}


// 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);
}

Setze Deinen Code bitte in Codetags (</>-Button oben links im Forumseditor oder [code] davor und [/code] dahinter oder gehe in der IDE auf Bearbeiten - Für Forum kopieren und füge es hier ein).
Dann ist er auch auf mobilen Geräten besser lesbar.
Das kannst Du auch noch nachträglich ändern.

Welches Weihnachten meinst Du?

Gruß Tommy

Freemi:
Ich benötige Hilfe im Zusammenhang mit einer Lichtshow und Buzzer Musik gleichzeitig.
Ist das überhaupt möglich?

Ja.

bis auf das Problem, dass Musik und licht voneinander getrennt laufen.
Ich stehe also voll auf dem Schlauch...

Warum kann der Code nicht in entsprechenden tags und vor allem nicht vernünftig formatiert(!) eingestellt werden?
Dein Problem dürfte die Syncronisation zwischen Note und Pixel sein, wenn ich mich Deiner Aussage nähere.

Ist dem so?
Oder wird erst das eine und dann das andere abgearbeitet?

Freemi:
Ich benötige Hilfe im Zusammenhang mit einer Lichtshow und Buzzer Musik gleichzeitig.
Ist das überhaupt möglich?

Konzeptionell ist das ein schlechter Ansatz, da die NeoPixel blockierend angesteuert werden müssen, was sich auf die Tonlänge auswirkt. Eine musikalische Schwester wird das möglicherweise bemerken, gruselig finden und auf eine leere Batterie hoffen. Bei nur zehn Pixeln hält sich die Verzögerung aber in Grenzen.

Mit dieser Einschränkung "Ja!".

Leider hast Du Dir Beispiele mit blockierendem delay() herausgesucht. Wenn es an der einen Stelle blockiert, kann es an einer anderen Stelle im Programm nichts tun. Die Lösung ist eine blockadearme Programmstruktur durch die Verwendung von millis().