#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define SIGNAL_PIN A0
#define PULSE_PIN 9
#define POT_TIME A1 // Potentiometer for time scaling
#define POT_AMP A2 // Potentiometer for amplitude scaling
const int stopButton = 7; // Stop / Freeze button (active LOW)
#define NUM_SAMPLES 128
int samples[NUM_SAMPLES]; // stores last captured waveform
void setup() {
pinMode(PULSE_PIN, OUTPUT);
tone(PULSE_PIN, 100); // Generate 100 Hz test square wave
pinMode(stopButton, INPUT_PULLUP); // button with pull-up
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
for (;;);
}
display.clearDisplay();
display.display();
}
void loop() {
// --- Read pots ---
int potTime = analogRead(POT_TIME); // 0–1023
int potAmp = analogRead(POT_AMP); // 0–1023
// Map potentiometers
int sampleDelay = map(potTime, 0, 1023, 20, 2000); // µs per sample
float ampScale = map(potAmp, 0, 1023, .5, 10) / 10.0; // 0.5x – 2.0x amplitude
// --- Capture waveform only if NOT stopped ---
if (digitalRead(stopButton) == HIGH) { // HIGH = running
for (int i = 0; i < NUM_SAMPLES; i++) {
samples[i] = analogRead(SIGNAL_PIN);
delayMicroseconds(sampleDelay);
}
}
// --- Always redraw (so scaling pots work while frozen) ---
display.clearDisplay();
for (int x = 1; x < NUM_SAMPLES; x++) {
int y1 = map(samples[x - 1], 0, 1023, SCREEN_HEIGHT - 1, 0);
int y2 = map(samples[x], 0, 1023, SCREEN_HEIGHT - 1, 0);
// Apply amplitude scaling around vertical center
y1 = (SCREEN_HEIGHT / 2) + (y1 - SCREEN_HEIGHT / 2) * ampScale;
y2 = (SCREEN_HEIGHT / 2) + (y2 - SCREEN_HEIGHT / 2) * ampScale;
y1 = constrain(y1, 0, SCREEN_HEIGHT - 1);
y2 = constrain(y2, 0, SCREEN_HEIGHT - 1);
display.drawLine(x - 1, y1, x, y2, SSD1306_WHITE);
}
// --- Display info text ---
display.setTextSize(1);
display.setCursor(0, 0);
display.print("T:");
display.print(sampleDelay);
display.print("us");
display.setCursor(70, 0);
display.print("A:");
display.print(ampScale, 1);
display.print("x");
if (digitalRead(stopButton) == LOW) {
display.setCursor(100, 0);
display.print("STOP");
}
display.display();
}