/* ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░ Gen3 Portionator Version 1.3.3 ░
░ Last modification: 2023-12-13 ░
░ Description: Initial Process to empty hoses ░
░ Main Potting Process (reverse mode) ░
░ Using HIGH LEVEL TRIGGER Relay ░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
*/
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
//#include "DFRobot_LCD.h"
// SDA for LCD Pin 20
// SCL for LCD Pin 21
#define Motor_Step 2 // Output pin for Motor - left side , 24v
#define Motor_Direction 3
#define Motor_Enable 4
#define Motor_OptoSwitch_UP 5 // input pin fot Optical switch (up)for left motor
#define Motor_OptoSwitch_DOWN 6 // input pin fot Optical switch (DOWN)for left motor
//#define Motor_Right_Step 7 // Output pin for Motor - Right side , 24v
//#define Motor_Right_Direction 8
//#define Motor_Right_Enable 9
//#define Motor_Right_OptoSwitch_UP 10 // input pin fot Optical switch (up)for Right motor
//#define Motor_Right_OptoSwitch_DOWN 11 // input pin fot Optical switch (DOWN)for Right motor
#define Button_1 12 // Left button
#define Button_2 13 // Right button
#define AirValve_AB 35 // Output pin for 2xPotting Air Valves - Left - potting type A and B - Number 0 , 24v
#define PottingValve_AB1 32 // Output pin for 2xPotting Valves - Left - potting type A and B - Number 1 , 24v
#define PottingValve_AB2 33 // Output pin for 2xPotting Valves - Left - potting type A and B - Number 2 , 24v
#define PottingValve_AB3 34 // Output pin for 2xPotting Valves - Left - potting type A and B - Number 3 , 24v
#define En_PotValve_L 53 // enable flag for potting valve
LiquidCrystal_I2C lcd(0x27, 16, 2); // or 0x3f
//************************* Variables ***************************
int adcValue; // Define a variable to save ADC value
float Turns_; // Define a variable to save the calculated voltage value
unsigned long startMillis, currentMillis;
//******************** Function prototype ***********************
void Activating_Valve(String Valve_Name, int Delay_Time);
//bool Move_Motor(String Motor_Side);
//bool Move_Motor( float Quantity);
//bool Move_Motor( String ProcessType); //for example: PRELIMINARY PROCESS
float Move_Motor( String Direction, float TurnsValue); // Motor move up/down with Turns control
void Move_Motor( String Direction); //Motor fully move up/down
//bool OptoSensor_Detect(String Motor_Side);
//int UltraSonicSensor_Value();
int Convert_Turns_To_Steps(float Turn);
void Initialize_Process(int PottingValve);
void MainPotting_Process();
//***************************************************************
void setup() {
Serial.begin(9600);
lcd.init();
// lcd.setPWM(REG_ONLY, 100);
pinMode(Button_1, INPUT);
pinMode(Button_2, INPUT);
pinMode( Motor_Step, OUTPUT);
pinMode( Motor_Direction, OUTPUT);
pinMode( Motor_Enable, OUTPUT);
pinMode( Motor_OptoSwitch_UP, INPUT);
pinMode( Motor_OptoSwitch_DOWN, INPUT);
pinMode( AirValve_AB, OUTPUT); // Air valve
pinMode( PottingValve_AB1, OUTPUT);
pinMode( PottingValve_AB2, OUTPUT);
pinMode( PottingValve_AB3, OUTPUT);
digitalWrite(AirValve_AB, LOW); // Air valve (NC) CLOSE
digitalWrite(PottingValve_AB1, LOW); // Potting valve (NC) CLOSE
digitalWrite(PottingValve_AB2, LOW);
digitalWrite(PottingValve_AB3, LOW);
lcd.setCursor(0, 0);
lcd.print(" Version: 1.3.3 ");
delay(3000);
lcd.setCursor(0, 0);
lcd.print("<1> Initializing");
lcd.setCursor(0, 1);
lcd.print("<2> Main Potting");
}
void loop() {
adcValue = analogRead(A7); // Convert analog of pin A0 to digital
Turns_ = adcValue * (14.1 / 1023.0); // Calculate voltage according to digital
delay(50);
if (digitalRead(Button_1)) {
delay(10); // Prevent bouncing
if (digitalRead(Button_1)) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Valve #1,2,3...");
Initialize_Process(PottingValve_AB1); // initialize process for potting valves A1-B1
// lcd.setCursor(0, 0);
// lcd.print("Valve #2 ...");
// Initialize_Process(PottingValve_AB2); // initialize process for potting valves A2-B2
// lcd.setCursor(0, 0);
// lcd.print("Valve #3 ...");
// Initialize_Process(PottingValve_AB3); // initialize process for potting valves A3-B3
}
}
if (digitalRead(Button_2)) {
delay(10);
if (digitalRead(Button_2)) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Potting is in ");
lcd.setCursor(0, 1);
lcd.print("process . . . #");
MainPotting_Process();
}
}
//lcd.clear();
lcd.setCursor(0, 0);
lcd.print("<1> Initializing");
lcd.setCursor(0, 1);
lcd.print("<2> Main Potting");
}
//
// lcd.print("<1> Regular Test");
// lcd.setCursor(0, 1);
// lcd.print("<2> Extra Tests");
// int Operator_Btn_Press = 0;
// while ((Operator_Btn_Press != 1) and (Operator_Btn_Press != 2)) {
// if (digitalRead(Button_1)) // regular
// Operator_Btn_Press = 1;
// if (digitalRead(Button_2)) // extra
// Operator_Btn_Press = 2;
// }
// if (Operator_Btn_Press == 1)
// Move_Motor( Turns_); // Regular test
// else
// {
// lcd.clear();
// lcd.setCursor(0, 0);
// lcd.print("<1> Move +2-1-1"); // Extra 1
// lcd.setCursor(0, 1);
// lcd.print("<2> Move +4-2-2"); // Extra 2
// int Operator_Btn_Press = 0;
// while ((Operator_Btn_Press != 1) and (Operator_Btn_Press != 2)) {
// if (digitalRead(Button_1)) // Extra 1
// Operator_Btn_Press = 1;
// if (digitalRead(Button_2)) // Extra 2
// Operator_Btn_Press = 2;
// }
// if (Operator_Btn_Press == 1)
// Move_Motor( 1.0); // Extra 1 test
// else
// {
// Move_Motor( 2.0); // Extra 2 test
// }
// }
//
// }
// if (digitalRead(Button_2)) {
// Move_Motor( "Preliminary");//PRELIMINARY PROCESS
//------------------- Motor fully move up/down ---------------------------
void Move_Motor( String Direction) {
if (Direction == "DOWN") {
while (!digitalRead(Motor_OptoSwitch_DOWN)) {
digitalWrite(Motor_Direction, HIGH); // Enables the motor to move in DOWN direction
digitalWrite(Motor_Step, HIGH);
delayMicroseconds(350);
digitalWrite(Motor_Step, LOW);
delayMicroseconds(350);
}
lcd.setCursor(0, 1);
lcd.print("Motor fully Down");
}
if (Direction == "UP") {
while (!digitalRead(Motor_OptoSwitch_UP)) {
digitalWrite(Motor_Direction, LOW); // Enables the motor to move in UP direction
digitalWrite(Motor_Step, HIGH);
delayMicroseconds(350);
digitalWrite(Motor_Step, LOW);
delayMicroseconds(350);
}
lcd.setCursor(0, 1);
lcd.print("Motor fully UP ");
}
}
//------------------- Motor move up/down with quantity control ---------------------------
float Move_Motor( String Direction, float TurnsValue) {
int Step_Count = 0;
int Moving_Steps = Convert_Turns_To_Steps(TurnsValue);
Serial.println(Moving_Steps);
if(Direction=="UP"){
for (int i = 0 ; i < Moving_Steps ; i++) {
if (digitalRead(Motor_OptoSwitch_UP)) {
Serial.println("Attention! Moving Stopped in " + String((Step_Count) / 400.0) + " turns");
break;
}
digitalWrite(Motor_Direction, LOW); // Enables the motor to move in UP direction
digitalWrite(Motor_Step, HIGH);
delayMicroseconds(350);//(1000);
digitalWrite(Motor_Step, LOW);
delayMicroseconds(350);//(1000);
Step_Count += 1;
}
}
if(Direction=="DOWN"){
for (int i = 0 ; i < Moving_Steps ; i++) {
if (digitalRead(Motor_OptoSwitch_DOWN)) {
Serial.println("Attention! Moving Stopped in " + String((Step_Count) / 400.0) + " turns");
break;
}
digitalWrite(Motor_Direction, HIGH); // Enables the motor to move in DOWN direction
digitalWrite(Motor_Step, HIGH);
delayMicroseconds(350);
digitalWrite(Motor_Step, LOW);
delayMicroseconds(350);
Step_Count += 1;
}
}
return Step_Count / 400.0 ;
}
/********************* Initialize Process *************************
1- Motor move down full --> motor delay
2- Air valve A-B OPEN --> air valve delay --> CLOSE
3- Potting Valve A1-B1 OPEN
4- Motor move UP full
5- Potting Valve A1-B1 CLOSE
6- Air valve A-B OPEN --> air valve delay --> CLOSE
7- Repeat the process for A2-B2 , A3-B3
*/
void Initialize_Process(int PottingValve) {
int Motor_delay = 8000;
int AirValve_delay = 2000;
//delay(Motor_delay - 2000);
digitalWrite(PottingValve_AB1, LOW); // Potting valve (NC) CLOSE
digitalWrite(PottingValve_AB2, LOW); // Potting valve (NC) CLOSE
digitalWrite(PottingValve_AB3, LOW); // Potting valve (NC) CLOSE
// delay(AirValve_delay);
// digitalWrite(AirValve_AB, HIGH); // Air valve (NC) CLOSE
// digitalWrite(PottingValve, LOW); // OPEN
Move_Motor("DOWN");
//delay(Motor_delay + 4000);
digitalWrite(PottingValve_AB1, HIGH); // Potting valve OPEN
digitalWrite(PottingValve_AB2, HIGH); // Potting valve OPEN
digitalWrite(PottingValve_AB3, HIGH); // Potting valve OPEN
Move_Motor("UP");
digitalWrite(PottingValve_AB1, LOW); // Potting valve (NC) CLOSE
digitalWrite(PottingValve_AB2, LOW); // Potting valve (NC) CLOSE
digitalWrite(PottingValve_AB3, LOW); // Potting valve (NC) CLOSE
// digitalWrite(AirValve_AB, LOW); // Air valve (NC) OPEN
// delay(AirValve_delay);
// digitalWrite(AirValve_AB, HIGH); // Air valve (NC) CLOSE
}
/********************* Main Potting Process *************************
1- Motor move down full --> motor delay
2- Potting Valve A1-B1 OPEN
3- Air valve A-B OP++++++++++++++++++++++++EN --> air valve delay --> CLOSE
4- Motor move UP N-1.1 turns --> motor delay
5- Potting Valve A1-B1 CLOSE
6- Potting Valve A2-B2 OPEN
7- Motor move UP N-1.2 turns --> motor delay
8- Potting Valve A2-B2 CLOSE
9- Potting Valve A3-B3 OPEN
10- Motor move UP N-1.3 turns --> motor delay
11- Potting Valve A2-B2 CLOSE
12- Air valve A-B OPEN --> air valve delay --> CLOSE
*/
void MainPotting_Process() {
int Motor_delay = 12000;
int AirValve_delay = 2000;
float Turns_1 = 8.0;//7.650; //4.0; //4.68;
float Turns_2 = 4.5;//3.275;//.4.0; //4.68;
float Turns_3 = 0.5;//2.575;//4.0; //4.68;
// lcd.setCursor(15, 1);
// lcd.print("1");
digitalWrite(PottingValve_AB1, LOW); // Potting valve CLOSE
digitalWrite(PottingValve_AB2, LOW); // Potting valve CLOSE
digitalWrite(PottingValve_AB3, LOW); // Potting valve CLOSE
Move_Motor("DOWN");
// Move_Motor("DOWN", (Turns_1 + Turns_2 + Turns_3)); // move motor fully down.
//delay(Motor_delay - 9000);
digitalWrite(PottingValve_AB1, HIGH); // Potting valve OPEN
digitalWrite(PottingValve_AB2, HIGH); // Potting valve OPEN
digitalWrite(PottingValve_AB3, HIGH); // Potting valve OPEN
Move_Motor("UP", Turns_1); // move motor N-1.1 Turns up
digitalWrite(PottingValve_AB1, LOW); // Potting valve CLOSE
Move_Motor("UP", Turns_3); // move motor N-1.1 Turns up
digitalWrite(PottingValve_AB3, LOW); // Potting valve CLOSE
Move_Motor("UP", Turns_2); // move motor N-1.1 Turns up
delay(Motor_delay - 4000);
digitalWrite(PottingValve_AB2, LOW); // Potting valve CLOSE
lcd.setCursor(0, 1);
lcd.print("process Ending. ");
}
//********************* Convert Quantity to Steps *************************
int Convert_Turns_To_Steps(float Turn) {
// full sylinder is 7.5cm
// 50mm is 10 turn
// 1 turn is 400 steps
int Step = Turn * 400.0;
return Step ;
}
//********************* *************************