#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Servo.h>
#define LCD_I2C_ADDRESS 0x3C
#define LEBAR_LCD 128
#define TINGGI_LCD 64
Adafruit_SSD1306 lcd(LEBAR_LCD,TINGGI_LCD,&Wire);
//membuat konstruktor objek servo1
Servo servo1;
byte sudut_servo = 0;
byte step_servo = 0;
long timer_millis =0;
#define PIN_SERVO1 12
#define PIN_POTENSIO A0
void setup() {
// put your setup code here, to run once:
lcd.begin(SSD1306_SWITCHCAPVCC,LCD_I2C_ADDRESS);
servo1.attach(PIN_SERVO1);
pinMode(PIN_POTENSIO,INPUT);
lcd.clearDisplay();
lcd.setTextSize(1);
lcd.setTextColor(WHITE);
lcd.setCursor(0,0);
lcd.print("BLPT DIY");
lcd.setTextColor(BLACK,WHITE);
lcd.setCursor(0,10);
lcd.print("Embedded System");
lcd.setTextColor(WHITE);
lcd.setCursor(0,20);
lcd.print("2024");
lcd.display();
delay(2000);
}
void loop() {
// put your main code here, to run repeatedly:
//baris kode berulang
lcd.clearDisplay();
lcd.setTextSize(1);
lcd.setTextColor(WHITE);
lcd.setCursor(0,0);
lcd.print("Sudut servo:");
lcd.print(sudut_servo);
lcd.setCursor(0,10);
lcd.print("Potensio:");
lcd.print(analogRead(A0));
lcd.display();
if(millis() - timer_millis > 2000){
if(step_servo == 0){
sudut_servo=0;
step_servo=1;
}
else if(step_servo == 1){
sudut_servo = 90;
step_servo =2;
}
else if(step_servo == 2){
sudut_servo = 180;
step_servo =0;
}
servo1.write(sudut_servo);
//reset timer_millis
timer_millis =millis();
}
}