#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <Keypad.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
Servo myservo;
int potpin = A1;
int nilaiADC;
int deg = 0;
int i = 0;
int cursor=0;
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {3, 2, 1, 0};
byte colPins[COLS] = {7, 6, 5, 4};
byte x = 1;
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() {
// put your setup code here, to run once:
lcd.init();
Serial.begin(9600);
pinMode(potpin, INPUT);
myservo.attach(11);
}
void loop() {
// put your main code here, to run repeatedly:
char key = keypad.getKey();
if (key != NO_KEY)
{
lcd.setCursor(0,0);
deg = (deg * 10) + (key - 48);
i++;
if (i >= 3) {
myservo.write(deg);
deg = 0; i = 0;
}
if (key != 'A')
if (key != 'B')
//if (key != 'C')
//if (key != 'D')
if (key != '*')
if (key != '#')
cursor++;
lcd.setCursor(cursor,0);
if(cursor==3) cursor=0;
lcd.print(key);
}
if (key == 'C')
{
lcd.setCursor(0,0);
lcd.clear();
//myservo.write(deg);
}
if (key == 'D')
{
lcd.setCursor(0,0);
lcd.clear();
myservo.write(deg);
lcd.setCursor(0,1);
lcd.print("Sudut = ");lcd.print(deg);
}
nilaiADC = analogRead(potpin);
//Serial.print("Nilai ADC = "); Serial.print(nilaiADC);
lcd.setCursor(0,2);
lcd.print("Nilai ADC = ");lcd.print(nilaiADC);
deg = map(nilaiADC, 0, 1023, 0, 180);
//Serial.print(", Sudut = "); Serial.println(deg);
myservo.write(deg);
lcd.setCursor(0,3);
lcd.print("Sudut = ");lcd.print(deg);
//delay(50);
//lcd.clear();
}