#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Servo.h>
Adafruit_SSD1306 display(128, 64, &Wire, -1);
Servo myServo;
const int angleOptions[] = {0, 45, 90, 135, 180};
const int buttonPin = 2;
const int upButtonPin = 3;
const int downButtonPin = 4;
const int piezoPin = 8;
int currentIndex = 0;
int angle = 0;
void setup() {
Serial.begin(9600);
myServo.attach(9);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(upButtonPin, INPUT_PULLUP);
pinMode(downButtonPin, INPUT_PULLUP);
pinMode(piezoPin, OUTPUT);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Angle:");
display.display();
}
void drawMenu() {
display.clearDisplay();
for (int i = 0; i < sizeof(angleOptions) / sizeof(angleOptions[0]); i++) {
if (i == currentIndex) {
display.setTextColor(SSD1306_WHITE);
display.setTextSize(2);
} else {
display.setTextColor(WHITE);
display.setTextSize(1);
}
display.setCursor(0, i * 10);
display.println(angleOptions[i]);
}
display.display();
}
void loop() {
drawMenu();
if (digitalRead(buttonPin) == LOW) {
angle = angleOptions[currentIndex];
myServo.write(angle);
tone(piezoPin, 1000, 200);
display.clearDisplay();
display.setCursor(0, 0);
display.print("Angle: ");
display.println(angle);
display.display();
delay(400);
}
if (digitalRead(upButtonPin) == LOW) {
currentIndex++;
if (currentIndex >= sizeof(angleOptions) / sizeof(angleOptions[0])) {
currentIndex = 0;
}
delay(100);
}
if (digitalRead(downButtonPin) == LOW) {
currentIndex--;
if (currentIndex < 0) {
currentIndex = sizeof(angleOptions) / sizeof(angleOptions[0]) - 1;
}
delay(100);
}
}