#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define OUTPUT_PIN 5
#define POT_PIN A1
const int MIN_FREQUENCY = 100;
const int MAX_FREQUENCY = 600;
const int MAX_READ_VALUE = 1023;
int potValue, frequency, prevSetFreq = 0;
void setup(){
// Setup serial
Serial.begin(115200);
// Set output pin
pinMode(OUTPUT_PIN, OUTPUT);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
}
void loop(){
// Convert potValue to freqency
potValue = analogRead(POT_PIN); // 1023 max
frequency = ((potValue / (float) MAX_READ_VALUE) * (MAX_FREQUENCY - MIN_FREQUENCY)) + MIN_FREQUENCY;
// Set frequency (if changed)
if (frequency != prevSetFreq) {
refreshDisplay();
prevSetFreq = frequency;
}
// Set output
tone(OUTPUT_PIN, frequency);
}
void refreshDisplay() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 10);
display.print("Frequency: ");
display.println(frequency);
display.setCursor(0, 20);
display.display();
}