#include <Servo.h>
#include <Keypad.h>
Servo myservo; // Buat objek Servo
int pos = 90; // Posisi awal servo
const byte ROWS = 4; // Jumlah baris keypad
const byte COLS = 4; // Jumlah kolom keypad
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; // Hubungkan pin baris keypad ke pin 6, 7, 8, dan 9
byte colPins[COLS] = {5, 4, 3, 2}; // Hubungkan pin kolom keypad ke pin 2, 3, 4, dan 5
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() {
myservo.attach(10); // Attache servo ke pin 10
myservo.write(pos); // Set servo ke posisi awal (90 derajat)
Serial.begin(9600);
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.println(key);
if (key == '1') {
pos = 30;
} else if (key == '2') {
pos = 60;
} else if (key == '3') {
pos = 90;
} else if (key == 'A') {
pos = 120;
} else if (key == '4') {
pos = 180;
}
myservo.write(pos);
}
}