Meldody über Buzzer ohne Delay

Habe vorhin einmal die Standard Methode gestetet zum Abspielen von Tönen auf dem UNO. Gefiel mir Performance teschnisch überhaupt nicht. Das Hauptprogramm muss auf das Ende der Funktion warten, und das dauert.

Versuche gerade, eine andere Variante auf Millis() umzuschreiben. Klappt aber noch nicht ganz.

void beep (unsigned char buzzerPin, int frequencyInHertz, long timeInMilliseconds)
{ 
  digitalWrite(ledPin[1], HIGH);	 
  //use led to visualize the notes being played

  int x; 	 
  long delayAmount = (long)(1000000/frequencyInHertz);
  long loopTime = (long)((timeInMilliseconds*1000)/(delayAmount*2));


for (x = 0; x < loopTime;) {
unsigned long prevMillis;
unsigned long prevMillis2;

  digitalWrite(buzzerPin, HIGH);
int i = 0;
if((millis() - prevMillis > delayAmount) && i == 0)
{
  prevMillis = millis();
  digitalWrite(buzzerPin, LOW);
  i = 1;
}

if((millis() - prevMillis > delayAmount) && i == 1)
{
  prevMillis = millis();
  i = 0;
  x++;}
}

 delay(20);
  //a little delay to make all notes sound separate







 /* for (x=0;x<loopTime;x++) 	 
  { 	 
    digitalWrite(buzzerPin,HIGH);
    delayMicroseconds(delayAmount);
    digitalWrite(buzzerPin,LOW);
    delayMicroseconds(delayAmount);
  } 	 

  digitalWrite(ledPin[1], LOW);
  //set led back to low

  delay(20);
  //a little delay to make all notes sound separate*/

Hab mich bislang nur an die for Schleife gehängt. Aber es will noch nicht klappen, er rastel so durch ohne groß etwas abzuspielen. Die Ausgänge für die LEDs hab ich ersteinmal beabsichtigt rausgelassen.

P.S. Hier Orginal Sketch

Funktioniert so noch nicht, denke abe rich bin weitergekommen.

Kann mir wer bei der Fehlerbeseitgung helfen?

starwars.ino: In function 'void beep(unsigned char, int, long int)':
starwars:36: error: invalid operands of types 'long unsigned int' and 'long unsigned int ()()' to binary 'operator-'
starwars:38: error: assignment of function 'long unsigned int prevMillis()'
starwars:38: error: cannot convert 'long unsigned int' to 'long unsigned int ()()' in assignment

void beep (unsigned char buzzerPin, int frequencyInHertz, long timeInMilliseconds)
{ 
  digitalWrite(ledPin[1], HIGH);	 
  digitalWrite(ledPin[0], LOW);

  long delayAmount = (long)(1000000/frequencyInHertz);
  long loopTime = (long)((timeInMilliseconds*1000)/(delayAmount*2));

  unsigned long prevMillis();
  byte i;
  int x;

  if(millis() - prevMillis > delayAmount)  //ZEILE 36
  {
    prevMillis = millis(); //ZEILE 38

    if( i >= 0 && i < 255)
    {
      i++;
    }
    else i = 0;
  }


  for (x = 0; x < loopTime; ) 	 
  { 
    if (i & 1) {	 
      digitalWrite(buzzerPin,HIGH);
    }
    else {
      digitalWrite(buzzerPin,LOW);
      x++;
    }
  } 	
  digitalWrite(ledPin[1], LOW);
  digitalWrite(ledPin[0], HIGH); 

  delay(20);
  //a little delay to make all notes sound separate

}

unsigned long prevMillis(); <---- Klammern weg. Das ist eine Variable und keine Funktion

EDIT:
Der Compiler hält das wahrscheinlich für einen Funktions Prototyp und lässt es durchgehen

Mein Gott, bin ich blind :smiley: