#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define buttonStart 13
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {12, 11, 10, 9}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6, 5}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd(0x27, 16, 2);
byte data_count = 10;
int counter = 0;
int target = 0;
// String number = "";
int number = 0;
int memory;
char customKey;
bool switchState = false;
char keyLock = NO_KEY;
void setup(){
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
String data = String ("Target = ");
lcd.print(data + target);
lcd.setCursor(0, 1);
String data2 = String ("Plastik = ");
lcd.print(data2 + counter);
lcd.setCursor(10,0);
pinMode(buttonStart, INPUT_PULLUP);
}
void loop(){
updateCursor();
char key = customKeypad.getKey();
if (key) {
processInput(key);
}
int buttonStart_State = digitalRead(buttonStart);
if (buttonStart_State == LOW){
switchState = true;
} else {
switchState = false;
}
if (switchState){
if(counter>=number){
counter=0;
for (int i=10;i<=15;i++){
lcd.setCursor(i,1);
lcd.print(' ');
}
} else {
counter++;
delay(100);
}
}else{
for (int ii=11;ii<=15;ii++){
lcd.setCursor(ii,1);
lcd.print(' ');
if (ii == 15){
break;
}
}
counter=0;
}
lcd.setCursor(data_count,1);
lcd.print(counter);
Serial.println(counter);
}
void processInput(char key){
if (key != NO_KEY){
if (key >= '0' && key <= '9'){
if (keyLock == NO_KEY) {
number = (number * 10) + (key - '0');
}
}else if (key == 'A'){
keyLock = key;
}else if (key == 'B'){
for(int i = 10; i <= 15; i++){
lcd.setCursor(i,0);
lcd.print(' ');
lcd.setCursor(i,1);
lcd.print(' ');
if(i == 16){
break;
}
}
number = 0;
keyLock = NO_KEY;
}
}
lcd.setCursor(data_count,0);
lcd.print(number);
//lcd.setCursor(data_count,1);
//lcd.print(counter);
Serial.println("Inputan monitor :" + number);
Serial.println("Tombol :" + key);
// switch (key){
// case 'A':
// memory = number.toDouble();
// lcd.noCursor();
// return;
// case 'B':
// break;
// }
// customKey = customKeypad.getKey();
// number = customKey - '0';
// if (customKey){
// Serial.print(number);
// lcd.setCursor(data_count, 0);
// lcd.print(number);
// data_count++;
// }
// if (counter>=number){
// counter=0;
// }
// if (customKey == 'A'){
// lcd.clear();
// counter=0;
// }
// lcd.print(key);
}
void updateCursor() {
if (millis() / 250 % 2 == 0 ) {
lcd.cursor();
} else {
lcd.noCursor();
}
}