ss20:audio_guide

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

ss20:audio_guide [2021/10/26 22:41] – angelegt andiss20:audio_guide [2021/10/26 22:53] (aktuell) andi
Zeile 108: Zeile 108:
  
 Pitterle, M. (2014). Gedächtnisunterstützung mittels Audio im Museumskontext (Master's thesis). Pitterle, M. (2014). Gedächtnisunterstützung mittels Audio im Museumskontext (Master's thesis).
 +
 +
 +
 +===== Code =====
 +
 +<code>
 +
 +// Soundbox
 +// V1.0
 +
 +// Dies ist der Code zur Sound-Box für lab:prepare im Sommersemester 2020
 +// Die Box nutzt Arduino und den dfPlayer
 +// last edit 20.09.2020
 +// Annika Just
 +
 +// Soundfiles müssen auf der SD Karte im Ordner mp3 liegen
 +// und müssen so nummeriert sein: 0001.mp3
 +
 +// Libraries einbinden
 +
 +#include <SoftwareSerial.h>
 +#include <DFMiniMp3.h>
 +
 +// DEFINITIONS
 +
 +// Für Debounce
 +const int debounceDelay = 15; //Debounce Time
 +unsigned long lastDebounceTime = 0; //Zeitstempel
 +
 +//Buttons für die Songs
 +const uint8_t NUM_BUTTONS   = 9; //Anzahl der Buttons
 +const byte buttons[NUM_BUTTONS] = {4, 5, 6, 7, 8, 9, 10, 11, 12}; //Pins auf denen die Button-Inputs jeweils liegen, anordnung nach Tonleiter
 +byte prevButtonStates[NUM_BUTTONS]; //bool?
 +byte currentButtonStates[NUM_BUTTONS]; //bool?
 +
 +//Poti für Lautstärke
 +// edit: noch nicht Hardwaremässig implementiert (habe kein Einschraubpoti da)
 +const int NUM_POTIS = 1; // Anzahl der Potis
 +const byte potis[NUM_POTIS] = {A3}; //Analog-Pins auf denen die Potis liegen
 +int prevPotiWert[NUM_POTIS];
 +int currentPotiWert[NUM_POTIS];
 +int volume;
 +
 +
 +// das hier wurde 1:1 so übernommen aus Bsp. Code der library:
 +// implement a notification class,
 +// its member methods will get called
 +//
 +class Mp3Notify
 +{
 +  public:
 +    static void PrintlnSourceAction(DfMp3_PlaySources source, const char* action)
 +    {
 +      if (source & DfMp3_PlaySources_Sd)
 +      {
 +        Serial.print("SD Card, ");
 +      }
 +      if (source & DfMp3_PlaySources_Usb)
 +      {
 +        Serial.print("USB Disk, ");
 +      }
 +      if (source & DfMp3_PlaySources_Flash)
 +      {
 +        Serial.print("Flash, ");
 +      }
 +      Serial.println(action);
 +    }
 +    static void OnError(uint16_t errorCode)
 +    {
 +      // see DfMp3_Error for code meaning
 +      Serial.println();
 +      Serial.print("Com Error ");
 +      Serial.println(errorCode);
 +    }
 +    static void OnPlayFinished(DfMp3_PlaySources source, uint16_t track)
 +    {
 +      Serial.print("Play finished for #");
 +      Serial.println(track);
 +    }
 +    static void OnPlaySourceOnline(DfMp3_PlaySources source)
 +    {
 +      PrintlnSourceAction(source, "online");
 +    }
 +    static void OnPlaySourceInserted(DfMp3_PlaySources source)
 +    {
 +      PrintlnSourceAction(source, "inserted");
 +    }
 +    static void OnPlaySourceRemoved(DfMp3_PlaySources source)
 +    {
 +      PrintlnSourceAction(source, "removed");
 +    }
 +};
 +
 +// instance a DFMiniMp3 object,
 +// defined with the above notification class and the hardware serial class
 +SoftwareSerial secondarySerial(3, 13); // RX, TX
 +DFMiniMp3<SoftwareSerial, Mp3Notify> mp3(secondarySerial);
 +
 +
 +void setup() {
 +  // put your setup code here, to run once:
 +  Serial.begin(9600);
 +
 +  Serial.println("initializing...");
 +
 +  mp3.begin();
 +
 +  uint16_t volume = mp3.getVolume();
 +  Serial.print("volume ");
 +  Serial.println(volume);
 +  mp3.setVolume(25);
 +
 +  uint16_t count = mp3.getTotalTrackCount(DfMp3_PlaySource_Sd);
 +  Serial.print("files ");
 +  Serial.println(count);
 +
 +  Serial.println("starting...");
 +
 +  // initialize the pushbutton pin as an input:
 +  for (int i = 0; i < NUM_BUTTONS; ++i) {
 +    pinMode(buttons[i], INPUT_PULLUP);
 +  }
 +
 +// initialize the poti as an input:
 +  for (int i = 0; i < NUM_POTIS; ++i) {
 +    pinMode(potis[i], INPUT);
 +  }
 +
 +}
 +
 +void waitMilliseconds(uint16_t msWait)
 +{
 +  uint32_t start = millis();
 +
 +  while ((millis() - start) < msWait)
 +  {
 +    // calling mp3.loop() periodically allows for notifications
 +    // to be handled without interrupts
 +    mp3.loop();
 +    delay(1);
 +  }
 +}
 +
 +
 +void buttonUpdatePlay() {
 +  for (int i = 0; i < NUM_BUTTONS; ++i) {
 +    int reading = digitalRead(buttons[i]);
 +
 +    // Debounce setings: Das Array mit den vorherigen Button States bekommt ein Update
 +    if (reading != prevButtonStates[i]) {
 +      lastDebounceTime = millis();
 +      prevButtonStates[i] = reading;
 +    }
 +    if ((millis() - lastDebounceTime) > debounceDelay) { // wenn die debounce zeit vergangen ist
 +      if (reading != currentButtonStates[i]) { //überprüfe, ob button state konstant ist
 +        currentButtonStates[i] = reading; // wenn nicht, dann neues Update
 +        if (currentButtonStates[i] == LOW) {
 +          mp3.playMp3FolderTrack(i + 1);
 +          Serial.println(i);
 +
 +        }
 +        //        if (currentButtonStates[i] == HIGH) {
 +        //        }
 +      }
 +    }
 +  }
 +}
 +
 +
 +// Poti Update
 +void potiUpdateVolume() {
 +  int newVolume;
 +  for (int i = 0; i < NUM_POTIS; ++i) {
 +    currentPotiWert[i] = analogRead(potis[i]);
 +    newVolume = map(currentPotiWert[i], 0, 1023, 0, 30);
 +    
 +    if (newVolume != volume) {
 +      
 +      mp3.setVolume(newVolume);
 +      Serial.println(newVolume);
 +      volume = newVolume;
 +    }
 +  }
 +}
 +
 +
 +void loop() {
 +
 +  buttonUpdatePlay();
 +  potiUpdateVolume();
 +
 +}
 +
 +</code>
  
  • ss20/audio_guide.1635280866.txt.gz
  • Zuletzt geändert: 2021/10/26 22:41
  • von andi