#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <EEPROM.h>
#include <Stepper.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <qrcode.h>
#define TFT_WIDTH 320
#define TFT_HEIGHT 240
// LCD I2C address 0x27, 20 column and 4 rows
LiquidCrystal_I2C lcd(0x27, 20, 4);
// Keypad configuration
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 8, 9};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
/*
// Stepper configuration
const int stepsPerRevolution = 200;
Stepper stepper1(stepsPerRevolution, 10, 11, 12, 13);
Stepper stepper2(stepsPerRevolution, 14, 15, 16, 17);
Stepper stepper3(stepsPerRevolution, 18, 19, 20, 21);
*/
int motor1 = 14;
int motor2 = 15;
int motor3 = 16;
// TFT screen configuration (assuming Adafruit ILI9341)
#define TFT_CS 10
#define TFT_RST 11
#define TFT_DC 12
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// QR Code generation
QRCode qrCode;
// LED pins
const int redLed = 22;
const int blueLed = 23;
const int greenLed = 24;
const int YellowLed = 29;
// Button pins
const int button1 = 25;//Keypad
const int button2 = 26;//QrCode
const int button3 = 27;//TestRFID
// Variables
char qty[6];
int qtyIndex = 0;
int x, y, z;
int qr[20];
int upin[13];
int randVals[6];
int lastRand[7];
bool button1State = false;
bool button2State = false;
bool button3State = false;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
pinMode(redLed, OUTPUT);
pinMode(blueLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(YellowLed, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
lcd.init();
lcd.backlight();
Serial.begin(9600);
tft.begin();
tft.fillScreen(ILI9341_BLACK);
resetState();
}
void loop() {
int button1Read = digitalRead(button1);
int button2Read = digitalRead(button2);
int button3Read = digitalRead(button3);
if ((millis() - lastDebounceTime) > debounceDelay) {
if (button1Read == HIGH && !button1State) {
button1State = true;
button1Function();
lastDebounceTime = millis();
} else if (button2Read == HIGH && !button2State) {
button2State = true;
button2Function();
lastDebounceTime = millis();
} else if (button3Read == HIGH && !button3State) {
button3State = true;
button3Function();
lastDebounceTime = millis();
}
}
if (button1Read == LOW) button1State = false;
if (button2Read == LOW) button2State = false;
if (button3Read == LOW) button3State = false;
}
void resetState() {
digitalWrite(redLed, HIGH);
digitalWrite(blueLed, LOW);
digitalWrite(greenLed, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press to Buy");
}
/*void button1Function() {
digitalWrite(redLed, LOW);
digitalWrite(blueLed, HIGH);
digitalWrite(greenLed, LOW);
lcd.print("Function 1");
delay(1000);
resetState();
}*/
void button1Function() {
digitalWrite(redLed, LOW);
digitalWrite(blueLed, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Qty:");
qtyIndex = 0;
while (qtyIndex < 6) {
char key = keypad.getKey();
if (key) {
if (key >= '0' && key <= '9') {
qty[qtyIndex] = key;
lcd.setCursor(qtyIndex + 1 + 10, 0);
lcd.print(key);
qtyIndex++;
} else if (key == 'C' && qtyIndex > 0) {
qtyIndex--;
lcd.setCursor(qtyIndex + 1 + 10, 0);
lcd.print(' ');
} else if (key == 'D') {
qtyIndex = 0;
lcd.setCursor(1, 0);
lcd.print(" ");
}
}
}
x = (qty[0] - '0') * 10 + (qty[1] - '0');
y = (qty[2] - '0') * 10 + (qty[3] - '0');
z = (qty[4] - '0') * 10 + (qty[5] - '0');
lcd.setCursor(0, 2);
lcd.print("Press A to save");
while (true) {
char key = keypad.getKey();
if (key == 'A') {
EEPROM.put(0, x);
EEPROM.put(2, y);
EEPROM.put(4, z);
Serial.println("PUTtvalue");
Serial.print(x);
Serial.print(y);
Serial.println(z);
lcd.setCursor(0, 3);
lcd.print("Saved");
delay(2000);
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Completed");
delay(2000);
resetState();
break;
}
}
}
/*void button2Function() {
digitalWrite(redLed, LOW);
digitalWrite(blueLed, LOW);
digitalWrite(greenLed, HIGH);
lcd.print("Gong2");
delay(1000);
//resetState();
}*/
void button2Function() {
digitalWrite(redLed, LOW);
digitalWrite(blueLed, LOW);
digitalWrite(greenLed, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SCAN QR");
// Fetch values from EEPROM
EEPROM.get(0, x);
EEPROM.get(2, y);
EEPROM.get(4, z);
// Serial.println("Retvalue");
// Serial.print(x);
// Serial.print(y);
// Serial.println(z);
int retValue[6];
retValue[0] = x / 10;
retValue[1] = x % 10;
retValue[2] = y / 10;
retValue[3] = y % 10;
retValue[4] = z / 10;
retValue[5] = z % 10;
qr[0] = 3;
char qrText[20];
for (int i = 0; i < 6; i++) qr[i + 1] = retValue[i];
for (int i = 0; i < 6; i++) qr[i + 7] = random(0, 10);
for (int i = 0; i < 7; i++) qr[i + 13] = random(0, 10);
for (int i = 0; i < 7; i++) lastRand[i] = qr[i + 7];
for (int i = 0; i < 20; i++) qrText[i] = (char)(qr[i] + 65);
//for (int i = 0; i < 19; i++) qrText[i] = qr[i];
// for (int i = 0; i < 20; i++) {
// m[i] = m[i] + 65;
// //Serial.print(m[i]);
// }
// //Serial.println(" ");
// for (int i = 0; i < 20; i++) {
// qrText[i] = (char)m[i];
// Display QR code on TFT and Serial Monitor
tft.fillScreen(ILI9341_BLACK);
/*
qrCode.init();
qrCode.encodeText(qr, sizeof(qr) / sizeof(qr[0]), 1);
qrCode.render(&tft, 50, 50, 200, 200);*/
/////////////////////////////////////ADDED QR CODED
char qrText2[4] = "A111";
generateQRCode(qrText2);
//////////////////////////////////////////////////////
for (int i = 0; i < 20; i++) {
Serial.print(qr[i]);
Serial.print(" ");
}
Serial.println();
for (int i = 0; i < 20; i++) {
Serial.print(qrText[i]);
Serial.print(" ");
}
Serial.println();
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Enter PIN:");
bailout:
int pinIndex = 0;
while (pinIndex < 13) {
char key = keypad.getKey();
if (key >= '0' && key <= '9') {
upin[pinIndex] = key - '0';
lcd.setCursor(pinIndex, 2);
lcd.print(key);
pinIndex++;
}
}
int group1[6], group2[7];
for (int i = 0; i < 6; i++) group1[i] = upin[i];
for (int i = 0; i < 7; i++) group2[i] = upin[i + 6];
int x1 = group1[0] * 10 + group1[1];
int y1 = group1[2] * 10 + group1[3];
int z1 = group1[4] * 10 + group1[5];
Serial.println(x1);
Serial.println(y1); Serial.println(z1);
bool match = true;
for (int i = 0; i < 7; i++) {
if (lastRand[i] != group2[i]) {
match = false;
Serial.println("sdsderer");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Incorrect");
lcd.setCursor(0, 1);
lcd.print("Enter Again");
goto bailout;
//break;
}
}
if (match) {
int xup = x - x1;
int yup = y - y1;
int zup = z - z1;
EEPROM.put(0, xup);
EEPROM.put(2, yup);
EEPROM.put(4, zup);
Serial.println("Jerer");
if (x1 > 0) {
//stepper1.step(720); // Rotate 360 degrees twice
for (int i = 0; i < x1; i++) {
digitalWrite(motor1, HIGH);
delay(500);
digitalWrite(motor1, LOW);
delay(500);
}
}
if (y1 > 0) {
//stepper2.step(720); // Rotate 360 degrees twice
for (int i = 0; i < y1; i++) {
digitalWrite(motor2, HIGH);
delay(500);
digitalWrite(motor2, LOW);
delay(500);
}
}
if (z1 > 0) {
//stepper3.step(720); // Rotate 360 degrees twice
for (int i = 0; i < z1; i++) {
digitalWrite(motor3, HIGH);
delay(500);
digitalWrite(motor3, LOW);
delay(500);
}
}
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Thank you :)");
tft.fillScreen(ILI9341_BLACK);
delay(2000);
resetState();
}
}
void button3Function() {
digitalWrite(YellowLed, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.println("RFID MODE:");
lcd.setCursor(2, 1);
lcd.println("BN 06 04 19");
}
void generateQRCode(const char* text) {
// Create a QR code object
QRCode qrcode;
// Define the size of the QR code (1-40, higher means bigger size)
uint8_t qrcodeData[qrcode_getBufferSize(3)];
qrcode_initText(&qrcode, qrcodeData, 3, 0, text);
// Clear the screen
tft.fillScreen(ILI9341_BLACK);
// Calculate the scale factor
int scale = min(TFT_WIDTH / qrcode.size, TFT_HEIGHT / qrcode.size);
// Calculate horizontal shift
int shiftX = ((TFT_WIDTH - qrcode.size * scale) / 2) - 41;
// Calculate vertical shift
int shiftY = ((TFT_HEIGHT - qrcode.size * scale) / 2) + 50;
// Draw the QR code on the display
for (uint8_t y = 0; y < qrcode.size; y++) {
for (uint8_t x = 0; x < qrcode.size; x++) {
if (qrcode_getModule(&qrcode, x, y)) {
tft.fillRect(shiftX + x * scale, shiftY + y * scale, scale, scale, ILI9341_WHITE);
}
}
}
}