/*#include <Wire.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
////////////////QR GENERATOR
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <qrcode.h>
// TFT display dimensions
#define TFT_CS 10 // Define the chip select pin for the TFT display
#define TFT_RST 11 // Define the reset pin for the TFT display
#define TFT_DC 12 // Define the data/command pin for the TFT display
#define TFT_WIDTH 320
#define TFT_HEIGHT 240
// TFT display object
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
/////////////////////////////
////////////////RANDOM NUMBER GENERATOR
String randNumberArray[6];
String randNumber;
char c[6];
//////////////////////
//////////////MASTER STRING
int m[20];
int zz = 3;
int x1 = 1;
int x2 = 2;
int x3 = 3;
int x4 = 4;
int x5 = 5;
int x6 = 6;
int x7 = 7;
int x8 = 8;
int x9 = 9;
int x10 = 0;
int x11 = 1;
int x12 = 2;
int x13 = 3;
int x14 = 4;
int x15 = 5;
int x16 = 6;
int x17 = 7;
int x18 = 8;
int x19 = 9;
//////////////////////////////
// BUTTON STATE
const int buttonPin = A0;
int detectionState = 0;
int buttonState = 0;
int lastButtonState = 0;
////////////////////
//////////////////////
// BUTTON STATE TWO
const int buttonPin1 = A1;
int detectionState1 = 0;
int buttonState1 = 0;
int lastButtonState1 = 0;
////////////////////
int x;
int y;
int z;
int l,mmm,n;
// Set up the keypad
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}; // Connect to the row pins of the keypad
byte colPins[COLS] = {6, 7, 8, 9}; // Connect to the column pins of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Set up the LCD
LiquidCrystal_I2C lcd(0x27, 20, 4); // Change the address if needed
// Maximum number of digits to store
const int maxDigits = 6; // 6-digit number
// Array to store the input number
int userInput[maxDigits]; // Assuming input numbers will be integers
// Index to keep track of the current position in the array
int currentIndex = 0;
// EPROM
// Define the EEPROM addresses for each int variable
const int eepromAddressInt1 = 0;
const int eepromAddressInt2 = sizeof(int);
const int eepromAddressInt3 = sizeof(int) * 2;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
/////////////////QR GENERATOR
// Initialize the TFT display
tft.begin();
tft.setRotation(3); // Adjust rotation if needed
tft.fillScreen(ILI9341_BLACK);
//////////////////////
/////////////////RANDOM NMBER GENERATOR
randomSeed(analogRead(0));
// //////////////BUTTON STATE
pinMode(buttonPin, INPUT);
pinMode(14,OUTPUT);
////////////////////
//////////////////////
// BUTTON STATE TWO
pinMode(buttonPin1, INPUT);
pinMode(16,OUTPUT);
////////////////////
//EPROM
// Save the initial values to EEPROM
//saveIntsToEEPROM();
// Read the values from EEPROM, add 3 to each, and print on Serial Monitor
//updateAndPrintInts();
}
void loop() {
buttonState = digitalRead(buttonPin);
buttonState1 = digitalRead(buttonPin1);
if(buttonState != lastButtonState){
detectionState=(detectionState+1)%2;
//Serial.print("The detection state is:");
//Serial.println(detectionState);
digitalWrite(14,HIGH);
lcd.clear();
char key = keypad.getKey();
// Display the entered digits on the LCD
//lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Input: ");
for (int i = 0; i < currentIndex; i++) {
lcd.print(userInput[i]);
}
if (key != NO_KEY) {
if (isdigit(key) && currentIndex < maxDigits) {
// Add the pressed digit to the array
userInput[currentIndex++] = key - '0'; // Convert char to integer and store
}
if (key == 'D') {
// Delete the last entered digit
if (currentIndex > 0) {
currentIndex--;
}
} else if (key == 'C') {
// Clear all entered digits
currentIndex = 0;
}
else if (key == 'A') {
if(currentIndex == maxDigits){
lcd.setCursor(0, 2);
lcd.print("DONE!");
x = (userInput[0] * 10) + userInput[1];
y = (userInput[2] * 10) + userInput[3];
z = (userInput[4] * 10) + userInput[5];
//EPROM
EEPROM.put(eepromAddressInt1, x);
EEPROM.put(eepromAddressInt2, y);
EEPROM.put(eepromAddressInt3, z);
Serial.println("Initial int values saved to EEPROM.");
}
Serial.println("Inside BTN1");
delay(200);
}
if (key == 'B') {
lcd.setCursor(0, 3);
lcd.print(x);
lcd.print("--");
lcd.print(y);
lcd.print("--");
lcd.print(z);
}
}
digitalWrite(14,LOW);
delay(50);
}
///////////////////////////////////////////////////////////
else if (buttonState1 != lastButtonState1) {
detectionState1=(detectionState1+1)%2;
digitalWrite(16,HIGH);
delay(200);
//Serial.println("Inside 2nd button");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Scan QR");
////////////////////////////////
//////////////////////RANDOM NUMBER GENERATOR
randNumber = random(100000, 999999);
//Serial.println("rand");
//Serial.println(randNumber);
randNumberArray[0] = String(randNumber);
Serial.println(randNumberArray[0]);
// Split string a into character array c
splitString(randNumber, c);
// Print character array c
printCharArray(c, 6);
///////////GET FROM EEPROM
EEPROM.get(eepromAddressInt1, l);
EEPROM.get(eepromAddressInt2, mmm);
EEPROM.get(eepromAddressInt3, n);
Serial.println("Getting values saved to EEPROM.");
Serial.println(l);
Serial.println(mmm);
Serial.println(n);
int p1,p2,q1,q2,r1,r2;
p1= (l/10);
p2= l - (p1*10);
q1= (mmm/10);
q2= mmm - (q1*10);
r1= (n/10);
r2= n - (r1*10);
Serial.println("Splitted from EEPROM.");
Serial.print(p1);
Serial.println(p2);
Serial.print(q1);
Serial.println(q2);
Serial.print(r1);
Serial.println(r2);
int var[6];
for (int i = 0; i < 6; i++) {
var[i] = c[i] - '0'; // Subtracting '0' converts the char digit to the corresponding int value
}
for (int i = 0; i < 6; i++) {
Serial.print("var[");
Serial.print(i);
Serial.print("] = ");
Serial.println(var[i]);
}
////////////////////////////////////
///////////////////////MASTER STRING
m[0] = zz;
m[1] = p1;
m[2] = p2;
m[3] = q1;
m[4] = q2;
m[5] = r1;
m[6] = r2;
int g=0;
for (int i=7 ; i<=12 ; i++) {
m[i] = var[g];
g++;
}
m[13] = x13;
m[14] = x14;
m[15] = x15;
m[16] = x16;
m[17] = x17;
m[18] = x18;
m[19] = x19;
Serial.println("Without ASCI");
Serial.println(" ");
// Print all elements of the array
for (int i = 0; i < 20; i++) {
Serial.print(m[i]);
}
Serial.println("");
//delay(500);
////////////////////////////////
/////////////////////////GENERATE QR
//int exampleValue = m[20];
char qrText[20];
//char newChar[20];
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]; // Convert integer to character
}
// Print the character array to the Serial Monitor
for (int i = 0; i < 20; i++) {
//Serial.print(qrText[i]);
}
//Serial.println();
//char qrText[] = "31234567890123456789";
generateQRCode(qrText);
//Serial.println(qrText[20]);
for (int i = 0; i < 20; i++) {
Serial.print(qrText[i]);
}
delay(1000);
////////////////////////////////////
/////////////////////////ENTER PIN
///////////////////////////////////////
/////////////////////////HOLD LOOP
char key = waitForKeypadInput();
Serial.print("You pressed: ");
Serial.println(key);
delay(100);
lcd.setCursor(0, 1);
lcd.print("Enter PIN:");
digitalWrite(16,LOW);
delay(1000);
}
else{
digitalWrite(15,HIGH);
delay(500);
digitalWrite(15,LOW);
delay(500);
Serial.println("Press Again");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press again");
}
}
/////////////////////////QR FUNCTION
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;
// Calculate vertical shift
int shiftY = (TFT_HEIGHT - qrcode.size * scale) / 2;
// 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);
}
}
}
}
////////////////////Random NUMBER GENERATOR
void splitString(String str, char arr[]) {
int length = str.length();
for (int i = 0; i < length; i++) {
arr[i] = str.charAt(i);
}
}
void printCharArray(char arr[], int size) {
Serial.println("Character Array:");
for (int i = 0; i < size; i++) {
Serial.print(arr[i]);
Serial.print(" ");
}
Serial.println();
}
/////////////////////////////////////
//////////////////////////////HOLD LOOP
char waitForKeypadInput() {
char key = NO_KEY;
while (key == NO_KEY) {
key = keypad.getKey();
}
return key;
}
*/
#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;
// Button pins
const int button1 = 25;
const int button2 = 26;
// 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;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
pinMode(redLed, OUTPUT);
pinMode(blueLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, 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);
if ((millis() - lastDebounceTime) > debounceDelay) {
if (button1Read == HIGH && !button1State) {
button1State = true;
button1Function();
lastDebounceTime = millis();
} else if (button2Read == HIGH && !button2State) {
button2State = true;
button2Function();
lastDebounceTime = millis();
}
}
if (button1Read == LOW) button1State = false;
if (button2Read == LOW) button2State = 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 qrText[] = "A1234567890123456789";
generateQRCode(qrText);
//////////////////////////////////////////////////////
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 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);
}
}
}
}