Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
ss20:audio_guide [2021/10/26 22:41] – angelegt andi | ss20:audio_guide [2021/10/26 22:53] (aktuell) – andi | ||
---|---|---|---|
Zeile 108: | Zeile 108: | ||
Pitterle, M. (2014). Gedächtnisunterstützung mittels Audio im Museumskontext (Master' | Pitterle, M. (2014). Gedächtnisunterstützung mittels Audio im Museumskontext (Master' | ||
+ | |||
+ | |||
+ | |||
+ | ===== 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 < | ||
+ | #include < | ||
+ | |||
+ | // DEFINITIONS | ||
+ | |||
+ | // Für Debounce | ||
+ | const int debounceDelay = 15; //Debounce Time | ||
+ | unsigned long lastDebounceTime = 0; // | ||
+ | |||
+ | //Buttons für die Songs | ||
+ | const uint8_t NUM_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]; | ||
+ | byte currentButtonStates[NUM_BUTTONS]; | ||
+ | |||
+ | //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}; // | ||
+ | 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(" | ||
+ | } | ||
+ | if (source & DfMp3_PlaySources_Usb) | ||
+ | { | ||
+ | Serial.print(" | ||
+ | } | ||
+ | if (source & DfMp3_PlaySources_Flash) | ||
+ | { | ||
+ | Serial.print(" | ||
+ | } | ||
+ | Serial.println(action); | ||
+ | } | ||
+ | static void OnError(uint16_t errorCode) | ||
+ | { | ||
+ | // see DfMp3_Error for code meaning | ||
+ | Serial.println(); | ||
+ | Serial.print(" | ||
+ | Serial.println(errorCode); | ||
+ | } | ||
+ | static void OnPlayFinished(DfMp3_PlaySources source, uint16_t track) | ||
+ | { | ||
+ | Serial.print(" | ||
+ | Serial.println(track); | ||
+ | } | ||
+ | static void OnPlaySourceOnline(DfMp3_PlaySources source) | ||
+ | { | ||
+ | PrintlnSourceAction(source, | ||
+ | } | ||
+ | static void OnPlaySourceInserted(DfMp3_PlaySources source) | ||
+ | { | ||
+ | PrintlnSourceAction(source, | ||
+ | } | ||
+ | static void OnPlaySourceRemoved(DfMp3_PlaySources source) | ||
+ | { | ||
+ | PrintlnSourceAction(source, | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | // instance a DFMiniMp3 object, | ||
+ | // defined with the above notification class and the hardware serial class | ||
+ | SoftwareSerial secondarySerial(3, | ||
+ | DFMiniMp3< | ||
+ | |||
+ | |||
+ | void setup() { | ||
+ | // put your setup code here, to run once: | ||
+ | Serial.begin(9600); | ||
+ | |||
+ | Serial.println(" | ||
+ | |||
+ | mp3.begin(); | ||
+ | |||
+ | uint16_t volume = mp3.getVolume(); | ||
+ | Serial.print(" | ||
+ | Serial.println(volume); | ||
+ | mp3.setVolume(25); | ||
+ | |||
+ | uint16_t count = mp3.getTotalTrackCount(DfMp3_PlaySource_Sd); | ||
+ | Serial.print(" | ||
+ | Serial.println(count); | ||
+ | |||
+ | Serial.println(" | ||
+ | |||
+ | // initialize the pushbutton pin as an input: | ||
+ | for (int i = 0; i < NUM_BUTTONS; | ||
+ | pinMode(buttons[i], | ||
+ | } | ||
+ | |||
+ | // initialize the poti as an input: | ||
+ | for (int i = 0; i < NUM_POTIS; ++i) { | ||
+ | pinMode(potis[i], | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | 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; | ||
+ | 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]) { // | ||
+ | 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], | ||
+ | | ||
+ | if (newVolume != volume) { | ||
+ | | ||
+ | mp3.setVolume(newVolume); | ||
+ | Serial.println(newVolume); | ||
+ | volume = newVolume; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | void loop() { | ||
+ | |||
+ | buttonUpdatePlay(); | ||
+ | potiUpdateVolume(); | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||