#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <math.h>
#define OLED_RESET 4
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define POT_PIN A0
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int triangle(float x, float freq) {
float cycle_length = SCREEN_WIDTH / freq;
float cycle_pos = fmod(x, cycle_length);
float slope = 2 * SCREEN_HEIGHT / cycle_length;
if (cycle_pos < cycle_length / 2) {
return cycle_pos * slope;
} else {
return SCREEN_HEIGHT - (cycle_pos - cycle_length / 2) * slope;
}
}
void setup() {
pinMode(5, INPUT);
digitalWrite(5, LOW);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.display();
}
void loop() {
display.clearDisplay();
float pot_val = analogRead(POT_PIN);
float freq = map(pot_val, 0, 1023, 1, 10); // map pot_val to frequency range
for (int x = 0; x < SCREEN_WIDTH; x++) {
int y = triangle(x, freq);
display.drawPixel(x, y, WHITE);
display.drawLine(x, y, x, triangle(x + 1, freq), WHITE);
}
display.display();
delay(10);
}
/*for (int x = 0; x < SCREEN_WIDTH; x++) {
y = (5*x/(freq))%SCREEN_WIDTH;
display.drawPixel(x, y, WHITE);
}*/