Hi, i want suggestion to improve my code. I want to add manual mode, where user can call each washing function at a time with digital pin.
#include <ESP8266WiFi.h>
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <SPI.h>
WidgetLCD lcd(V1);
BlynkTimer timer;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "XXXXXXXXXXXXXXX";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "XXXXXXXXX";
char pass[] = "XXXXXXXXX";
//-------- Relay State Setups -------//
#define CLOCKWISE HIGH
#define ANTICLOCKWISE LOW
#define RELAY_ON LOW
#define RELAY_OFF HIGH
//----------------------------------//
int onoff;
// PORTS //
#define MotorPowerPort D0
#define MototrDirectionPort D1
#define WaterFillValvePort D2
#define WaterDrainValvePort D3
#define button1Pin D4
// GLOBAL PROCESS VARIABLES //
double process_start = 0L;
unsigned long process_duration = 0L;
int motor_status = CLOCKWISE;
int process = 0;
String process_name = "";
int process_max = 14;
long millis_padding = 0;
// ------------------------ //
void setup() {
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
lcd.clear(); //Use it to clear the LCD Widget
lcd.print(4, 0, "Washing"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
pinMode (button1Pin, INPUT);
digitalWrite(button1Pin, HIGH);
delay(1000);
processInit();
}
void loop() {
// put your main code here, to run repeatedly:
Blynk.run();
timer.run();
// stage(); , if i remove this function and call from V1 then it stucks on process == 0
while (digitalRead(button1Pin) == LOW)
{if (doProcess())
{
processReset();
lcd.print(4, 0, "Washing");
lcd.print(4, 1, "Complete");
delay(3000);
}
}
}
void stage(){
if (doProcess())
{
processReset();
lcd.print(4, 0, "Washing");
lcd.print(4, 1, "Complete");
delay(3000);
}
}
BLYNK_WRITE(V2){
int button1Pin = param.asInt();
if(onoff == 1){
//stage(); //after writing value V1== 1 , the code stuck at process==0)
}
}
//------------------PROCESS AND OPERATIONS ---------------------------------//
boolean doProcess() //Return is Completed !
{
if (process == 0)drainWater(1);
else if (process == 1)fillWater(2);
else if (process == 2)doWash(4000, 1, "SOAKING");
else if (process == 3)doSoak(15);
else if (process == 4)doWash(4000, 10, "WASHING");
else if (process == 5)drainWater(2);
else if (process == 6)fillWater(3);
else if (process == 7)doWash(5000, 8, "RINSING");
else if (process == 8)drainWater(3);
else if (process == 9)fillWater(3);
else if (process == 10)doWash(6000, 7, "RINSING");
else if (process == 11)drainWater(4);
else if (process == 12)fillWater(3);
else if (process == 13)doWash(6000, 3, "RINSING");
else if (process == 14)drainWater(4);
else return true;
processUpdate();
return false;
}
double process_pivot = 0;
void processUpdate()
{
double process_current = millis_padded();
if ((process_current - process_start) >= process_duration)
{
process++;
processReset();
delay(1000);
}
else
{
if ((process_current - process_pivot) > 1000) //If past a minimum of one sec
{
//double remain=(process_remain/1000)/60;
process_pivot = process_current;
lcd.clear();
lcd.print(10, 0,process_name);
process_current = (process_current - process_start);
process_current = process_current / 1000;
if (process_current >= 60)
{
process_current = process_current / 60;
lcd.print(0, 0,(int)process_current);
lcd.print(0, 0, "Min");
}
else
{
lcd.print(0, 0,(int)process_current);
lcd.print(2, 1,"Sec");
}
lcd.print(4, 0,"/");
process_current = process_duration / 1000;
if (process_current >= 60)
{
process_current = process_current / 60;
lcd.print(0, 0,(int)process_current);
lcd.print(2, 1,"Min");
}
else
{
lcd.print(0, 0,(int)process_current);
lcd.print(2, 1,"Sec");
}
}
}
}
void processInit()
{
pinMode(MotorPowerPort, OUTPUT);
pinMode(MototrDirectionPort, OUTPUT);
pinMode(WaterFillValvePort, OUTPUT);
pinMode(WaterDrainValvePort, OUTPUT);
pinMode(button1Pin, INPUT_PULLUP);
process_duration = 0;
process = 0;
process_start = millis_padded();
motor_status = CLOCKWISE;
digitalWrite(MotorPowerPort, RELAY_OFF);
digitalWrite(MototrDirectionPort, CLOCKWISE);
digitalWrite(WaterFillValvePort, RELAY_OFF);
digitalWrite(WaterDrainValvePort, RELAY_OFF);
}
void processReset()
{
process_start = millis_padded();
process_duration = 0;
motor_status = CLOCKWISE;
digitalWrite(MotorPowerPort, RELAY_OFF);
digitalWrite(MototrDirectionPort, CLOCKWISE);
digitalWrite(WaterFillValvePort, RELAY_OFF);
digitalWrite(WaterDrainValvePort, RELAY_OFF);
}
void setProcessDuration(int mins)
{
process_duration = mins2millis(mins);
}
//---------------------Process In Detail --------------------------------------//
void doWash(int cycle_delay, int time_mins, String title)
{
process_name = title;
setProcessDuration(time_mins);
if (motor_status == CLOCKWISE)motor_status = ANTICLOCKWISE;
else motor_status = CLOCKWISE;
digitalWrite(MotorPowerPort, RELAY_OFF); //TURN OFF MAIN POWER TO MOTOR
delay(1000);
digitalWrite(MototrDirectionPort, motor_status); //SET MOTOR DIRECTION
delay(1000);
digitalWrite(MotorPowerPort, RELAY_ON); //TURN ON MAIN POWER TO MOTOR
delay(cycle_delay);
}
void fillWater(int time_mins)
{
process_name = "FILLING";
setProcessDuration(time_mins);
digitalWrite(WaterFillValvePort, RELAY_ON);
}
void drainWater(int time_mins)
{
process_name = "DRAINING";
setProcessDuration(time_mins);
digitalWrite(WaterDrainValvePort, RELAY_ON);
}
void doSpin(int time_mins, int DIR)
{
process_name = "DRY";
setProcessDuration(time_mins);
digitalWrite(WaterDrainValvePort, RELAY_ON);
delay(3000);
digitalWrite(MototrDirectionPort, CLOCKWISE);
delay(1000);
digitalWrite(MotorPowerPort, RELAY_ON);
}
void doSoak(int time_mins)
{
process_name = "SOAKING";
setProcessDuration(time_mins);
delay(1000);
//JUST WAIT :)
}
//---------------------------------------------------------------------------------------//
unsigned long mins2millis(int mins)
{
return (((unsigned long)mins) * 60 * 1000);
}
double millis_padded()
{
return (millis() + millis_padding);
}