Fehler im Sketch aber wo???

Hallo zusammen,

kann mir jemand mit dem Sketch helfen?
wenn ich den Sketch mit allen voids teste, dann funktioniert die "checkMidi abfrage nur einmal.
alle anderen "voids" funktionieren weiterhin tadellos.

Sobald ich nur eine "void potisAbfragen" im laufenden Programm habe funktioniert auch die "checkMidi" Void tadellos. Ab zwei "voids potisAbfragen" hängt sich die MidiAbfrage wieder auf.

Ich finde den Fehler aber nicht.

Kann mir jemand dabei helfen???

int controlChange = 176; // MIDI Kanal 1
 int controllerNummer [] = {50,51,52,53};
 int controllerWert []= {0,0,0,0};
 int controllerWertAlt []= {0,0,0,0};
 int potiWert []= {0,0,0,0};

// LED Abfrage Midi Empfang
byte commandByte ; //Midi Empfang Daten Channal 1
byte channelByte;  //Midi Daten CHxx
byte statusByte;   //Midi Daten Status
byte cc = 176;


#include <Bounce2.h>
//Tastenabfrage 22-27 [6]
byte taste [] = {LOW,LOW,LOW,LOW,LOW,LOW};
byte tasteAlt [] = {LOW,LOW,LOW,LOW,LOW,LOW};
byte tastepin [] = {22,23,24,25,26,27};
boolean tasteGedrueckt [] = {false,false,false,false,false,false};
Bounce debounce22 = Bounce();
Bounce debounce23 = Bounce();
Bounce debounce24 = Bounce();
Bounce debounce25 = Bounce();
Bounce debounce26 = Bounce();
Bounce debounce27 = Bounce(); 


void setup() {
 Serial.begin(9600);
 Serial1.begin(31250); //Midi Ausgang
 Serial2.begin(31250); //Midi Eingang
 
pinMode(22, INPUT);
pinMode(23, INPUT);
pinMode(24, INPUT);
pinMode(25, INPUT);
pinMode(26, INPUT);
pinMode(27, INPUT);

pinMode (8, OUTPUT);
pinMode (9, OUTPUT);
pinMode (10, OUTPUT);
pinMode (11, OUTPUT);

 debounce22.attach(tastepin[0]);
 debounce22.interval(5); // interval in ms
 debounce23.attach(tastepin[1]);
 debounce23.interval(5); // interval in ms
 debounce24.attach(tastepin[2]);
 debounce24.interval(5); // interval in ms
 debounce25.attach(tastepin[3]);
 debounce25.interval(5); // interval in ms
 debounce26.attach(tastepin[4]);
 debounce26.interval(5); // interval in ms
 debounce27.attach(tastepin[5]);
 debounce27.interval(5); // interval in ms
}

void loop() {

SerialLesen();
checkMIDI();
 
//potisAbfragen(0,0,A0);
//potisAbfragen(1,1,A1);
//potisAbfragen(2,2,A2);
potisAbfragen(3,3,A3);

Springe_zu_taster22(0,22);
Springe_zu_taster23(1,23);
Springe_zu_taster24(2,24);
Springe_zu_taster25(3,25);
Springe_zu_taster26(4,26);
Springe_zu_taster27(5,27);
}

void checkMIDI()
{ 

if ((commandByte == cc)&& (channelByte == 22)&& (statusByte == 1)){digitalWrite(8,HIGH);}
if ((commandByte == cc)&& (channelByte == 22)&& (statusByte == 0)){digitalWrite(8,LOW);}

if ((commandByte == cc)&& (channelByte == 23)&& (statusByte == 1)){digitalWrite(9,HIGH);}
if ((commandByte == cc)&& (channelByte == 23)&& (statusByte == 0)){digitalWrite(9,LOW);}

if ((commandByte == cc)&& (channelByte == 24)&& (statusByte == 1)){digitalWrite(10,HIGH);}
if ((commandByte == cc)&& (channelByte == 24)&& (statusByte == 0)){digitalWrite(10,LOW);}

if ((commandByte == cc)&& (channelByte == 25)&& (statusByte == 1)){digitalWrite(11,HIGH);}
if ((commandByte == cc)&& (channelByte == 25)&& (statusByte == 0)){digitalWrite(11,LOW);}
}          

void SerialLesen()  {
      
                                            
 if     (Serial2.available()==1)              {   
              commandByte = Serial2.read();
            Serial.println (commandByte);
            delay (1);
            channelByte = Serial2.read();
            Serial.println (channelByte);
            delay (1);
            statusByte = Serial2.read();
            Serial.println (statusByte);
      
             {
       
     while (Serial2.available() ==3);  
     } }}
 

  
void potisAbfragen(byte x,byte y,int analogPin){
       potiWert[x] = analogRead(analogPin);
 controllerWert[x] = map(potiWert[x],0,1023,0,127);

 if (controllerWert[x] != controllerWertAlt[x]) {
    sendeMIDI(controlChange, controllerNummer[y], controllerWert[x]); }
    controllerWertAlt[x] = controllerWert[x];}

    void Springe_zu_taster22(int x, int Nummer)
{ debounce22.update();  //debouncer
{   taste[x] = debounce22.read();
    if (taste[x] == HIGH && tasteAlt[x] == LOW) 
    if (tasteGedrueckt[x] == false)  { sendeMIDI(176,Nummer, 127);
    tasteGedrueckt[x] = true; }
    else { sendeMIDI(176, Nummer, 0);
    tasteGedrueckt[x] = false;}
    tasteAlt[x]= taste[x];    }   }

void Springe_zu_taster23(int x, int Nummer)
{ debounce23.update();  //debouncer
{   taste[x] = debounce23.read();
    if (taste[x] == HIGH && tasteAlt[x] == LOW) 
    if (tasteGedrueckt[x] == false)  { sendeMIDI(176,Nummer, 127);
    tasteGedrueckt[x] = true; }
    else { sendeMIDI(176, Nummer, 0);
    tasteGedrueckt[x] = false;}
    tasteAlt[x]= taste[x];    }}

void Springe_zu_taster24(int x, int Nummer)
{ debounce24.update();  //debouncer
{   taste[x] = debounce24.read();
    if (taste[x] == HIGH && tasteAlt[x] == LOW) 
    if (tasteGedrueckt[x] == false)  { sendeMIDI(176,Nummer, 127);
    tasteGedrueckt[x] = true; }
    else { sendeMIDI(176, Nummer, 0);
    tasteGedrueckt[x] = false;}
    tasteAlt[x]= taste[x];    }}

 void Springe_zu_taster25(int x, int Nummer)
{ debounce25.update();  //debouncer
{   taste[x] = debounce25.read();
    if (taste[x] == HIGH && tasteAlt[x] == LOW) 
    if (tasteGedrueckt[x] == false)  { sendeMIDI(176,Nummer, 127);
    tasteGedrueckt[x] = true; }
    else { sendeMIDI(176, Nummer, 0);
    tasteGedrueckt[x] = false;}
    tasteAlt[x]= taste[x];    }}
    
void Springe_zu_taster26(int x, int Nummer)
{ debounce26.update();  //debouncer
{   taste[x] = debounce26.read();
    if (taste[x] == HIGH && tasteAlt[x] == LOW) 
    if (tasteGedrueckt[x] == false)  { sendeMIDI(176,Nummer, 127);
    tasteGedrueckt[x] = true; }
    else { sendeMIDI(176, Nummer, 0);
    tasteGedrueckt[x] = false;}
    tasteAlt[x]= taste[x];    }}

void Springe_zu_taster27(int x, int Nummer)
{ debounce27.update();  //debouncer
{   taste[x] = debounce27.read();
    if (taste[x] == HIGH && tasteAlt[x] == LOW) 
    if (tasteGedrueckt[x] == false)  { sendeMIDI(176,Nummer, 127);
    tasteGedrueckt[x] = true; }
    else { sendeMIDI(176, Nummer, 0);
    tasteGedrueckt[x] = false;}
    tasteAlt[x]= taste[x];    }}

    
void sendeMIDI(int statusByte, int dataByte1, int dataByte2) {
 Serial1.write(statusByte);
 Serial1.write(dataByte1);
 Serial1.write(dataByte2);}

HeiMa:
Kann mir jemand dabei helfen???

Moin,
Setze den bitte in Code-Tags.

Verwende dazu die Schaltfläche </> oben links im Editorfenster.
Dazu den Sketch markieren und die Schaltfläche klicken.

Damit wird dieser für alle besser lesbar, auch mobile Geräte.

 if     (Serial2.available()==1)

Wenn nur ein Byte zur Verfügung steht, kannst du keine 3 Byte lesen.
Zumindest nicht, ohne den Rückgabe Wert zu prüfen.

while (Serial2.available() ==3);

Diese Endlosschleife ist doch nicht beabsichtigt, oder?

Bei deinem Serial Kram fehlt mir die Synchonisation.
Ein Protokoll.

Grundsätzlich:
Ich halte es für falsch, Variablen durchzunummerieren.
Verwende besser Arrays.
Verzichte auf die endlosen, schwer zu wartenden, Codeduplizierungen.


Zu deinem eigentlichen Problem kann ich wenig sagen, da ich noch nicht verstanden habe, was der ganze Klumpen überhaupt tun soll....

Ich bekomme serielle Daten nur wenn ich auch im Player (Native) was änder, da es sich um Midi Daten handelt.
Alleine funktioniert das Daten einlesen. Und auch die Verarbeitung.

Das mit den Arrays habe ich probiert aber mit den analogen Eingängen bin ich gescheitert.

Bin auch nicht der beste Programmierer und verstehe leider nicht immer alles.

Allerdings weiß ich nicht wo genau der Fehler liegt, dass nicht alles in dem CODE funktioniert.

:-)))

Allles klar. Ich habe den Code mal verändert.

Von:

if (Serial2.available()==1) ......
while (Serial2.available() ==3);

zu:

if (Serial2.available()==3) ......
while (Serial2.available() ==1);

Das hat zumindest geholfen, dass der CODE jetzt vollständig funktioniert.

Kann mir jemand noch einen Tipp geben wie ich den Code etwas einfacher schreiben kann?

ACH SO....kann mir jemand sagen, ob ich die DEBOUNCER Funktion auch in einen Array machen kann?

Denn das habe ich versucht aber nicht hin bekommen....
Wäre super wenn mir das jemand beantworten könnte... :slight_smile:

Ja, das geht!

Hier mal nur das Taster/Debounce Gedöns überarbeitet, den Rest habe ich stumpf übernommen.

und völlig ungetestet

int controlChange = 176; // MIDI Kanal 1
 int controllerNummer [] = {50,51,52,53};
 int controllerWert []= {0,0,0,0};
 int controllerWertAlt []= {0,0,0,0};
 int potiWert []= {0,0,0,0};

// LED Abfrage Midi Empfang
byte commandByte ; //Midi Empfang Daten Channal 1
byte channelByte;  //Midi Daten CHxx
byte statusByte;   //Midi Daten Status
byte cc = 176;

void sendeMIDI(int statusByte, int dataByte1, int dataByte2);
void potisAbfragen(byte x,byte y,int analogPin);
void SerialLesen();
void checkMIDI();

#include <Bounce2.h>

class Taster
{
  private:
  byte pin;
  byte Nummer;
  bool tasteAlt = false;
  bool tasteGedrueckt = false;
  Bounce debounce;

  public:
  Taster(byte pin,byte Nummer):pin(pin),Nummer(Nummer){}
  
  void init()
  {
    pinMode(pin,INPUT);
    debounce.attach(pin);
    debounce.interval(5); // interval in ms
  }
  
  void run()
  {
      debounce.update();  //debouncer
         
      bool taste = debounce.read();
      if (taste == HIGH && tasteAlt == LOW) 
      {
        if (tasteGedrueckt == false)  
        { 
          sendeMIDI(176,Nummer, 127);
          tasteGedrueckt = true; 
        }
        else 
        { 
          sendeMIDI(176, Nummer, 0);
          tasteGedrueckt = false;
        }
      }
      tasteAlt= taste;    
  }
};

Taster taster[] = {
                    {22,22},
                    {23,23},
                    {24,24},
                    {25,25},
                    {26,26},
                    {27,27},
                   } ; 


void setup() {
 Serial.begin(9600);
 Serial1.begin(31250); //Midi Ausgang
 Serial2.begin(31250); //Midi Eingang
 for(Taster & taste:taster) taste.init();
 

pinMode (8, OUTPUT);
pinMode (9, OUTPUT);
pinMode (10, OUTPUT);
pinMode (11, OUTPUT);

}

void loop() 
{
  
  SerialLesen();
  checkMIDI();
   
  //potisAbfragen(0,0,A0);
  //potisAbfragen(1,1,A1);
  //potisAbfragen(2,2,A2);
  potisAbfragen(3,3,A3);
  
  for(Taster & taste:taster) taste.run();
  
}

void checkMIDI()
{ 

if ((commandByte == cc)&& (channelByte == 22)&& (statusByte == 1)){digitalWrite(8,HIGH);}
if ((commandByte == cc)&& (channelByte == 22)&& (statusByte == 0)){digitalWrite(8,LOW);}

if ((commandByte == cc)&& (channelByte == 23)&& (statusByte == 1)){digitalWrite(9,HIGH);}
if ((commandByte == cc)&& (channelByte == 23)&& (statusByte == 0)){digitalWrite(9,LOW);}

if ((commandByte == cc)&& (channelByte == 24)&& (statusByte == 1)){digitalWrite(10,HIGH);}
if ((commandByte == cc)&& (channelByte == 24)&& (statusByte == 0)){digitalWrite(10,LOW);}

if ((commandByte == cc)&& (channelByte == 25)&& (statusByte == 1)){digitalWrite(11,HIGH);}
if ((commandByte == cc)&& (channelByte == 25)&& (statusByte == 0)){digitalWrite(11,LOW);}
}          

void SerialLesen()  {
      
                                            
 if     (Serial2.available()==1)              {   
              commandByte = Serial2.read();
            Serial.println (commandByte);
            delay (1);
            channelByte = Serial2.read();
            Serial.println (channelByte);
            delay (1);
            statusByte = Serial2.read();
            Serial.println (statusByte);
      
             {
       
     while (Serial2.available() ==3);  
     } }}
 

  
void potisAbfragen(byte x,byte y,int analogPin){
       potiWert[x] = analogRead(analogPin);
 controllerWert[x] = map(potiWert[x],0,1023,0,127);

 if (controllerWert[x] != controllerWertAlt[x]) {
    sendeMIDI(controlChange, controllerNummer[y], controllerWert[x]); }
    controllerWertAlt[x] = controllerWert[x];}

    
void sendeMIDI(int statusByte, int dataByte1, int dataByte2) 
{
 Serial1.write(statusByte);
 Serial1.write(dataByte1);
 Serial1.write(dataByte2);
}

{22,22}, <<-- da scheint mir noch was überflüssig zu sein. zumindest solange Pin und Nummer gleich bleiben sollen.

WOW, vielen Dank, Combie.

Das funktioniert.

Da muss ich mal in Ruhe drüber schauen. Glaub zwar nicht, dass ich das mal ganz verstehen werde aber ich geb die Hoffnung nicht auf.

Glaub zwar nicht, dass ich das mal ganz verstehen werde

Aber ich!

Sobald du die Sprache gelernt hast, die du da verwendest, dann verstehst du auch den Code, bzw. meine Veränderungen.
Ich kann dir nur empfehlen, dir ein dickes fettes C++ Buch zu kaufen.

Nachtrag:
An deiner Codestrukturierung, Klammersetzung und Einrückung, solltest du noch arbeiten.
Etwas Sorgfalt, macht es deutlich übersichtlicher.

ja, das was ich bisher weiß, habe ich mit anlesen im Netz und "try and error" gelernt.

FUP ist da bedeutend einfacher.

Die Struktur vereinfachen und einrücken muss ich noch machen.

Danke für die Hilfe.

Hab nochmals eine Frage zu meinem serial.read()

void SerialLesen()  {
      
                                            
 if     (Serial2.available()==3)              {  
            delay (2); 
            commandByte = Serial2.read();
            
            delay (2);
            channelByte = Serial2.read();
            
            delay (2);
            statusByte = Serial2.read(); {while (Serial2.available() ==1);  } 
            }
            }

erst lese ich die daten, dann "tu etwas"

 void checkMIDI()
{ 

if ((commandByte == cc)&& (channelByte == 22)&& (statusByte == 1)){digitalWrite(8,HIGH);}
if ((commandByte == cc)&& (channelByte == 22)&& (statusByte == 0)){digitalWrite(8,LOW);}

if ((commandByte == cc)&& (channelByte == 23)&& (statusByte == 1)){digitalWrite(9,HIGH);}
if ((commandByte == cc)&& (channelByte == 23)&& (statusByte == 0)){digitalWrite(9,LOW);}

if ((commandByte == cc)&& (channelByte == 24)&& (statusByte == 1)){digitalWrite(10,HIGH);}
if ((commandByte == cc)&& (channelByte == 24)&& (statusByte == 0)){digitalWrite(10,LOW);}

if ((commandByte == cc)&& (channelByte == 25)&& (statusByte == 1)){digitalWrite(11,HIGH);}
if ((commandByte == cc)&& (channelByte == 25)&& (statusByte == 0)){digitalWrite(11,LOW);}
}

wenn ich vom Controller zufällig zwei Signale (fast zeitgleich) bekomme, dann hängt sich die read funktion auf.

ich weiß nur nicht warum?
Hat jemand eine Idee?

In SerialLesen() wird dein commandByte zwei mal überschrieben...
Warum tust du das?
Ganz offensichtlich brauchst du einen Kommando Parser.

Weiterhin fehlen Einrückungen/Struktur.
Es ist dadurch sehr unangenehm zu lesen.

Weiterhin die sinnfreie Endlosschleife: while (Serial2.available() ==1);
Oder hat sie einen Sinn?
(und ich sehe ihn nur nicht)

Auch dein checkMIDI() Gedöns kannst du sicherlich optimieren.

Es gibt keine "voids". Void ist ein Datentyp. Hier der Rückgabewert der Funktion

:slight_smile:

hab mal die while-schleife entfernt...und siehe da - es macht was es soll.

ja das array muss ich mir auch mal in Ruhe anschauen....
aber alles was ich da bisher versucht habe - hat nicht funktioniert.

HeiMa:
ja das array muss ich mir auch mal in Ruhe anschauen....
aber alles was ich da bisher versucht habe - hat nicht funktioniert.

Weil das dicke fette C++ Buch noch ungelesen in der Bücherei steht.

int controlChange = 176; // MIDI Kanal 1
 int controllerNummer [] = {50,51,52,53};
 int controllerWert []= {0,0,0,0};
 int controllerWertAlt []= {0,0,0,0};
 int potiWert []= {0,0,0,0};

// LED Abfrage Midi Empfang
const byte cc = 176;

void sendeMIDI(int statusByte, int dataByte1, int dataByte2);
void potisAbfragen(byte x,byte y,int analogPin);

#include <Bounce2.h>

class Taster
{
  private:
  byte pin;
  byte Nummer;
  bool tasteAlt = false;
  bool tasteGedrueckt = false;
  Bounce debounce;

  public:
  Taster(byte pin,byte Nummer):pin(pin),Nummer(Nummer){}
  
  void init()
  {
    pinMode(pin,INPUT);
    debounce.attach(pin);
    debounce.interval(5); // interval in ms
  }
  
  void run()
  {
      debounce.update();  //debouncer
         
      bool taste = debounce.read();
      if (taste == HIGH && tasteAlt == LOW) 
      {
        if (tasteGedrueckt == false)  
        { 
          sendeMIDI(176,Nummer, 127);
          tasteGedrueckt = true; 
        }
        else 
        { 
          sendeMIDI(176, Nummer, 0);
          tasteGedrueckt = false;
        }
      }
      tasteAlt= taste;    
  }
};

Taster taster[] = {
                    {22,22},
                    {23,23},
                    {24,24},
                    {25,25},
                    {26,26},
                    {27,27},
                   } ; 


void setup() {
 Serial.begin(9600);
 Serial1.begin(31250); //Midi Ausgang
 Serial2.begin(31250); //Midi Eingang
 for(Taster & taste:taster) taste.init();
 

pinMode (8, OUTPUT);
pinMode (9, OUTPUT);
pinMode (10, OUTPUT);
pinMode (11, OUTPUT);

}

void loop() 
{
 
  //potisAbfragen(0,0,A0);
  //potisAbfragen(1,1,A1);
  //potisAbfragen(2,2,A2);
  potisAbfragen(3,3,A3);
  
  for(Taster & taste:taster) taste.run();
  
}

void CmdParser(byte value)
{
  static byte chanel;  
  enum Status{START,CHANEL,STATUS};  
  static Status status = START;  
  
  switch(status)
  {
    case START  :  if(cc == value) status = CHANEL;
                   break;
                   
    case CHANEL :  if(value < 22 || value > 25)
                   {
                     status = START;
                   }else
                   {
                     chanel = value;
                     status = STATUS;
                   }
                   break;
                  
    case STATUS :  if(value < 2)
                   {
                     byte pin;
                     if(chanel == 22) pin =  8;
                     if(chanel == 23) pin =  9;
                     if(chanel == 24) pin = 10;
                     if(chanel == 25) pin = 11;
                     digitalWrite(pin,value);
                   }
                   status = START;
                   break;
  }
}


void serialEvent2()
{
  while(Serial2.available())  CmdParser(Serial2.read());
}

 

  
void potisAbfragen(byte x,byte y,int analogPin){
       potiWert[x] = analogRead(analogPin);
 controllerWert[x] = map(potiWert[x],0,1023,0,127);

 if (controllerWert[x] != controllerWertAlt[x]) {
    sendeMIDI(controlChange, controllerNummer[y], controllerWert[x]); }
    controllerWertAlt[x] = controllerWert[x];}

    
void sendeMIDI(int statusByte, int dataByte1, int dataByte2) 
{
 Serial1.write(statusByte);
 Serial1.write(dataByte1);
 Serial1.write(dataByte2);
}

Vielen Dank combie.... :slight_smile:

ich glaube bis ich das 1000 Seiten Buch durch habe bin ich an Altersschwäche gestorben.

ich glaube bis ich das 1000 Seiten Buch durch habe bin ich an Altersschwäche gestorben.

Ich habe es gelesen und auch etwas davon verstanden.
Und ich lebe noch.

Meine Hilfe endet damit jetzt auch....

Kannst du mir ein gutes Buch - auch für Anfänger - empfehlen?

Dieses http://www.cppbuch.de/
Es geht nicht auf die Besonderheiten der µC Programmierung ein.

Super.
Vielen Dank....
hab mal wieder einiges gelernt.