#include <AccelStepper.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
const byte ROW_NUM = 4;
const byte COLUMN_NUM = 4;
char keys[ROW_NUM][COLUMN_NUM] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
byte pin_rows[ROW_NUM] = { 4, 5, 18, 19 };
byte pin_column[COLUMN_NUM] = { 13, 12, 14, 27 };
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
LiquidCrystal_I2C lcd(0x27, 16, 2);
AccelStepper stepper1(1, 15, 2);
#define LIMIT_SWITCH_PIN 23 // Change this to the actual pin connected to the limit switch
const byte enPin = 4; // EN (enable) pin for A4988 stepper motor driver
bool st1 = true;
bool waitForInput = false;
int count_input = 0;
int deep_rate = 0;
int widthrorate = 0;
String print_data[] = { "", "Dwell Time:", "DIP RATE(mm/min):", "Wait time:", "NO of cycle:", "Substate length(mm)", "Press * to start" };
int set_speed = 200;
bool widthrorate_st = false;
int solution_hight = 0;
int substrate_length = 0;
int no_of_cycle = 0;
int waiting_time = 0;
int distance_dip = 0;
int dwell_time = 0;
int h = 0;
int count_withdraw = 0;
int solution_h = 0;
int dip_rate = 0;
int onoff_switch = 0;
int no_of_cycle_bly = 0;
int substrate_length_bly = 0;
int factor_mm = 1;
bool motor_st = false;
bool withdra_st=false;
int wi=0;
void setup() {
Serial.begin(9600);
pinMode(enPin, OUTPUT);
pinMode(LIMIT_SWITCH_PIN, INPUT_PULLUP);
Wire.begin();
lcd.begin(16, 2);
lcd.setBacklight(255);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Calibrating..");
while (1) {
stepper1.setMaxSpeed(1000);
stepper1.setAcceleration(1000 / 2);
stepper1.moveTo(factor_mm * 20*1000);
stepper1.run();
if (digitalRead(LIMIT_SWITCH_PIN) == 0) {
break;
}
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WELCOME TO DIG");
lcd.setCursor(0, 1);
lcd.print("SOL-GEL SYSTEM");
stepper1.setCurrentPosition(0);
digitalWrite(enPin, HIGH);
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == 'A') {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Solution H. mm");
count_input = 0;
solution_hight = 0;
distance_dip = 0;
dwell_time = 0;
waiting_time = 0;
no_of_cycle = 0;
deep_rate = 0;
substrate_length = 0;
waitForInput = true;
} else if (key == '#') {
lcd.clear();
count_input++;
lcd.setCursor(0, 0);
lcd.print(print_data[count_input]);
if (count_input >= 8) {
count_input = 0;
waitForInput = false;
}
} else if (key == '*') {
float cm = distance_dip / (factor_mm);
digitalWrite(enPin, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Dipping...");
lcd.setCursor(0, 1);
lcd.print(cm);
lcd.print("mm");
dip_function(deep_rate);
count_input = 0;
st1 = false;
}
}
if (waitForInput && isDigit(key)) {
if (count_input == 0) {
solution_hight = put_func(solution_hight, key);
distance_dip = (200 - solution_hight) * factor_mm;
}
if (count_input == 1) {
dwell_time = put_func(dwell_time, key);
}
if (count_input == 2) {
deep_rate = put_func(deep_rate, key);
}
if (count_input == 3) {
waiting_time = put_func(waiting_time, key);
}
if (count_input == 4) {
no_of_cycle = put_func(no_of_cycle, key);
}
if (count_input == 5) {
substrate_length = put_func(substrate_length, key);
}
}
while (!st1) {
stepper1.moveTo(-distance_dip);
stepper1.runToPosition();
for (int i = dwell_time; i >= 0; i--) {
lcd.setCursor(0, 0);
lcd.print("Dwell Time:");
lcd.setCursor(0, 1);
lcd.print(i);
lcd.print(" sec. ");
delay(1000);
if (i == 0) {
widthrorate_st = true;
h = stepper1.currentPosition();
}
}
while (widthrorate_st) {
count_withdraw++;
if (count_withdraw > no_of_cycle) {
if(withdra_st)
{
digitalWrite(enPin, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Withdrawing...");
lcd.setCursor(0, 1);
lcd.print(wi);
dip_function(wi);
withdra_st=false;
}
stepper1.moveTo(0);
stepper1.runToPosition();
if (stepper1.currentPosition() == 0) {
digitalWrite(enPin, HIGH);
stepper1.setCurrentPosition(0);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Thank you.");
lcd.setCursor(0, 1);
lcd.print("Press * to start");
count_input = 0;
widthrorate_st = false;
st1 = true;
count_withdraw = 0;
break;
}
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Withdrawing...");
digitalWrite(enPin, LOW);
stepper1.moveTo(h + substrate_length * factor_mm);
stepper1.runToPosition();
lcd.clear();
for (int i = waiting_time; i >= 0; i--) {
digitalWrite(enPin, HIGH);
lcd.setCursor(0, 0);
lcd.print("Wait time..");
lcd.setCursor(0, 1);
lcd.print(i);
lcd.print(" sec. ");
delay(1000);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Dipping...");
digitalWrite(enPin, LOW);
stepper1.moveTo(h);
stepper1.runToPosition();
lcd.clear();
for (int i = dwell_time; i >= 0; i--) {
digitalWrite(enPin, HIGH);
lcd.setCursor(0, 0);
lcd.print("Dwell Time:");
lcd.setCursor(0, 1);
lcd.print(i);
lcd.print(" sec. ");
delay(1000);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("No of cycle:");
lcd.setCursor(0, 1);
lcd.print(count_withdraw + 1);
digitalWrite(enPin, LOW);
delay(1000);
}
}
}
void dip_function(int a) {
int ai = abs((a * factor_mm) / 60);
stepper1.setMaxSpeed(ai);
stepper1.setAcceleration(ai / 2);
}
int put_func(int &variable, char key) {
variable = variable * 10 + (key - '0');
lcd.setCursor(0, 1);
lcd.print(variable);
return variable;
}