#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Arduino.h> // Include Arduino.h for dtostrf function
#define disk1 0x50 //Address of 24LC512 eeprom chip
// Define LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD I2C address and dimensions
byte value;
const int buzz = 13, change_switch = 5,mode_switch =6;
bool flag = 0;
int mode = 0, MAX_LENGTH = 32, address = 0;
long timemillis;
//char incoming[50];
String barCode, subBarcode, vender, m, y, SerialBarcode;
String TMLCURRVBarcode;
String TMLTAMORBarcode;
String APTIVCURRVBarcode;
String APTIVTAMORBarcode;
int count;
int scen_flag = 0;
String TML_CURRV = "005471162124010CD63807"; //mode = 1 MMYY000000 TOTAL DIGIT 32
String APTIV_CURRV = "28850239"; //mode = 1 DDMMYY0000 TOTAL DIGIT 18
String TML_TAMOR = "005472162124010CD63807"; //mode = 0 MMYY000000 TOTAL DIGIT 32
String APTIV_TAMOR = "28850240"; //mode = 0 DDMMYY0000 TOTAL DIGIT 18
int counter = 0;
int compare_flag = 0, set_flag = 0;
long previousmillis;
void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data )
{
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.write(data);
Wire.endTransmission();
delay(5);
}
byte readEEPROM(int deviceaddress, unsigned int eeaddress )
{
byte rdata = 0xFF;
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress, 1);
if (Wire.available()) rdata = Wire.read();
return rdata;
}
void buzzy(int del)
{
digitalWrite(buzz, HIGH); delay(del); digitalWrite(buzz, LOW); delay(del);
}
void gpio_init()
{
pinMode(change_switch, INPUT);
pinMode(mode_switch, INPUT);
pinMode(buzz, OUTPUT);
Serial.begin(9600);
Serial.println(F("Started"));
}
void lcd_init()
{
lcd.init();
//lcd.init();
lcd.backlight();
// Print title message to the LCD.
lcd.print(F(" Jay Switches ")); /* Write string on 1st line of LCD*/
lcd.setCursor(0, 1); /* Go to 2nd line*/
lcd.print(F("(India) Pvt Ltd ")); /* Write string on 2nd line*/
}
void LCD_Machine_Title()
{
lcd.setCursor(0, 0); /* Go to 2nd line*/
lcd.print(F(" BAR-CODE ")); /* Write string on 1st line of LCD*/
lcd.setCursor(0, 1); /* Go to 2nd line*/
lcd.print(F("BATTERY CHECKING ")); /* Write string on 2nd line*/
}
void Display_Mode()
{
if(digitalRead(mode_switch)==HIGH)
{
mode =1;
}
else
{
mode =0;
}
if (mode == 0)
{
lcd.setCursor(0, 0);
lcd.print(F(" *** TAMOR *** "));
}
else
{
lcd.setCursor(0, 0);
lcd.print(F(" *** CARRV *** "));
}
}
void compareString()
{
if (mode == 0)
{
if (scen_flag == 0)
{
if (TMLTAMORBarcode.equals(TML_TAMOR))
{
lcd.setCursor(0, 1);
lcd.print(" DETECTED ");
digitalWrite(buzz, HIGH);delay(500);digitalWrite(buzz, HIGH);
}
else
{
lcd.setCursor(0, 1);
lcd.print(" WRONG BARCODE ");
while (1)
{
buzzy(100);
}
}
}
else if (scen_flag == 1)
{
if (APTIVTAMORBarcode.equals(APTIV_TAMOR))
{
lcd.setCursor(0, 1);
lcd.print(" DETECTED ");
digitalWrite(buzz, HIGH);delay(500);digitalWrite(buzz, HIGH);
}
else
{
lcd.setCursor(0, 1);
lcd.print(" WRONG BARCODE ");
while (1)
{
buzzy(100);
}
}
}
}
//----------------------------------------------------------
if (mode == 1)
{
if (scen_flag == 0)
{
if (TMLCURRVBarcode.equals(TML_CURRV))
{
lcd.setCursor(0, 1);
lcd.print(" DETECTED ");
digitalWrite(buzz, HIGH);delay(500);digitalWrite(buzz, HIGH);
}
else
{
lcd.setCursor(0, 1);
lcd.print(" WRONG BARCODE ");
while (1)
{
buzzy(100);
}
}
}
else if (scen_flag == 1)
{
if (APTIVCURRVBarcode.equals(APTIV_CURRV))
{
lcd.setCursor(0, 1);
lcd.print(" DETECTED ");
digitalWrite(buzz, HIGH);delay(500);digitalWrite(buzz, HIGH);
}
else
{
lcd.setCursor(0, 1);
lcd.print(" WRONG BARCODE ");
while (1)
{
buzzy(100);
}
}
}
}
}
void setup()
{
lcd_init();
delay(500);
gpio_init();
LCD_Machine_Title();
delay(500);
buzzy(1000);
// Clear any data if in buffer
while (Serial.available()) Serial.read();
lcd.clear();
Display_Mode(); //Display_dash();
lcd.setCursor(0, 1);
lcd.print("SECN TML QRCODE ");
mode = 0;
}
void loop()
{
Display_Mode();
}
void serialEvent()
{
barCode = ""; // Clear the string
if (Serial.available() > 0)
{
barCode = Serial.readStringUntil('\n');
Serial.println(barCode.length());
Serial.println(barCode);
}
if (scen_flag == 0)
{
// Valid String length
if (barCode.length() == 32)
{
if (mode == 0) {
TMLTAMORBarcode = barCode.substring(0, 22);
Serial.println(TMLTAMORBarcode);
}
else if (mode == 1) {
TMLCURRVBarcode = barCode.substring(0, 22);
Serial.println(TMLCURRVBarcode);
}
SerialBarcode = barCode.substring(26, 32);
Serial.println(SerialBarcode);
compareString();
scen_flag = 1;
lcd.setCursor(0, 1);
lcd.print("SECN APTIV CODE ");
/*count = SerialBarcode.toInt();
if (readEEPROM(disk1, count) > 0)
{
Serial.println("Duplicacy");
lcd.setCursor(0, 1);
lcd.print("DUPLICACY DETECT");
while (1)
{
buzzy(100);
}
}
else
{
writeEEPROM(disk1, count, 1);
compareString();
scen_flag = 1;
}*/
}
else
{
// Serial.println("Invalid");
lcd.setCursor(0, 1);
lcd.print("INVALID BARCODE ");
while (1)
{
buzzy(100);
}
}
}
//----------------------------------------------------
else if (scen_flag == 1)
{
// Valid String length
if (barCode.length() == 18)
{
if (mode == 0) {
APTIVTAMORBarcode = barCode.substring(0, 8);
Serial.println(APTIVTAMORBarcode);
}
else if (mode == 1) {
APTIVCURRVBarcode = barCode.substring(0, 8);
Serial.println(APTIVCURRVBarcode);
}
compareString();
scen_flag = 0;
lcd.setCursor(0, 1);
lcd.print("SECN TML QRCODE ");
}
else
{
lcd.setCursor(0, 1);
lcd.print("INVALID BARCODE ");
while (1)
{
buzzy(100);
}
}
}
}