#include <Servo.h>
Servo myServo;
// Pin untuk push button
const int button1Pin = 8; // Tombol 1 terhubung ke pin 8
const int button2Pin = 9; // Tombol 2 terhubung ke pin 9
const int button3Pin = 10; // Tombol 3 terhubung ke pin 10
// Variabel untuk menyimpan status tombol
int button1State = 0;
int button2State = 0;
int button3State = 0;
void setup() {
myServo.attach(11); // Servo terhubung ke pin 11
pinMode(button1Pin, INPUT_PULLUP); // Set tombol 1 sebagai input dengan pull-up
pinMode(button2Pin, INPUT_PULLUP); // Set tombol 2 sebagai input dengan pull-up
pinMode(button3Pin, INPUT_PULLUP); // Set tombol 3 sebagai input dengan pull-up
myServo.write(90); // Inisialisasi servo pada posisi 90 derajat
}
void loop() {
// Membaca status tombol
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
button3State = digitalRead(button3Pin);
// Jika tombol 1 ditekan, servo ke 0 derajat
if (button1State == LOW) {
myServo.write(0);
delay(500); // Debouncing delay
}
// Jika tombol 2 ditekan, servo ke 90 derajat
if (button2State == LOW) {
myServo.write(90);
delay(500); // Debouncing delay
}
// Jika tombol 3 ditekan, servo ke 180 derajat
if (button3State == LOW) {
myServo.write(180);
delay(500); // Debouncing delay
}
}