#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
#define POT A1 // A1 ADC for LGT8F328
#define BUZ A3
int sensorValue = 0;
// create an OLED display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
Serial.begin(9600);
// initialize OLED display with I2C address 0x3C
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
delay(1000); // initialising
pinMode(BUZ, OUTPUT); // Set buzzer - pin 9 as an output
oled.clearDisplay(); // clear display
oled.setTextSize(1); // set text size
oled.setTextColor(WHITE); // set text color
oled.println("Initializing..");
oled.display();
}
void loop() {
sensorValue = analogRead(POT);
int val = map(sensorValue, 0, 717, 1, 255);
int buzzerValue = map(sensorValue, 0, 717, 10, 1000);
oled.clearDisplay(); // clear display
oled.setCursor(0, 0); // set position to display (x,y)
sensorValue = (sensorValue > 715) ? 717: sensorValue;
val = (val > 250) ? 255: val;
oled.println("Pot: " + String(sensorValue) + " " + String(val));
oled.println("Buz: " + String(buzzerValue));
oled.display();
if (buzzerValue > 60) {
tone(BUZ, buzzerValue);
} else {
noTone(BUZ);
}
delay(1000);
}