beliebige Reihenfolge der Trigger

Hallo, ich kann jetzt alle trigger von meinem LilyPad MP3 ansprechen (er gibt mir zwar immer noch Fehlermeldungen auf den Seriellen Monitor aus, aber er spielt das file ab). Nun würde ich aber gerne die Trigger in beliebiger Reihenfolge ansprechen können, d.h. Ich möchte beispielsweise direkt TRIG3 ansprechen können ohne vorher TRIG1 und TRIG2 angesprochen zu haben. Ich habe es mit switch-case versucht, doch irgendwie funktioniert das nicht. Ich bräuchte dringend Hilfe. Hat jemand einen Tipp oder eine Ahnung wie ich das programmieren kann?
Anbei nochmal mein Code.
Vielen Dank im Voraus.

trigger1.ino (15 KB)

kitamrofni:
Hallo, ich kann jetzt alle trigger von meinem LilyPad MP3 ansprechen (er gibt mir zwar immer noch Fehlermeldungen auf den Seriellen Monitor aus, aber er spielt das file ab). Nun würde ich aber gerne die Trigger in beliebiger Reihenfolge ansprechen können, d.h. Ich möchte beispielsweise direkt TRIG3 ansprechen können ohne vorher TRIG1 und TRIG2 angesprochen zu haben. Ich habe es mit switch-case versucht, doch irgendwie funktioniert das nicht. Ich bräuchte dringend Hilfe. Hat jemand einen Tipp oder eine Ahnung wie ich das programmieren kann?
Anbei nochmal mein Code.
Vielen Dank im Voraus.

Es wäre prima, wenn du deinen Sketch zukünftig direkt als Code einfügst. Dann kann ich den auch auf dem Tablet lesen.
Füge den bitte mit Code-Tags (Schaltfläche </>) hier ein.

hier nochmal der code:

[...]



#include <SPI.h>            // To talk to the SD card and MP3 chip
#include <SdFat.h>          // SD card file system
#include <SFEMP3Shield.h>   // MP3 decoder chip


const int TRIG1 = A0;
const int TRIG2 = A4;
const int TRIG3 = A5;
const int TRIG4 = 1;
const int TRIG5 = 0;
int trigger[5] = {TRIG1,TRIG2,TRIG3,TRIG4,TRIG5};

// And a few outputs we'll be using:

const int ROT_LEDR = 10; // Red LED in rotary encoder (optional)
const int EN_GPIO1 = A2; // Amp enable + MIDI/MP3 mode select
const int SD_CS = 9;     // Chip Select for SD card

SFEMP3Shield MP3player;
SdFat sd;

boolean debugging = true;

boolean interrupt = true;

boolean interruptself = false;

char filename[5][13];

void setup()
{
  int x, index;
  SdFile file;
  byte result;
  char tempfilename[13];

 [...]
  if (debugging) {// List all the files we saved:
    for(x = 0; x <= 4; x++){
      switch(x){
        case 1: {
          Serial.print(F("trigger "));
          Serial.print(x);
          Serial.print(F(": "));
          Serial.println(filename[x-1]);
          break;
        }
        case 2:{
          Serial.print(F("trigger "));
          Serial.print(x);
          Serial.print(F(": "));
          Serial.println(filename[x-1]);
          break;
        }
        case 3:{
          Serial.print(F("trigger "));
          Serial.print(x);
          Serial.print(F(": "));
          Serial.println(filename[x-1]);
          break;
        }
        case 4:{
          Serial.print(F("trigger "));
          Serial.print(x);
          Serial.print(F(": "));
          Serial.println(filename[x-1]);
          break;
        }
      }
    }
  }

  
}


void loop()
{
  int t;              // current trigger
  static int last_t;  // previous (playing) trigger
  int x;
  byte result;
 
         if (digitalRead(trigger[t-1]) == LOW){      
         
     
          x = 0;
          while(x < 50){
            if (digitalRead(trigger[t-1]) == HIGH)
              x++;
            else
              x = 0;
              delay(1);
         }
        } 
    switch(t){
      case 1:{
        if (debugging){
            Serial.print(F("got trigger "));
            Serial.println(t);
         }

         // Do we have a valid filename for this trigger?
         // (Invalid filenames will have 0 as the first character)

         if (filename[t-1][0] == 0){
           if (debugging)
              Serial.println(F("no file with that number"));
         }
         else // We do have a filename for this trigger!
         {
            // If a file is already playing, and we've chosen to
            // allow playback to be interrupted by a new trigger,
            // stop the playback before playing the new file.

            if (interrupt && MP3player.isPlaying() && ((t != last_t) || interruptself)){
                if (debugging)
                   Serial.println(F("stopping playback"));

                   MP3player.stopTrack();
            }

            // Play the filename associated with the trigger number.
            // (If a file is already playing, this command will fail
            //  with error #2).

            result = MP3player.playMP3(filename[t-1]);

            if (result == 0) last_t = t;  // Save playing trigger
 
            if(debugging){
              if(result != 0){
                Serial.print(F("error "));
                Serial.print(result);
                Serial.print(F(" when trying to play track "));
              } 
              else{
                Serial.print(F("playing "));
              }
            Serial.println(filename[t-1]);
            }
          }
          break;
         }

        case 2:{      
         if (debugging){
            Serial.print(F("got trigger "));
            Serial.println(t);
         }

         // Do we have a valid filename for this trigger?
         // (Invalid filenames will have 0 as the first character)

         if (filename[t-1][0] == 0){
           if (debugging)
              Serial.println(F("no file with that number"));
         }
         else // We do have a filename for this trigger!
         {
            // If a file is already playing, and we've chosen to
            // allow playback to be interrupted by a new trigger,
            // stop the playback before playing the new file.

            if (interrupt && MP3player.isPlaying() && ((t != last_t) || interruptself)){
                if (debugging)
                   Serial.println(F("stopping playback"));

                   MP3player.stopTrack();
            }

            // Play the filename associated with the trigger number.
            // (If a file is already playing, this command will fail
            //  with error #2).

            result = MP3player.playMP3(filename[t-1]);

            if (result == 0) last_t = t;  // Save playing trigger
 
            if(debugging){
              if(result != 0){
                Serial.print(F("error "));
                Serial.print(result);
                Serial.print(F(" when trying to play track "));
              } 
              else{
                Serial.print(F("playing "));
              }
            Serial.println(filename[t-1]);
            }
          }
          break;
         }
        
        case 3:{
       
          if (debugging){
              Serial.print(F("got trigger "));
              Serial.println(t);
          }

          // Do we have a valid filename for this trigger?
          // (Invalid filenames will have 0 as the first character)

          if (filename[t-1][0] == 0){
            if (debugging)
               Serial.println(F("no file with that number"));
          }
          else // We do have a filename for this trigger!
          {
            // If a file is already playing, and we've chosen to
            // allow playback to be interrupted by a new trigger,
            // stop the playback before playing the new file.

            if (interrupt && MP3player.isPlaying() && ((t != last_t) || interruptself)){
                if (debugging)
                   Serial.println(F("stopping playback"));

                   MP3player.stopTrack();
            }

            // Play the filename associated with the trigger number.
            // (If a file is already playing, this command will fail
            //  with error #2).

            result = MP3player.playMP3(filename[t-1]);

            if (result == 0) last_t = t;  // Save playing trigger
 
            if(debugging){
              if(result != 0){
                Serial.print(F("error "));
                Serial.print(result);
                Serial.print(F(" when trying to play track "));
              } 
              else{
                Serial.print(F("playing "));
              }
            Serial.println(filename[t-1]);
            }
          }
          break;
         }

        case 4:{      
          if (debugging){
            Serial.print(F("got trigger "));
            Serial.println(t);
          }

          // Do we have a valid filename for this trigger?
          // (Invalid filenames will have 0 as the first character)

          if (filename[t-1][0] == 0){
            if (debugging)
              Serial.println(F("no file with that number"));
          }
          else // We do have a filename for this trigger!
          {
            // If a file is already playing, and we've chosen to
            // allow playback to be interrupted by a new trigger,
            // stop the playback before playing the new file.

            if (interrupt && MP3player.isPlaying() && ((t != last_t) || interruptself)){
                if (debugging)
                   Serial.println(F("stopping playback"));

                   MP3player.stopTrack();
            }

            // Play the filename associated with the trigger number.
            // (If a file is already playing, this command will fail
            //  with error #2).

            result = MP3player.playMP3(filename[t-1]);

            if (result == 0) last_t = t;  // Save playing trigger
 
            if(debugging){
              if(result != 0){
                Serial.print(F("error "));
                Serial.print(result);
                Serial.print(F(" when trying to play track "));
              } 
              else{
                Serial.print(F("playing "));
              }
            Serial.println(filename[t-1]);
            }
          }
          break;
         }
      }
    }

Ich musste leider alle Kommentare rauslöschen, sonst wäre es zu groß gewesen.

kitamrofni: Ich musste leider alle Kommentare rauslöschen, sonst wäre es zu groß gewesen.

Ich vermute, Du hast zu viel gelöscht :)