#define BLYNK_PRINT Serial
/* Fill in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "TMPL6koajCCCL"
#define BLYNK_TEMPLATE_NAME "Dipp counting"
#define BLYNK_AUTH_TOKEN "Kzwob0KRctGjFTO7iKTsGradBD-PPm8q"
#include <AccelStepper.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
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);
BLYNK_CONNECTED() {
Blynk.syncVirtual(V0);
Blynk.syncVirtual(V1);
Blynk.syncVirtual(V2);
Blynk.syncVirtual(V3);
Blynk.syncVirtual(V4);
}
unsigned long st = 0;
unsigned long st_time = 0;
bool st1 = true;
bool waitForInput = false;
int count_input = 0;
int deeping_length = 0;
int deep_rate = 0;
int widthrorate = 0;
String print_data[] = { "", "Dwell Time:", "DIP RATE(mm/s):", "Wait time:", "NO of cycle:", "Substate length(mm)", "Press * to start" };
int withdraw_rate = 0;
int holding_time = 0;
int deeping_rate = 0;
bool onoff = false;
int set_speed = 100;
bool state = false;
int count_time = 0;
bool dipping_rate_st = false;
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;
const byte LIMIT_SWITCH_PIN = 23; // Replace with the actual pin connected to the limit switch
void IRAM_ATTR toggleLED()
{
stepper1.stop();
}
BLYNK_WRITE(V0) {
solution_h = param.asInt();
}
BLYNK_WRITE(V1) {
dip_rate = param.asInt();
}
BLYNK_WRITE(V2) {
onoff_switch = param.asInt();
}
BLYNK_WRITE(V3) {
no_of_cycle_bly = param.asInt();
}
BLYNK_WRITE(V4) {
substrate_length_bly = param.asInt();
}
void setup() {
Serial.begin(115200);
Wire.begin();
lcd.begin(16, 2);
lcd.setBacklight(255);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WELCOME TO DIG");
lcd.setCursor(0, 1);
lcd.print("SOL-GEL SYSTEM");
stepper1.setMaxSpeed(set_speed);
stepper1.setAcceleration(set_speed);
stepper1.setCurrentPosition(0);
pinMode(LIMIT_SWITCH_PIN, INPUT_PULLUP);
attachInterrupt(LIMIT_SWITCH_PIN, toggleLED, CHANGE);
stepper1.moveTo(-2000000);
stepper1.runToPosition();
stepper1.setCurrentPosition(0);
Serial.println("OKKK.");
}
void loop() {
char key = keypad.getKey();
if (onoff_switch==1 && st1 == true) {
solution_hight = solution_h;
distance_dip = 20 - solution_hight;
deep_rate = dip_rate;
no_of_cycle = no_of_cycle_bly;
substrate_length = substrate_length_bly;
waiting_time=10;
dwell_time=5;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Dipping...");
lcd.setCursor(0, 1);
lcd.print(distance_dip);
lcd.print("cm");
stepper1.setMaxSpeed(deep_rate);
stepper1.setAcceleration(deep_rate);
Serial.println("Start");
st1 = false;
onoff_switch=0;
}
if (key) {
if (key == 'A') {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Solution H. cm");
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 == 'B') {
stepper1.moveTo(-200);
stepper1.runToPosition();
} else if (key == 'C') {
stepper1.stop();
deeping_length = 0;
waiting_time = 0;
widthrorate = 0;
count_input = 0;
} else if (key == 'D') {
stepper1.moveTo(200);
stepper1.runToPosition();
} 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 == '*') {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Dipping...");
lcd.setCursor(0, 1);
lcd.print(distance_dip);
lcd.print("cm");
stepper1.setMaxSpeed(deep_rate);
stepper1.setAcceleration(deep_rate);
count_input=0;
st1 = false;
}
}
if (waitForInput && isDigit(key)) {
if (count_input == 0) {
solution_hight = put_func(solution_hight, key);
distance_dip = 20 - solution_hight;
}
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) * 10;
}
}
while (!st1) {
stepper1.moveTo(-distance_dip * 10);
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-1) {
stepper1.moveTo(0);
stepper1.runToPosition();
if (stepper1.currentPosition() == 0) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Thank you.");
lcd.setCursor(0, 1);
lcd.print("Press * to start");
count_input=0;
waitForInput = false;
widthrorate_st = false;
st1 = true;
count_withdraw = 0;
break;
}
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Withdrawing...");
stepper1.moveTo(h + substrate_length);
stepper1.runToPosition();
lcd.clear();
for (int i = waiting_time; i >= 0; i--) {
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...");
stepper1.moveTo(h);
stepper1.runToPosition();
lcd.clear();
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);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("No of cycle:");
lcd.setCursor(0, 1);
lcd.print(count_withdraw + 1);
delay(1000);
}
}
}
int put_func(int &variable, char key) {
variable = variable * 10 + (key - '0');
lcd.setCursor(0, 1);
lcd.print(variable);
return variable;
}