#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
#define NUM_BANDS 3
#define BAR_WIDTH 25
#define BAR_GAP 15
#define MAX_BAR_HEIGHT 40
#define X_OFFSET 15 // Starting x position for the first bar
// Arduino pins for each band (might need to change)
const int bandPins[NUM_BANDS] = {A0, A1, A2}; // For bass, mid, treble
const char* bandLabels[] = {"BASS", "MID", "TREB"};
// Smoothing
int currentBarHeights[NUM_BANDS] = {0, 0, 0};
const float smoothingFactor = 0.2; // Lower = smoother, higher = more responsive
// Amplitude measurement
#define SAMPLES_PER_READING 10 // Number of samples to average for each reading
#define DECAY_FACTOR 0.95 // Factor for peak decay (0-1)
// Tracking peak amplitude
int peakValues[NUM_BANDS] = {0, 0, 0};
// Welcome screen bitmap
const unsigned char welcomeScreen [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0xd0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x30, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
0x00, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x01, 0x70, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
0x00, 0x1f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x01, 0x20, 0x00, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00,
0x00, 0x30, 0x18, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00,
0x00, 0x60, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00,
0x00, 0xc0, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00,
0x00, 0x80, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00,
0x01, 0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xc8, 0x00, 0x00,
0x01, 0x00, 0x05, 0x00, 0x00, 0x09, 0x40, 0x60, 0x00, 0x00, 0x00, 0x00, 0x07, 0x8f, 0x00, 0x00,
0x01, 0x00, 0x0e, 0x80, 0x00, 0x29, 0x4c, 0x21, 0x86, 0x6c, 0xcc, 0x00, 0x07, 0x89, 0x00, 0x00,
0x01, 0x00, 0x0f, 0x80, 0x00, 0xcb, 0x52, 0x22, 0x4a, 0x55, 0x23, 0x00, 0x03, 0x08, 0x00, 0x00,
0x01, 0x00, 0x0f, 0x80, 0x01, 0x8f, 0xde, 0x22, 0x19, 0x55, 0xe1, 0x00, 0x00, 0x18, 0x00, 0x00,
0x01, 0x00, 0x0f, 0x80, 0x00, 0x6c, 0x90, 0x22, 0x19, 0x55, 0x0e, 0x00, 0x00, 0x38, 0x00, 0x00,
0x01, 0x30, 0x0f, 0x80, 0x00, 0x04, 0x90, 0x22, 0x4a, 0x55, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,
0x00, 0xb8, 0x0f, 0x00, 0x00, 0x04, 0x8e, 0x21, 0x84, 0x54, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xfc, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7e, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x3f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x0c, 0xcf, 0x99, 0xdf, 0x07, 0x19, 0xbe, 0x67, 0xc3, 0xd9, 0xde, 0xfd, 0xf6, 0x70, 0x00,
0x00, 0x0c, 0xcd, 0xdd, 0xd8, 0x05, 0x19, 0xb3, 0x6c, 0xc3, 0x0d, 0xb0, 0xf1, 0x07, 0x70, 0x00,
0x00, 0x0f, 0xd8, 0xdf, 0xdf, 0x0d, 0x99, 0xb3, 0x6c, 0x63, 0x0f, 0x38, 0x31, 0xe7, 0xf0, 0x00,
0x00, 0x0f, 0xd8, 0xd6, 0xdf, 0x0f, 0x99, 0xb3, 0x6c, 0x61, 0xc6, 0x1e, 0x31, 0xe5, 0xb0, 0x00,
0x00, 0x0c, 0xd8, 0xd0, 0xd8, 0x0f, 0x99, 0xb3, 0x6c, 0xc0, 0xe6, 0x06, 0x31, 0x04, 0x30, 0x00,
0x00, 0x0c, 0xcd, 0x90, 0xdf, 0x18, 0xdf, 0x3e, 0x67, 0xc3, 0xc6, 0x3e, 0x21, 0xf4, 0x30, 0x00,
0x00, 0x0c, 0xc7, 0x10, 0xdf, 0x08, 0xcf, 0x1c, 0x03, 0x83, 0x82, 0x1c, 0x21, 0xf4, 0x20, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x03, 0x9d, 0x1e, 0x80, 0x50, 0x02, 0x03, 0xc0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x1a, 0x15, 0xd2, 0xe0, 0x77, 0x32, 0x92, 0x58, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x23, 0x95, 0x12, 0xa0, 0x55, 0x43, 0x13, 0xc4, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x30, 0x9d, 0x1e, 0xa0, 0x55, 0x43, 0x12, 0x18, 0x00, 0x00, 0x00, 0x00,
0x00, 0x80, 0x00, 0x00, 0x1b, 0x85, 0x02, 0xa0, 0x57, 0x32, 0x93, 0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x80, 0x04, 0x07, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0xb4, 0x1c, 0xe0, 0x05, 0x33, 0xb9, 0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x03, 0x2c, 0x98, 0x84, 0x05, 0x22, 0xa8, 0x40, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0xac, 0x84, 0x64, 0x05, 0x22, 0xa9, 0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x24, 0x9c, 0xe4, 0x07, 0x33, 0xb8, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
void setup() {
Serial.begin(9600);
// Initializing analog pins as inputs
for (int i = 0; i < NUM_BANDS; i++) {
pinMode(bandPins[i], INPUT);
}
// Initializing the display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Loop forever
}
// Showing the display
display.clearDisplay();
showWelcomeScreen();
delay(3000); // Show for 3 seconds
display.clearDisplay();
}
void loop() {
updateSpectrogram();
delay(50); // Updates every 50 ms
}
// Read and process amplitude from an Arduino pin
int readAmplitude(int pin) {
// To get multiple samples and find the peak-to-peak amplitude
int maxValue = 0;
int minValue = 1023;
// Sample the signal a few times to find peaks
for (int i = 0; i < SAMPLES_PER_READING; i++) {
int sample = analogRead(pin);
if (sample > maxValue) maxValue = sample;
if (sample < minValue) minValue = sample;
delayMicroseconds(100); // 100 ms delay
}
// Calculate peak-to-peak amplitude
int amplitude = maxValue - minValue;
return amplitude;
}
void showWelcomeScreen() {
display.clearDisplay();
display.drawBitmap(0, 0, welcomeScreen, 128, 64, 1);
display.display();
}
void updateSpectrogram() {
int rawAmplitudes[NUM_BANDS];
// Read amplitudes from each band
for (int i = 0; i < NUM_BANDS; i++) {
rawAmplitudes[i] = readAmplitude(bandPins[i]);
// Update peak values with decay
if (rawAmplitudes[i] > peakValues[i]) {
// If new amplitude is higher, update the peak
peakValues[i] = rawAmplitudes[i];
} else {
// Otherwise, let the peak decay slowly
peakValues[i] = peakValues[i] * DECAY_FACTOR;
}
// Map the peak values to bar heights (might need to adjust range)
int barHeight = map(peakValues[i], 0, 300, 0, MAX_BAR_HEIGHT);
// Smoothing
currentBarHeights[i] = currentBarHeights[i] * (1 - smoothingFactor) + barHeight * smoothingFactor;
}
displaySpectrogram(currentBarHeights);
}
void displaySpectrogram(int barValues[]) {
display.clearDisplay();
// Title
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(15, 0); // Where the title starts
display.println(F("AUDIO SPECTROGRAM"));
// Baseline
display.drawLine(0, SCREEN_HEIGHT - 10, SCREEN_WIDTH, SCREEN_HEIGHT - 10, SSD1306_WHITE);
// Loop to draw the bars
for (int i = 0; i < NUM_BANDS; i++) {
int x = X_OFFSET + i * (BAR_WIDTH + BAR_GAP);
int barHeight = map(barValues[i], 0, MAX_BAR_HEIGHT, 0, SCREEN_HEIGHT - 25);
// Draw the actual bar
display.fillRect(x, SCREEN_HEIGHT - 10 - barHeight, BAR_WIDTH, barHeight, SSD1306_WHITE);
// Peak marker at the top of the bar
display.drawFastHLine(x, SCREEN_HEIGHT - 10 - barHeight - 1, BAR_WIDTH, SSD1306_WHITE);
// Labels below the bar
display.setTextSize(1);
display.setCursor(x + 2, SCREEN_HEIGHT - 8);
display.print(bandLabels[i]);
}
display.display();
}