Front view of echo. powered on, displaying the January 2024 music screen.
A tangible music memory device that allows users to revisit meaningful moments through songs and photographs. By rotating a physical dial to navigate through time, users can rediscover personal memories and the music associated with them, creating a nostalgic and intimate listening experience.
Front view of echo. showing a user rotating the dial to navigate to the next screen.
Close-up bottom view of echo, highlighting the rotary dial and push-button mechanism.
Close-up view of the screen showing the January 2026 selection, including the song title, artist, rotating album cover, and a preview of the associated memory.
Wall-mounted view of echo., showing a user interacting with the dial.
This video demonstrates the user interaction with echo. The user first rotates the dial to navigate through different months before selecting July 2025. This opens the music screen, displaying the song title, artist, and rotating album cover as the music begins playing. The user then pauses the music and rotates the dial to view the memory photograph associated with the song. Finally, the user double-clicks the dial to return to the date selection screen.
Soldering wires to the TFT display for electrical connections.
Troubleshooting speaker playback issues after the DFPlayer Mini failed to communicate with the ESP32, leading to experimentation with alternative microcontrollers, including the Pro Micro.
Testing the connection between the TFT display and rotary encoder, exploring how turning the dial navigates between dates on the screen.
Initial prototype setup focused on testing the rotary encoder's ability to navigate between dates and display different screens, without audio functionality. A rough enclosure was also fabricated using laser-cut components.
One of the most unexpected and time-consuming challenges was getting the DFPlayer Mini to communicate with the ESP32 and play audio through the speaker. Since this was my first time working with SD cards and many of these electronic components, troubleshooting took up a significant portion of my process. I experimented with several approaches, including storing audio in the ESP32’s internal flash memory, using an external SD card reader, and accessing the SD card slot built into the TFT display. However, none of these methods worked reliably.
Eventually, I suspected that the ESP32’s power supply or communication with the DFPlayer might be contributing to the issue. I decided to introduce an Arduino Uno as a separate controller to power and communicate directly with the DFPlayer, while the ESP32 handled the display and rotary encoder. Surprisingly, separating these functions finally allowed the audio to work consistently. Although this process took much longer than anticipated, it taught me the importance of isolating components when troubleshooting and considering alternative hardware configurations rather than relying on a single controller for everything.
Looking back on the crit, the feedback I received pushed me to think harder about why this project exists as a physical object. One guest wrote, "while the idea is good, it seems unlikely someone would have a separate device for this, rather than having it as a phone app. What is the benefit of the physical artifact?" I disagree, because for music enthusiasts the value lies in the experience itself. Like a vinyl player, the physical interaction creates a ritual around listening. Since everything has been consolidated into our phones, I wanted a dedicated artifact that encourages intentional listening away from digital distractions. Another classmate, Hayool, suggested, "I think adding a volume knob, and a feature to add songs to it in the future would be nice." I agree, especially because the current volume is quite loud. With more time, I would have added a physical volume knob and built an interface to upload new songs, photographs, and memories through the ESP32's Wi-Fi capabilities.
Overall, I'm really happy with how this project turned out, and I feel it achieved the goals I set for myself. It pushed me outside my comfort zone and made me work with unfamiliar electronic components and programming techniques. There were many frustrating moments, but seeing everything come together was rewarding and gave me more confidence in tackling technical challenges. I'm especially proud of the meaning behind the device. It works almost like a digital diary of my past, letting me revisit moments in my life through the songs and photographs I associate with them. Music has always been tied to my memories, and rotating a dial to revisit them feels far more intentional than scrolling through photos on my phone.
The process also taught me a lot about my own habits and limitations. If I could advise my past self, I would say to start troubleshooting the speaker much earlier. I assumed it would take an hour, but it took two days, and resolving it sooner would have left time for extra features like a volume knob or a connected web interface. I also learned the importance of designing the internal enclosure more intentionally. Toward the end, I shoved everything inside without much organization, and dedicated compartments for each component, plus planning the wiring beforehand, would have made for a much cleaner assembly. As for the exterior, I really like the minimal, industrial look. It was initially unintentional, but the silver hardware gave it an industrial yet modern feel. That said, a matte black finish on the 3D-printed enclosure would have made it look more polished.
If I built another iteration, I would focus on making the experience more seamless and customizable. First, as mentioned before, I would connect the device to a website where users could upload songs and memories wirelessly, rather than manually swapping the SD card each time. I would also expand the number of songs and photographs associated with each month for a richer collection, and add features like random song selection from a given month, volume control, and skipping between songs. Ultimately, I want the device to be easier to update and more interactive.
Block Diagram
Schematic Diagram
/*
echo.
Arduino Uno R3 - Audio Controller
This code controls music playback for echo., a device that
lets users revisit personal memories through songs and photos.
The Arduino Uno communicates with the DFPlayer Mini and
receives song selection commands from the ESP32-S3.
I ended up using two separate controllers because I had
difficulty getting the DFPlayer to work reliably with
the ESP32 alone.
Pin mapping:
Arduino Uno Connection
-------------------------------------------------------
D2 ESP32-S3 GPIO 2 (serial input)
D3 SoftwareSerial TX (not connected)
D10 DFPlayer Mini TX
D11 DFPlayer Mini RX (through 1k resistor)
5V DFPlayer Mini VCC
GND DFPlayer Mini GND
GND ESP32-S3 GND (shared ground)
The 4-ohm, 3-watt speaker connects directly to
DFPlayer Mini SPK1 and SPK2.
Both controllers are independently powered through USB.
Music files are stored on the DFPlayer's microSD card
in the /mp3/ folder, numbered 0002.mp3 to 0007.mp3.
Serial commands received from ESP32:
'2' to '7' = select song
'P' = pause
'R' = resume
'S' = stop
Libraries:
SoftwareSerial - Arduino
DFRobotDFPlayerMini - DFRobot
Code developed for my Physical Computing project with
assistance from OpenAI ChatGPT (GPT-6),
November-December 2026.
AI prompts included:
- Troubleshoot audio playback and serial communication.
- Assign numbered MP3 files to selected months.
The final wiring, device behavior, and code were
developed and tested through physical prototyping.
*/
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
// DFPlayer Mini serial connection.
// SoftwareSerial uses RX, TX pin order.
SoftwareSerial dfSerial(10, 11);
// Separate serial connection for commands from ESP32.
SoftwareSerial espLink(2, 3);
// Create the DFPlayer control object.
DFRobotDFPlayerMini player;
// Playback state.
bool ready = false;
bool playQueued = false;
bool pausedBeforePlay = false;
int currentSong = 0;
unsigned long playAt = 0;
// Send playback commands to the DFPlayer.
// Only one SoftwareSerial connection can listen at a time,
// so listening switches between the DFPlayer and ESP32.
void sendDF(char command) {
dfSerial.listen();
delay(4);
switch (command) {
case 'P':
player.pause();
break;
case 'R':
player.start();
break;
case 'S':
player.stop();
break;
}
delay(20);
espLink.listen();
}
// Process one command received from the ESP32.
// Numbers 2-7 select songs, while letters control playback.
void receiveCommand(char command) {
if (!ready) return;
if (command == '\r' || command == '\n') {
return;
}
Serial.print("Received: ");
Serial.println(command);
// Select a specific song.
if (command >= '2' && command <= '7') {
currentSong = command - '0';
playQueued = true;
pausedBeforePlay = false;
// Stop the previous track before starting a new one.
sendDF('S');
// Short delay gives the DFPlayer time to stop.
playAt = millis() + 350;
Serial.print("Queued song: ");
Serial.println(currentSong);
return;
}
// Pause, resume, or stop.
switch (command) {
case 'P':
// If the song has not started yet,
// cancel the queued playback.
if (playQueued) {
playQueued = false;
pausedBeforePlay = true;
} else if (currentSong > 0) {
sendDF('P');
}
break;
case 'R':
if (pausedBeforePlay && currentSong > 0) {
pausedBeforePlay = false;
playQueued = true;
playAt = millis() + 50;
} else if (currentSong > 0 && !playQueued) {
sendDF('R');
}
break;
case 'S':
playQueued = false;
pausedBeforePlay = false;
currentSong = 0;
sendDF('S');
break;
}
}
// Initialize the DFPlayer and both serial connections.
void setup() {
Serial.begin(115200);
dfSerial.begin(9600);
dfSerial.listen();
// Allow the DFPlayer and microSD card to start.
delay(3000);
Serial.println("echo. - Arduino Uno Audio Controller");
// Disable acknowledgement waiting and automatic reset.
// This configuration worked more reliably during testing.
bool initialized = player.begin(
dfSerial,
false,
false
);
if (!initialized) {
Serial.println("DFPlayer did not confirm startup.");
}
delay(1500);
// Select the SD card as the music source.
player.outputDevice(DFPLAYER_DEVICE_SD);
delay(800);
// Set volume (0-30).
player.volume(25);
delay(500);
// Begin receiving commands from the ESP32.
espLink.begin(9600);
espLink.listen();
ready = true;
Serial.println("Uno ready.");
}
// Continuously check for song selection and playback commands.
void loop() {
if (!ready) return;
// Read commands coming from the ESP32-S3.
while (espLink.available()) {
receiveCommand(espLink.read());
}
// Also allows direct testing through Serial Monitor.
while (Serial.available()) {
receiveCommand(Serial.read());
}
// Start the selected MP3 after the stop delay.
// This avoids sending repeated playback commands.
if (
playQueued &&
(int32_t)(millis() - playAt) >= 0
) {
playQueued = false;
dfSerial.listen();
delay(4);
// Select a numbered file in the /mp3/ folder.
// Example: 2 plays /mp3/0002.mp3.
player.playMp3Folder(currentSong);
delay(20);
espLink.listen();
Serial.print("Playing song: ");
Serial.println(currentSong);
}
}
/*
echo.
ESP32-S3 - Display and Interaction Controller
echo. is a personal music memory device that allows users
to revisit meaningful moments through songs and photos.
This code controls the TFT display and rotary encoder.
Turning the dial navigates between months from 2024
to 2026. Selecting a month with an assigned song opens
the music screen, showing the album cover and artist.
The album cover rotates while the music is playing.
Users can pause or resume the song by pressing the dial,
and rotate the dial to view the corresponding memory photo.
The ESP32 sends song commands to a separate Arduino Uno,
which handles playback through the DFPlayer Mini.
Pin mapping:
ESP32-S3 Connection
-------------------------------------------------------
GPIO 4 Rotary encoder CLK
GPIO 5 Rotary encoder DT
GPIO 6 Rotary encoder SW
3V3 Rotary encoder +
GND Rotary encoder GND
GPIO 9 TFT CS
GPIO 7 TFT DC
GPIO 8 TFT RST
GPIO 11 TFT MOSI
GPIO 12 TFT SCK
GPIO 13 TFT MISO
3V3 TFT VCC
GND TFT GND
GPIO 2 Arduino Uno D2 (serial commands)
GND Arduino Uno GND (shared ground)
The ESP32-S3 is powered independently through USB.
User interaction:
- Rotate dial: navigate between months
- Press dial: select an assigned month
- Single-click: pause or resume music
- Double-click: return to month selection
- Rotate right: view memory photograph
- Rotate left: return to music screen
Months without assigned songs remain silent.
Libraries:
SPI - Arduino
Adafruit GFX Library - Adafruit
Adafruit ILI9341 - Adafruit
The album covers and memory photographs were supplied
by me and converted into RGB565 image arrays for the TFT.
The three image header files are part of this project.
Code developed with assistance from OpenAI ChatGPT
(GPT-6), November-December 2026.
AI prompts included:
- Create a rotary dial interface for browsing dates.
- Display corresponding songs, album covers, and photos.
- Make the album artwork rotate during playback.
- Reduce image sizes to fit the ESP32 storage limit.
- Fix the circular date selector and comment the code.
The interface and interactions were refined through
physical prototyping, testing, and troubleshooting.
*/
#include <Arduino.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSansBold18pt7b.h>
#include <math.h>
// Image data generated from the album covers and
// memory photos used in the final prototype.
#include "album_covers.h"
#include "memory_photos_full.h"
#include "memory_photos_thumb.h"
// --------------------------------------------------
// Pin mapping
// --------------------------------------------------
#define TFT_CS 9
#define TFT_DC 7
#define TFT_RST 8
#define TFT_MOSI 11
#define TFT_SCK 12
#define TFT_MISO 13
#define SD_CS 10
#define ENCODER_CLK 4
#define ENCODER_DT 5
#define ENCODER_SW 6
// ESP32 GPIO 2 sends commands to Arduino Uno D2.
#define UNO_TX 2
HardwareSerial audioLink(1);
Adafruit_ILI9341 tft(TFT_CS, TFT_DC, TFT_RST);
// --------------------------------------------------
// Display settings
// --------------------------------------------------
const uint16_t BACKGROUND = ILI9341_WHITE;
uint16_t textColor;
uint16_t secondaryColor;
// Album artwork is drawn inside a 120 x 120 canvas.
const int ALBUM_SIZE = 120;
const int ALBUM_X = 20;
const int ALBUM_Y = 49;
GFXcanvas16 albumCanvas(ALBUM_SIZE, ALBUM_SIZE);
float albumAngle = 0.0f;
unsigned long lastSpinTime = 0;
const unsigned long SPIN_INTERVAL = 180;
// --------------------------------------------------
// Month and song information
// --------------------------------------------------
const char* monthNames[12] = {
"JAN", "FEB", "MAR", "APR",
"MAY", "JUN", "JUL", "AUG",
"SEP", "OCT", "NOV", "DEC"
};
const int TOTAL_MONTHS = 36;
int selectedMonth = 0;
// Each position represents one month.
// Index 0 = January 2024.
// Index 35 = December 2026.
//
// 0 means no song assigned.
//
// Songs are placed every six months.
const uint8_t songForMonth[TOTAL_MONTHS] = {
// January - December 2024
2, 0, 0, 0, 0, 0,
3, 0, 0, 0, 0, 0,
// January - December 2025
4, 0, 0, 0, 0, 0,
5, 0, 0, 0, 0, 0,
// January - December 2026
6, 0, 0, 0, 0, 0,
7, 0, 0, 0, 0, 0
};
// Song numbers correspond to the MP3 filenames.
int getCurrentSong() {
return songForMonth[selectedMonth];
}
// Separate title lines help fit text on the small screen.
const char* songTitleLine1[6] = {
"(Just Like)",
"Dream a Little",
"Don't Worry",
"Someday",
"Beaches",
"Bonehead's"
};
const char* songTitleLine2[6] = {
"Starting Over",
"Dream of Me",
"Baby",
"",
"",
"Bank Holiday"
};
const char* artistLine1[6] = {
"John Lennon",
"The Mamas &",
"The Beach Boys",
"The Strokes",
"beabadoobee",
"Oasis"
};
const char* artistLine2[6] = {
"",
"the Papas",
"",
"",
"",
""
};
// Match image arrays to the selected song.
const uint16_t* getAlbumCover() {
int song = getCurrentSong();
if (song >= 2 && song <= 7) {
return albumCovers[song - 2];
}
return nullptr;
}
const uint16_t* getMemoryPhoto() {
int song = getCurrentSong();
if (song >= 2 && song <= 7) {
return memoryFullImages[song - 2];
}
return nullptr;
}
const uint16_t* getMemoryThumbnail() {
int song = getCurrentSong();
if (song >= 2 && song <= 7) {
return memoryThumbImages[song - 2];
}
return nullptr;
}
// --------------------------------------------------
// Screen states
// --------------------------------------------------
enum Screen {
DATE_SCREEN,
MUSIC_SCREEN,
MEMORY_SCREEN
};
Screen currentScreen = DATE_SCREEN;
// --------------------------------------------------
// Music control
// --------------------------------------------------
bool isPlaying = false;
bool pendingPlay = false;
bool songCommandSent = false;
unsigned long playQueuedAt = 0;
// Wait for the Uno to finish starting up.
const unsigned long UNO_STARTUP_MS = 12000;
// Brief delay between displaying a song and playing it.
const unsigned long PLAY_DELAY_MS = 400;
// Send a single character to the Arduino Uno.
void sendAudioCommand(char command) {
audioLink.write(command);
audioLink.flush();
Serial.print("Audio command: ");
Serial.println(command);
}
// Queue the selected song without blocking the display.
void schedulePlay() {
if (getCurrentSong() == 0) return;
pendingPlay = true;
songCommandSent = false;
playQueuedAt = millis();
}
// Send playback command once the Uno is ready.
void updateAudio() {
if (!pendingPlay || !isPlaying) return;
if (millis() < UNO_STARTUP_MS) return;
if (millis() - playQueuedAt < PLAY_DELAY_MS) {
return;
}
pendingPlay = false;
int song = getCurrentSong();
if (song >= 2 && song <= 7) {
sendAudioCommand(char('0' + song));
songCommandSent = true;
}
}
// --------------------------------------------------
// Rotary encoder and push button
// --------------------------------------------------
int lastEncoderCLK = HIGH;
unsigned long lastRotationTime = 0;
int buttonReading = HIGH;
int stableButtonState = HIGH;
unsigned long buttonChangedAt = 0;
unsigned long firstClickTime = 0;
bool waitingForSecondClick = false;
const unsigned long DOUBLE_CLICK_MS = 350;
// --------------------------------------------------
// Text helpers
// --------------------------------------------------
// Draw text using one of Adafruit's built-in fonts.
void drawText(
const char* label,
int x,
int baseline,
const GFXfont* font,
uint16_t color
) {
tft.setFont(font);
tft.setTextSize(1);
tft.setTextColor(color);
tft.setCursor(x, baseline);
tft.print(label);
}
// Center text horizontally on the 320-pixel display.
void drawCenteredText(
const char* label,
int baseline,
const GFXfont* font,
uint16_t color
) {
int16_t x1, y1;
uint16_t width, height;
tft.setFont(font);
tft.setTextSize(1);
tft.getTextBounds(
label, 0, 0,
&x1, &y1,
&width, &height
);
tft.setTextColor(color);
tft.setCursor(
160 - x1 - (int)width / 2,
baseline
);
tft.print(label);
}
// Create the date label for the selected month.
void getDateLabel(char* buffer, size_t length) {
snprintf(
buffer,
length,
"%s %d",
monthNames[selectedMonth % 12],
2024 + selectedMonth / 12
);
}
// Draw an RGB565 image stored in program memory.
void drawImage(
int x,
int y,
const uint16_t* image,
int width,
int height
) {
tft.drawRGBBitmap(
x, y,
(uint16_t*)image,
width, height
);
}
// --------------------------------------------------
// Screen 1: Circular month selector
// --------------------------------------------------
// Redraw the month and year without clearing the ring.
void drawDateText() {
tft.fillRect(
97, 83,
126, 85,
BACKGROUND
);
drawCenteredText(
monthNames[selectedMonth % 12],
120,
&FreeSansBold18pt7b,
textColor
);
char year[16];
snprintf(
year,
sizeof(year),
"%d",
2024 + selectedMonth / 12
);
drawCenteredText(
year,
157,
&FreeSansBold18pt7b,
textColor
);
}
// Clear the message inside the ring.
// The rectangle does not overlap the circular outline.
void clearMonthMessage() {
tft.fillRect(
84, 174,
152, 32,
BACKGROUND
);
}
void drawDateScreen() {
currentScreen = DATE_SCREEN;
tft.fillScreen(BACKGROUND);
// Main circular outline.
tft.drawCircle(
160, 120,
112,
textColor
);
// Short tick marks give the screen a dial-like appearance.
for (int tick = 0; tick < 17; tick++) {
float angle =
(-125 + tick * 5) * PI / 180.0f;
int x1 = 160 + cosf(angle) * 91;
int y1 = 120 + sinf(angle) * 91;
int x2 = 160 + cosf(angle) * 99;
int y2 = 120 + sinf(angle) * 99;
tft.drawLine(
x1, y1,
x2, y2,
secondaryColor
);
}
drawDateText();
}
// --------------------------------------------------
// Rotating album cover
// --------------------------------------------------
// The album image is rotated pixel by pixel inside a
// circular mask. This creates a spinning-record effect
// without storing multiple animation frames.
void drawRotatingAlbum() {
albumCanvas.fillScreen(BACKGROUND);
const uint16_t* artwork = getAlbumCover();
float cosine = cosf(albumAngle);
float sine = sinf(albumAngle);
const float radius = 57.0f;
const float radiusSquared = radius * radius;
const float centerX = 59.5f;
const float centerY = 59.5f;
for (int y = 0; y < ALBUM_SIZE; y++) {
for (int x = 0; x < ALBUM_SIZE; x++) {
float distanceX = x - centerX;
float distanceY = y - centerY;
if (
distanceX * distanceX +
distanceY * distanceY > radiusSquared
) {
continue;
}
// Convert screen coordinates to rotated image coordinates.
float sourceU =
cosine * distanceX + sine * distanceY;
float sourceV =
-sine * distanceX + cosine * distanceY;
int sourceX = constrain(
(int)(sourceU + 60.0f), 0, 119
);
int sourceY = constrain(
(int)(sourceV + 60.0f), 0, 119
);
uint16_t pixel = artwork
? pgm_read_word(
&artwork[sourceY * ALBUM_SIZE + sourceX]
)
: textColor;
albumCanvas.drawPixel(x, y, pixel);
}
}
// Small spindle mark at the center of the record.
albumCanvas.fillCircle(60, 60, 3, textColor);
albumCanvas.fillCircle(60, 60, 1, BACKGROUND);
tft.drawRGBBitmap(
ALBUM_X,
ALBUM_Y,
albumCanvas.getBuffer(),
ALBUM_SIZE,
ALBUM_SIZE
);
}
// --------------------------------------------------
// Playback status on the music screen
// --------------------------------------------------
void drawPlaybackStatus() {
tft.fillRect(
157, 183,
153, 37,
BACKGROUND
);
if (isPlaying) {
// Pause symbol.
tft.fillRect(168, 191, 5, 17, textColor);
tft.fillRect(179, 191, 5, 17, textColor);
drawText(
"PLAYING",
195, 207,
&FreeSans9pt7b,
textColor
);
} else {
// Play symbol.
tft.fillTriangle(
168, 191,
168, 208,
184, 200,
textColor
);
drawText(
"PAUSED",
195, 207,
&FreeSans9pt7b,
textColor
);
}
}
// --------------------------------------------------
// Screen 2: Music playback
// --------------------------------------------------
void drawMusicScreen() {
currentScreen = MUSIC_SCREEN;
tft.fillScreen(BACKGROUND);
char date[20];
getDateLabel(date, sizeof(date));
drawText(
date,
16, 24,
&FreeSans9pt7b,
textColor
);
drawRotatingAlbum();
// Show the selected song title and artist.
int songIndex = getCurrentSong() - 2;
if (songIndex >= 0 && songIndex < 6) {
drawText(
songTitleLine1[songIndex],
160, 55,
&FreeSans9pt7b,
textColor
);
if (songTitleLine2[songIndex][0]) {
drawText(
songTitleLine2[songIndex],
160, 75,
&FreeSans9pt7b,
textColor
);
}
drawText(
artistLine1[songIndex],
160, 94,
&FreeSans9pt7b,
secondaryColor
);
if (artistLine2[songIndex][0]) {
drawText(
artistLine2[songIndex],
160, 111,
&FreeSans9pt7b,
secondaryColor
);
}
}
tft.drawFastHLine(
160, 118,
143,
secondaryColor
);
// Show the memory photo associated with the song.
const uint16_t* thumbnail = getMemoryThumbnail();
if (thumbnail) {
drawImage(
160, 130,
thumbnail,
49, 46
);
}
drawText(
"Memory",
218, 148,
&FreeSans9pt7b,
textColor
);
drawText(
"VIEW >",
218, 169,
&FreeSans9pt7b,
secondaryColor
);
drawPlaybackStatus();
}
// --------------------------------------------------
// Screen 3: Memory photograph
// --------------------------------------------------
void drawMemoryScreen() {
currentScreen = MEMORY_SCREEN;
tft.fillScreen(BACKGROUND);
// Only display the date and photograph.
char date[20];
getDateLabel(date, sizeof(date));
drawText(
date,
10, 35,
&FreeSans9pt7b,
textColor
);
// The image fills the width below the date header.
const uint16_t* photo = getMemoryPhoto();
if (photo) {
drawImage(
0, 50,
photo,
320, 190
);
}
}
// --------------------------------------------------
// Returning to the month selector
// --------------------------------------------------
void returnToDates() {
pendingPlay = false;
songCommandSent = false;
waitingForSecondClick = false;
// Stop the music when leaving the selected month.
sendAudioCommand('S');
isPlaying = false;
drawDateScreen();
}
// --------------------------------------------------
// Rotary encoder navigation
// --------------------------------------------------
void handleRotation(int direction) {
if (currentScreen == DATE_SCREEN) {
selectedMonth += direction;
// Wrap around the 36-month timeline.
if (selectedMonth >= TOTAL_MONTHS) {
selectedMonth = 0;
}
if (selectedMonth < 0) {
selectedMonth = TOTAL_MONTHS - 1;
}
clearMonthMessage();
drawDateText();
} else if (currentScreen == MUSIC_SCREEN) {
// Clockwise rotation opens the memory photograph.
if (direction == 1) {
drawMemoryScreen();
}
} else if (currentScreen == MEMORY_SCREEN) {
// Counterclockwise rotation returns to music.
if (direction == -1) {
drawMusicScreen();
}
}
}
// Read movement from the encoder's CLK and DT pins.
void readEncoder() {
int currentCLK = digitalRead(ENCODER_CLK);
if (
currentCLK != lastEncoderCLK &&
currentCLK == LOW &&
millis() - lastRotationTime > 25
) {
int direction = (
digitalRead(ENCODER_DT) != currentCLK
) ? 1 : -1;
handleRotation(direction);
lastRotationTime = millis();
}
lastEncoderCLK = currentCLK;
}
// --------------------------------------------------
// Button interactions
// --------------------------------------------------
// Toggle pause and resume on the music screen.
void singleClick() {
if (currentScreen != MUSIC_SCREEN) return;
isPlaying = !isPlaying;
if (isPlaying) {
if (songCommandSent) {
sendAudioCommand('R');
} else {
schedulePlay();
}
} else {
if (pendingPlay) {
pendingPlay = false;
} else if (songCommandSent) {
sendAudioCommand('P');
}
}
drawPlaybackStatus();
}
// Select the current month.
void selectMonth() {
int song = getCurrentSong();
if (song == 0) {
// Show feedback inside the circle for empty months.
clearMonthMessage();
drawCenteredText(
"No song assigned",
195,
&FreeSans9pt7b,
secondaryColor
);
return;
}
waitingForSecondClick = false;
isPlaying = true;
pendingPlay = false;
songCommandSent = false;
albumAngle = 0.0f;
drawMusicScreen();
schedulePlay();
}
// Debounce button presses and distinguish single clicks
// from double clicks without using long delays.
void readButton() {
int reading = digitalRead(ENCODER_SW);
if (reading != buttonReading) {
buttonReading = reading;
buttonChangedAt = millis();
}
if (
millis() - buttonChangedAt > 35 &&
reading != stableButtonState
) {
stableButtonState = reading;
// Button action occurs when released.
if (stableButtonState == HIGH) {
if (currentScreen == DATE_SCREEN) {
selectMonth();
} else if (
waitingForSecondClick &&
millis() - firstClickTime <= DOUBLE_CLICK_MS
) {
waitingForSecondClick = false;
returnToDates();
} else {
waitingForSecondClick = true;
firstClickTime = millis();
}
}
}
// A single-click action only happens when the
// double-click window has passed.
if (
waitingForSecondClick &&
millis() - firstClickTime > DOUBLE_CLICK_MS
) {
waitingForSecondClick = false;
singleClick();
}
}
// --------------------------------------------------
// Setup
// --------------------------------------------------
void setup() {
Serial.begin(115200);
// One-way UART connection to the Arduino Uno.
audioLink.begin(
9600,
SERIAL_8N1,
-1,
UNO_TX
);
delay(300);
// Initialize rotary encoder.
pinMode(ENCODER_CLK, INPUT_PULLUP);
pinMode(ENCODER_DT, INPUT_PULLUP);
pinMode(ENCODER_SW, INPUT_PULLUP);
lastEncoderCLK = digitalRead(ENCODER_CLK);
buttonReading = digitalRead(ENCODER_SW);
stableButtonState = buttonReading;
// Keep the TFT module's SD card chip select inactive.
// Album covers and memory photos are stored in flash.
pinMode(SD_CS, OUTPUT);
digitalWrite(SD_CS, HIGH);
// Initialize the TFT's SPI connection.
SPI.begin(
TFT_SCK,
TFT_MISO,
TFT_MOSI,
TFT_CS
);
tft.begin();
tft.setRotation(1);
// Colors for text and interface details.
textColor = tft.color565(37, 37, 39);
secondaryColor = tft.color565(128, 128, 128);
drawDateScreen();
Serial.println("echo. Memory Dial ready.");
}
// --------------------------------------------------
// Main loop
// --------------------------------------------------
void loop() {
readEncoder();
readButton();
updateAudio();
// Rotate the album artwork while music is playing.
// The image stops rotating when playback is paused.
if (
currentScreen == MUSIC_SCREEN &&
isPlaying &&
millis() - lastSpinTime >= SPIN_INTERVAL
) {
lastSpinTime = millis();
albumAngle += 0.18f;
if (albumAngle >= 2 * PI) {
albumAngle -= 2 * PI;
}
drawRotatingAlbum();
}
}