#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); //Device Address,N Charecter, LCD Lines
//SDA-> A4 SCL-> A5
int sensor_pin = A0;
int std_value = 0;
int sw_set = 2; //Connect to Switch Set
int sw_up = 3; //Connect to Switch UP
int sw_dwn = 4; //Connect to Switch Down
int sw_ent = 5; //Connect to Switch Enter
int purge = 6; //For Solenoid
int feed = 7; //For Solenoid
int bzr = 8; //Buzzer
int max_limit = 70;
int min_limit = 10;
float pressure_value = 0;
//int sensor_Value = 0;
int cal_factor = 0;
int set_mode = 0;
int threshold = 2;
int unit_mode = 1;
int inf_delay = 1000;
int key_delay = 300;
int run_mode = 0; // run_mode=0 Standby
int set_value = 0;
String disp_string[4] = { " Tyre Inflator", "Set Pressure", "-", "-" };
String unit_label[3] = { "BAR", "Kg/cm2", "PSI" };
void setup() {
// declare the ledPin as an OUTPUT:
//pinMode(ledPin, OUTPUT);
pinMode(purge, OUTPUT);
pinMode(feed, OUTPUT);
pinMode(bzr, OUTPUT);
pinMode(sw_up, INPUT_PULLUP);
pinMode(sw_dwn, INPUT_PULLUP);
pinMode(sw_set, INPUT_PULLUP);
pinMode(sw_ent, INPUT_PULLUP);
digitalWrite(purge, HIGH);
digitalWrite(feed, HIGH);
digitalWrite(bzr, HIGH);
Serial.begin(9600);
init_settings();
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("Welcome");
lcd.setCursor(4, 1);
lcd.print("Automatic");
lcd.setCursor(2, 2);
lcd.print("Tyre Inflator");
delay(3000);
lcd.clear();
disp_string[1] = "Set PV:" + String(std_value) + unit_label[unit_mode];
disp_string[2] = "Press Enter";
disp_string[3] = "To Start Inflating";
display();
}
void loop() {
pressure_value = read_sensor(unit_mode, cal_factor); //Mode,Calibration Factor
if (digitalRead(sw_set) == LOW) {
delay(key_delay);
if (run_mode > 3) { //0=Standby,1=Working, 2=Pressure Setting,3=Unit Setting,4=Calibration
run_mode = 0;
} else {
run_mode += 1;
}
lcd.clear();
display();
}
switch (run_mode) {
case 0:
disp_string[0] = "Stand By";
//standby();
break;
case 1:
disp_string[0] = "Inflate";
// inflate(std_value);
break;
case 2: //Pressure Setting
disp_string[0] = "Pressure Setting..";
set_value = std_value;
max_limit = 100;
min_limit = 10;
break;
case 3: //Unit Setting
disp_string[0] = "Unit Setting...";
disp_string[1] = "Mes. Unit:" + unit_label[unit_mode];
disp_string[2] = "____________________";
disp_string[3] = "Press SET To Stop ";
set_value = unit_mode;
max_limit = 3;
min_limit = 0;
break;
case 4: //Sensor Calibration
disp_string[0] = "Sensor Calibration";
int x = analogRead(sensor_pin); //Read ADC Value
pressure_value = read_sensor(unit_mode, cal_factor);
set_value = cal_factor;
max_limit = 25;
min_limit = 0;
disp_string[1] = "Cal. Factor:" + String(cal_factor) + " ";
disp_string[2] = "ADC Value=" + String(x);
disp_string[3] = "PV=" + String(pressure_value, 2) + unit_label[unit_mode];
break;
}
if (digitalRead(sw_up) == LOW) {
delay(key_delay);
if (set_value < max_limit) {
std_value += 1;
init_settings();
}
lcd.clear();
display();
}
if (digitalRead(sw_dwn) == LOW) {
delay(key_delay);
if (std_value > min_limit) {
std_value -= 1;
init_settings();
}
lcd.clear();
display();
}
if (digitalRead(sw_ent) == LOW) {
delay(key_delay);
run_mode = 1;
disp_string[0] = "Inflate";
disp_string[1] = " ";
disp_string[2] = " ";
disp_string[3] = " ";
init_settings();
lcd.clear();
display();
}
// lcd.clear();
// display();
}
void standby() {
// set_mode = 5;
digitalWrite(purge, HIGH);
digitalWrite(feed, HIGH);
lcd.clear();
disp_string[1] = "Set PV:" + String(std_value) + unit_label[unit_mode];
disp_string[2] = "Press Enter";
disp_string[3] = "To Start Inflating";
//display();
}
void inflate(float preset_pv) {
pressure_value = read_sensor(unit_mode, cal_factor);
if (pressure_value < (preset_pv + threshold)) {
if (pressure_value < preset_pv) {
disp_string[0] = "______Inflating_____";
disp_string[1] = "SET PV=" + String(preset_pv, 2) + unit_label[unit_mode];
disp_string[2] = "Tyre PV=" + String(pressure_value, 2) + unit_label[unit_mode];
disp_string[3] = "Press SET To Stop ";
digitalWrite(purge, HIGH);
digitalWrite(feed, LOW);
display();
} else {
disp_string[0] = "Tyre Inflator";
disp_string[1] = "Inflation Competed ";
digitalWrite(purge, HIGH);
digitalWrite(feed, HIGH);
lcd.clear();
display();
delay(2000);
run_mode = 0;
}
}
if (pressure_value > preset_pv) {
disp_string[0] = "________Purging_____";
disp_string[1] = "SET PV=" + String(preset_pv, 2) + unit_label[unit_mode];
disp_string[2] = "Tyre PV=" + String(pressure_value, 2) + unit_label[unit_mode];
disp_string[3] = "Press SET To Stop ";
digitalWrite(purge, LOW);
digitalWrite(feed, HIGH);
display();
} else {
disp_string[0] = "Tyre Inflator";
disp_string[1] = "Inflation Competed ";
digitalWrite(purge, HIGH);
digitalWrite(feed, HIGH);
lcd.clear();
display();
delay(2000);
run_mode = 0;
}
}
void display() {
for (int i = 0; i < 4; i++) {
lcd.setCursor(0, i);
delay(1);
lcd.print(disp_string[i]);
//Serial.println("L" + String(i) + ": " + disp_string[i]);
}
//delay(1000);
}
void save_settings(int address, int value) {
EEPROM.write(address, value);
EEPROM.update(address, value);
}
int load_settings(int address) {
int x;
x = EEPROM.read(address);
return x;
}
void init_settings() {
cal_factor = float(load_settings(2));
if (cal_factor > 50) {
save_settings(2, 1);
}
delay(1);
unit_mode = load_settings(3);
if (unit_mode > 3) {
unit_mode = 1;
save_settings(3, 1);
}
delay(1);
std_value = load_settings(10 + unit_mode);
switch (unit_mode) {
case 0: //BAR
//scale_span = 16;
if (std_value > 6) {
std_value = 5;
save_settings((unit_mode + 10), std_value);
}
break;
case 1: //Kg/cm2
// scale_span = 16.316;
if (std_value > 6) {
std_value = 5;
save_settings((unit_mode + 10), std_value);
}
break;
case 2: //PSI
//scale_span = 232.06038;
if (std_value > 35) {
std_value = 35;
save_settings((unit_mode + 10), std_value);
break;
}
}
}
float read_sensor(int rmode, float calval) {
float min_val = 100 + calval;
float max_val = 921 + calval;
float scale_span = 16.90;
switch (rmode) {
case 0: //BAR
scale_span = 16;
break;
case 1: //Kg/cm2
scale_span = 16.316;
break;
case 2: //PSI
scale_span = 232.06038;
break;
}
float pv = analogRead(sensor_pin);
pv = ((pv - min_val) * scale_span) / (max_val - min_val);
return pv;
}