#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#define button1 10
#define solenoid 13
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
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}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
int but1;
int frame=0;
char inputText[3] = "";
bool task = false;
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
LiquidCrystal_I2C lcd(0x27, 20, 4);
void type_input(){
char key = keypad.getKey();
but1 = digitalRead(button1);
lcd.setCursor(1,2);
lcd.print("Input : ");
Serial.print("key : ");
Serial.print(key);
Serial.print(" strlen : ");
Serial.println(strlen(inputText));
if(key){
if(key == 'D'){
lcd.clear();
lcd.setCursor(1, 2);
lcd.print("Processing...");
task = true;
}
if(strlen(inputText)==2 && task == false){
lcd.clear();
memset(inputText, 0, sizeof(inputText));
}
if(strlen(inputText)<2 && task == false){
inputText[strlen(inputText)] = key;
lcd.setCursor(10+strlen(inputText), 2);
lcd.print(key);
}
if(key == 'C'){
lcd.clear();
memset(inputText, 0, sizeof(inputText));
}
}
}
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(button1, INPUT);
pinMode(solenoid, OUTPUT);
digitalWrite(solenoid, HIGH);
}
void loop() {
if(task == false){
type_input();
}
if(task == true){
char key = keypad.getKey();
if(key == 'D'){
lcd.clear();
task = false;
}
Serial.println(inputText);
if(strcmp((inputText), "#0")==0){
Serial.println("soooleenoiiid");
digitalWrite(solenoid, LOW);
}
}
}