#include <Servo.h>
#include <Wire.h>
#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);
Servo myServo;
const int potPin = A0;
const int buttonPin = 7;
int servoAngle = 0;
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // 버튼 핀 설정
myServo.attach(9);
Wire.begin();
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 10);
display.print("System Ready...");
display.display();
delay(1000);
Serial.begin(9600); // Serial 모니터를 위한 설정
}
void loop() {
int potValue = analogRead(potPin);
servoAngle = map(potValue, 0, 1023, 0, 180);
myServo.write(servoAngle);
display.clearDisplay();
display.setCursor(0, 10);
display.print("Servo Angle: ");
display.print(servoAngle);
display.display();
// 버튼 상태 확인 및 디버깅
int buttonState = digitalRead(buttonPin);
Serial.print("Button State: "); // 버튼 상태 출력
Serial.println(buttonState); // 0: 눌림, 1: 눌리지 않음
if (buttonState == LOW) {
Serial.println("Button Pressed!"); // 버튼 눌렸을 때 출력
myServo.write(90); // 버튼을 누르면 90도로 초기화
display.clearDisplay();
display.setCursor(0, 10);
display.print("Reset to 90 deg");
display.display();
delay(500);
}
else {
Serial.println("Button Not Pressed"); // 버튼 눌리지 않았을 때 출력
}
}