#include "Keypad.h"
#include "Wire.h"
#include "LiquidCrystal_I2C.h"
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'}
};
// For Arduino Microcontroller
//byte rowPins[ROWS] = {9, 8, 7, 6};
//byte colPins[COLS] = {5, 4, 3, 2};
// For ESP8266 Microcontroller
byte rowPins[ROWS] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 9, 10};
// For ESP32 Microcontroller
//byte rowPins[ROWS] = {23, 22, 3, 21};
//byte colPins[COLS] = {19, 18, 5, 17};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd(0x27, 20, 4);
/*
E18-D80NK-Infrared-Switch
made on 24 oct 2020
by Amir Mohammad Shojaee @ Electropeak
Home
*/
const int Pin = A0; //analogRead(Pin); sensor 1
const int Pin1 = 8; //digitalRead(Pin); sensor 2 D8
int sensorValue = 0; // sensor 1
int sensorValue1 = 0; // sensor 2
int counter = 0;
int counter1 = 0;
int hitObject = false;
const int len_key = 5;
const int len_test = 5;
//char master_key[len_key]={'1','2','3','4','5'};
char master_key[len_test];
char attempt_key[len_key];
int z=0;
void setup() {
pinMode(Pin, INPUT); //sensor 1
pinMode(Pin1, INPUT); // sensor 2
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Insert ID STUDENT");
}
void loop() {
//**** keypad****//
char key =keypad.getKey();
lcd.setCursor(z-1,1);
if (key) {
Serial.println(key);
switch(key){
case '*':
z=0;
break;
case '=':
delay(100);
checkKEY(); // call check
sensordata(); // sensor press '*' stop
break;
default:
attempt_key[z]=key;
lcd.print(z);
z++;
}
}
}
void sensordata() {
///// ****sensor1****///////
sensorValue = analogRead(Pin);
Serial.print("sensorvalue_1 =");
Serial.println(sensorValue);
if (sensorValue <= 15) { // จูล ทวนเข็ม sensor
counter++;
Serial.println("detect Object_1");
digitalWrite(13, HIGH);
Serial.print("detect counter_1 =");
Serial.println(counter);
lcd.print(" sizeS ");
lcd.setCursor(7,3);
lcd.print(counter1);
//delay(1500);
}
else {
Serial.println("no Object");
digitalWrite(13, LOW);
//delay(1000);
}
/////**** sensor2****///////
sensorValue1 = digitalRead(Pin1);
Serial.print("sensorvalue_2 =");
Serial.println(sensorValue1);
if ((sensorValue1 == 0) && (hitObject == false)){ // HIGH = 1 to LOW =0
counter1++;
hitObject = true;
Serial.println("detect Object_2");
digitalWrite(13, HIGH);
Serial.print("detect counter_2 =");
Serial.println(counter1);
lcd.print(" sizeX ");
lcd.setCursor(7,2);
lcd.print(counter1);
//delay(1500);
}
else if ((sensorValue1 == 1) && (hitObject == true)){
hitObject = false;
Serial.println("no Object_2");
digitalWrite(13, LOW);
//delay(1000);
}
delay(1000);
}
void checkKEY()
{
int correct=0;
int i;
for (i=0; i<len_key; i++) {
if (attempt_key[i]==master_key[i]) {
correct++;
}
}
if (z==len_key){ // (correct==len_key && z==len_key) https://diyi0t.com/keypad-arduino-esp8266-esp32/
lcd.setCursor(0,1);
lcd.print("Correct Key");
delay(3000);
z=0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Insert ID");
}
else
{
lcd.setCursor(0,1);
lcd.print("Incorrect ID");
delay(3000);
z=0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Insert ID");
}
for (int zz=0; zz<len_key; zz++) {
attempt_key[zz]=0;
}
}