#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//Inisialisasi servo dan posisi awal
#include <Servo.h>
Servo servo1;
Servo servo2;
int pos = 90;
//inisialisasi button
int buttonPin1 = 7; // Pin tombol 1
int buttonPin2 = 6; // Pin tombol 2
int buttonPin3 = 5; // Pin tombol 3
int buttonPin4 = 4; // Pin tombol 4
LiquidCrystal_I2C lcd(0x27, 16, 2); // Alamat I2C LCD: 0x27, 16 kolom, 2 baris
//These 5 arrays paint the bars that go across the screen.
byte zero[] = {
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000
};
byte one[] = {
B10000,
B10000,
B10000,
B10000,
B10000,
B10000,
B10000,
B10000
};
byte two[] = {
B11000,
B11000,
B11000,
B11000,
B11000,
B11000,
B11000,
B11000
};
byte three[] = {
B11100,
B11100,
B11100,
B11100,
B11100,
B11100,
B11100,
B11100
};
byte four[] = {
B11110,
B11110,
B11110,
B11110,
B11110,
B11110,
B11110,
B11110
};
byte five[] = {
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};
void setup() {
// Inisialisasi LCD
lcd.init();
lcd.backlight();
Serial.begin(9600);
servo1.attach(9);
servo2.attach(8);
pinMode(buttonPin1, INPUT_PULLUP); // Mengatur tombol 1 sebagai input dengan pull-up resistor
pinMode(buttonPin2, INPUT_PULLUP); // Mengatur tombol 2 sebagai input dengan pull-up resistor
pinMode(buttonPin3, INPUT_PULLUP); // Mengatur tombol 3 sebagai input dengan pull-up resistor
pinMode(buttonPin4, INPUT_PULLUP); // Mengatur tombol 4 sebagai input dengan pull-up resistor
// initialize the LCD and allocate the 5 arrays to a number.
lcd.createChar(0, zero);
lcd.createChar(1, one);
lcd.createChar(2, two);
lcd.createChar(3, three);
lcd.createChar(4, four);
lcd.createChar(5, five);
//Setup Awal Ketika Running
animasi1();
animasi2();
lcd.clear();
}
void animasi1()
{
lcd.setCursor(3,0);
lcd.print("***TEST***");
lcd.setCursor(2,1);
lcd.print("***SERVO***");
delay(1500);
lcd.clear();
}
void animasi2()
{
for(int i=0; i <= 100; i++)
{
lcd.setCursor(3,0);
lcd.print("Loading...");
updateProgressBar(i, 100, 1); //This line calls the subroutine that displays the progress bar. The 3 arguments are the current count, the total count and the line you want to print on.
delay(5);
}
lcd.clear();
}
void updateProgressBar(unsigned long count, unsigned long totalCount, int lineToPrintOn)
{
double factor = totalCount/80.0; //See note above!
int percent = (count+1)/factor;
int number = percent/5;
int remainder = percent%5;
if(number > 0)
{
lcd.setCursor(number-1,lineToPrintOn);
lcd.write(5);
}
lcd.setCursor(number,lineToPrintOn);
lcd.write(remainder);
}
void gerakservo(){
if (digitalRead(buttonPin1) == LOW) {
// Jika tombol 1 ditekan (nilai LOW)
for (pos = 0; pos <= 180; pos += 1) {
servo1.write(pos); // Gerakkan servo1 ke posisi pos
servo2.write(180 - pos); // Gerakkan servo2 ke posisi berlawanan (180 - pos)
delay(10); // Tunda selama 15 milidetik
}
for (pos = 180; pos >= 0; pos -= 1) {
servo1.write(pos);
servo2.write(180 - pos);
delay(10);
}
}
if (digitalRead(buttonPin2) == LOW) {
for (pos = 0; pos <= 180; pos += 1) {
servo1.write(180 - pos); // Gerakkan servo1 ke posisi pos
servo2.write(45); // Gerakkan servo2 ke posisi berlawanan (180 - pos)
delay(10); // Tunda selama 15 milidetik
}
for (pos = 180; pos >= 0; pos -= 1) {
servo1.write(180 - pos);
servo2.write(45);
delay(10);
}
}
}
void andolighting(){
lcd.clear();
lcd.setCursor(2,0);
lcd.print("***Ando***");
lcd.setCursor(1,1);
lcd.print("***Lighting***");
}
void loop() {
gerakservo();
andolighting();
delay(100); // Memberikan sedikit keterlambatan agar tampilan stabil
}