#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <EEPROM.h>
#define buzz 13
#define sw1 A0
#define sw2 A1
#define sw3 A2
#define relay A3
int potpin = 0,flag=0; // analog pin used to connect the potentiometer
float val; // variable to read the value from the analog pin
unsigned long previousMillis=20000;
unsigned long previousMillis1=5000;
unsigned long previousMillis2=8000;
long count = 0;
int Swi_count = 0;
int interval_flag = 0;
int stop_flag = 0;
int flag1=0,flag2=0,flag3=0,flag4=0,Step=0;
void(* resetFunc) (void) = 0;
//BUZZER
void buzzy(int del)
{
digitalWrite(buzz,HIGH); delay(del); digitalWrite(buzz,LOW); delay(del);
}
//EEPROM
long EEPROMReadlong(long address)
{
long four = EEPROM.read(address);
long three = EEPROM.read(address + 1);
long two = EEPROM.read(address + 2);
long one = EEPROM.read(address + 3);
return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
}
void EEPROMWritelong(int address, long value) {
byte four = (value & 0xFF);
byte three = ((value >> 8) & 0xFF);
byte two = ((value >> 16) & 0xFF);
byte one = ((value >> 24) & 0xFF);
EEPROM.write(address, four);
EEPROM.write(address + 1, three);
EEPROM.write(address + 2, two);
EEPROM.write(address + 3, one);
}
void gpio_init()
{
pinMode(sw1, INPUT_PULLUP);
pinMode(sw2, INPUT_PULLUP);
pinMode(sw3, INPUT_PULLUP);
pinMode(buzz, OUTPUT); pinMode(relay, OUTPUT);
digitalWrite(relay, HIGH);
}
//LCD Functions
void lcd_init()
{
lcd.init();
lcd.backlight();
// Print title message to the LCD.
lcd.print(" Jay Switches "); /* Write string on 1st line of LCD*/
lcd.setCursor(0,1); /* Go to 2nd line*/
lcd.print("(India) Pvt Ltd "); /* Write string on 2nd line*/
}
// Title of the machine
void LCD_Machine_Title()
{
lcd.setCursor(0,0); /* Go to 2nd line*/
lcd.print(" PSA "); /* Write string on 1st line of LCD*/
lcd.setCursor(0,1); /* Go to 2nd line*/
lcd.print(" COUNTER "); /* Write string on 2nd line*/
}
void setup()
{
unsigned long avg_val=0;
Serial.begin(9600);
Serial.println("System Started");
lcd_init(); gpio_init();
buzzy(100);
delay(1000);
LCD_Machine_Title();
delay(1000);
if(EEPROM.read(0)==1)
{
count = EEPROMReadlong(10);
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Count:");lcd.print(count);lcd.print(" ");
}
void Auto_countinc()
{
if(millis() - previousMillis2 > 8000)
{
count++;
buzzy(50);
lcd.setCursor(0,0);
lcd.print("Count:");lcd.print(count);
EEPROMWritelong(10, count);
Serial.print(" A Count:");Serial.println(count);//Serial.print(">");
if(count>0 && flag == 0)
{
flag = 1;
EEPROM.write(0, 1);
}
Step = 1;
flag2 = 0;
Serial.println("previousMillis2 = 0");
previousMillis2 = 0;
}
}
void loop()
{
// Step 1 Lock press
if(digitalRead(sw1)==LOW)
{
Step = 2;
Serial.println("Step 2 ");
}
// Step 2 UNLock press
if(Step == 2)
{
previousMillis2 = millis();
while(digitalRead(sw2)==HIGH)
{
Auto_countinc();
}
if(digitalRead(sw2)==LOW)
{
Step = 3;
Serial.println("Step 3 ");
}
}
// Step 3 long Lock press
if(Step == 3)
{
previousMillis2 = millis();
while(digitalRead(sw1)==HIGH)
{
Auto_countinc();
}
if(digitalRead(sw1)==LOW)
{
Step = 4;
Serial.println("Step 4 ");
}
}
// Step 4 UNLock press
if(Step == 4)
{
previousMillis2 = millis();
while(digitalRead(sw2)==HIGH)
{
Auto_countinc();
}
if(digitalRead(sw2)==LOW)
{
Step = 5;
Serial.println("Step 5");
}
}
// Step 5 Both press
if(Step == 5)
{
if(digitalRead(sw1)==LOW && digitalRead(sw2)==LOW)
{
count++;
buzzy(50);
lcd.setCursor(0,0);
lcd.print("Count:");lcd.print(count);
EEPROMWritelong(10, count);
Serial.print(" R Count:");Serial.println(count);//Serial.print(">");
while(digitalRead(sw1)==LOW && digitalRead(sw2)==LOW){}
if(count>0 && flag == 0)
{
flag = 1;
EEPROM.write(0, 1);
}
Step = 1;
}
}
// Counter check and alert
if(count > 10)
{
if(interval_flag == 0)
{
previousMillis = millis();
interval_flag = 1;
}
if(millis() - previousMillis > 20000)
{
previousMillis = millis();
buzzy(500);
}
}
if(count > 20)
{
stop_flag = 1;
while(stop_flag == 1)
{
digitalWrite(relay,LOW);
// Wait Serial data for Count reset
buzzy(1000);
if(digitalRead(sw3)==LOW)
{
while(digitalRead(sw3)==LOW){}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Counter Clear ");
lcd.setCursor(0,1);
lcd.print("Process ON ");
stop_flag = 2;
Swi_count = 0;
previousMillis1 = millis();
while(stop_flag == 2)
{
if(digitalRead(sw3)==LOW)
{
delay(500);
Swi_count++;
Serial.print("Swi_Count : ");Serial.println(Swi_count);
}
while(digitalRead(sw3)==LOW){}
if(millis() - previousMillis1 > 5000)
{
if(Swi_count > 3)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Process Done ");
count = 1;
lcd.setCursor(0,1);
lcd.print("Count:");lcd.print(count);
EEPROMWritelong(10, count);
buzzy(50);buzzy(50);buzzy(50);
delay(2000);
resetFunc();
}
else
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Process Fail ");
lcd.setCursor(0,1);
lcd.print("Retry...........");
stop_flag = 1;
}
}
}
}
}
}
}