i have set up terminal widgets and three buttons as switch for runing 3 dosing pumps on/off for priming/filling there lines
i also have 2 data input for selecting the pump to be calibrate and to provide the software with the calibration amount
i would have also data output on display widgets for the test results and the PH
and this code is to make automatic water alkalinity tester based on PH testing
and i just posted the peace of code i was trying to just run the pump for 10 sec. and then will input the amount the water pumped as reference for pump rate in sec.
regarding
the 500ms i do not understand you question i think this priming function will not be active till the priming function active by button to start the pump …
regarding you comment “(which I wouldn’t recommend) then you need to structure your code differently and not attempt to achieve a 10 second time comparison inside a function which executes once every half second.”
could you please give me just an example for that or anything help me to how to structure my code to serve time compare and calibrating pumps… i want to tell you this my first time to write code i spend 2 months reading now every day more than 8 hours and practicing because i feel coding is something great i feel its like art in imagination and nothing stop your imagination …
regarding the full code its not yet complete but for now its complied and working but with some flaws in timing
#include "OTABlynkCredentials.h"
#ifdef ESP8266
#include <BlynkSimpleEsp8266.h>
#elif defined(ESP32)
#include <BlynkSimpleEsp32.h>
#else
#error "Board not found"
#endif
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include "DFRobot_ESP_PH.h"
#include "EEPROM.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#define PH_PIN 35 //the esp gpio data pin number
credentials Credentials;
const int stirrer = 28 ;
const int pumpPin[3] = { 25, 26, 27};
unsigned long Time_intervel = 10L;
float multiplier[3] = { 0, 0, 0}; //ms per ml
float caliberation_1 = 5; //ml per 10sec thos just assumption till finishing calibertion method which still not done in code
float caliberation_2 = 8;
float caliberation_3 = 7;
uint32_t startPump = 0;
uint32_t runCount;
bool pumpRunning = false;
int regect_time_MS = 25000;
float titrant_amount ;
int startPump2;
int runCount2 ;
int stopPump ;
float Alkalinity_dKH;
float Alkalinity_meq;
int start_Regect; // used in testing sequance for pump used to reject the water
int button = 0; //Button Widget set to Switch
int priming_pump_1 = 0; // used as pump flage
int priming_pump_2 = 0;
int priming_pump_3 = 0;
int button_on = 0;
int x; //Correlates Array Positions with Pump Motor Pins
int calibration;
BLYNK_WRITE(V10) {
priming_pump_1 = param.asInt();
}
BLYNK_WRITE(V11) {
priming_pump_2 = param.asInt();
}
BLYNK_WRITE(V12) {
priming_pump_3 = param.asInt();
}
BLYNK_WRITE(V4) {
x = param.asInt() - 1;
}
BLYNK_WRITE(V5) {
DOSEml = param.asFloat();
}
BLYNK_WRITE(V6) {
button = param.asInt();
}
BLYNK_WRITE(V13) {
calibration = param.asInt();
}
BLYNK_WRITE(V14) {
button_on = param.asInt();
}
WidgetRTC rtc;
BlynkTimer timer; // SimpleTimer instance named timer
WidgetTerminal terminal(V8);
//Variables
char auth_token[33];
bool connected_to_internet = 0;
const int Erasing_button = 0;
//Provide credentials for your ESP server
char* esp_ssid = "Egyreef.com";
char* esp_pass = "";
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress insideThermometer;
DFRobot_ESP_PH ph;
float voltage, phValue, temperature = 25;
/*void myTimerEvent()
{
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
temperature = sensors.getTempCByIndex(0); // Save the reading to use later
Serial.print("Temperature is: ");
Serial.println(temperature); // print the reading that we just took
Blynk.virtualWrite(V1, temperature); // and send the same reading to virtual pin 5
}*/
void test(){
digitalWrite(pumpPin[2], HIGH); //taking the probe water saving out to be readyfor next measuring
pumpRunning = true;
start_Regect = millis();
terminal.print("preparing for next test");
if (millis() - start_Regect> regect_time_MS)
{
digitalWrite(pumpPin[0], LOW);
}
*(multiplier) = caliberation_1;// taking 50 ml sample
pumpRunning = true;
digitalWrite(pumpPin[0], HIGH);
startPump = millis();
runCount = 50 * multiplier[0];
terminal.print("taking sample ");
terminal.println("50 ml from tank water");
if (millis() - startPump > runCount)
{
digitalWrite(pumpPin[0], LOW);
terminal.flush();
terminal.print("taking 50ml sample was completed");
pumpRunning = false;
}
*(multiplier+1) = caliberation_2; // titration and caculation of the pump
pumpRunning = true;
digitalWrite(pumpPin[1], HIGH);
digitalWrite(stirrer, HIGH);
startPump2 = millis();
runCount2 = titrant_amount * multiplier[1];
terminal.print("titrating/measuring KH ");
if (phValue<4.4)
{
digitalWrite(pumpPin[1], LOW);
digitalWrite(stirrer, LOW);
terminal.print("titration is finished");
stopPump = millis();
runCount2 = stopPump - startPump ;
}
titrant_amount = runCount2/ multiplier[1] ;
terminal.print("titrant amount is :");
terminal.print(titrant_amount);
Alkalinity_dKH = titrant_amount/50 * 280;
Alkalinity_meq = titrant_amount/50 * 100;
terminal.print("Alkalinty measured is : ");
terminal.print(Alkalinity_dKH);
digitalWrite(pumpPin[2], HIGH);
pumpRunning = true;
start_Regect = millis();
terminal.print("preparing for next test");
if (millis() - start_Regect> regect_time_MS)
{
digitalWrite(pumpPin[0], LOW);
}
terminal.flush();
*(multiplier) = caliberation_1;;
pumpRunning = true;
digitalWrite(pumpPin[0], HIGH);
startPump = millis();
runCount = 50 * multiplier[0];
terminal.print("preparing for next reading ");
if (millis() - startPump > runCount)
{
digitalWrite(pumpPin[0], LOW);
terminal.flush();
pumpRunning = false;
}
}
void myTimerEvent2() // this is called to give the PH of the water
{
static unsigned long timepoint = millis();
if ( millis()- timepoint >= 1000u) //time interval: 1s
{
timepoint = millis();
//voltage = rawPinValue / esp32ADC * esp32Vin
voltage = analogRead(PH_PIN); // read the voltage
phValue = ph.readPH(voltage, temperature); // convert voltage to pH with temperature compensation
Serial.print("pH:");
Serial.println(phValue, 4);
Blynk.virtualWrite(V3, phValue);
}
ph.calibration(voltage, temperature); // calibration process by Serail CMD
}
void calibration_dosing_pumps() // this should run the pump for 10 second and the user should enter the amount the pump dosed and the this amount should be divided /10 and the result should be saved in eeprom
{
if(priming_pump_1==1) // pumpRunning false to assure only one pump running atime
{
Blynk.virtualWrite(V11, 0); // to assure the pin is not active while other pump is runing
Blynk.virtualWrite(V12, 0); // to assure the pin is not active while other pump is runing
digitalWrite(pumpPin[0], HIGH);
static unsigned long timepoint = 0;
if (second()- timepoint > Time_intervel) //time interval: 1s
{
Serial.println(second());
timepoint = second();
Serial.println(second());
digitalWrite(pumpPin[0], LOW);
Blynk.virtualWrite(V10, 0);
priming_pump_1 = 0;
}
}
}
void Priming() // this working as manual toggel on/of for the pumps for me to fill the lines before testing
{
if(priming_pump_1==1 && pumpRunning==false) // pumpRunning false to assure only one pump running atime
{
Blynk.virtualWrite(V11, 0); // to assure the pin is not active while other pump is runing
Blynk.virtualWrite(V12, 0); // to assure the pin is not active while other pump is runing
pumpRunning = true;
digitalWrite(pumpPin[0], HIGH);
Serial.print("pump1 is priming");
}
else{
digitalWrite(pumpPin[1], LOW);
Serial.print("pump1 stoped priming");
pumpRunning = false;
}
if(priming_pump_2==1 && pumpRunning==false) // pumpRunning false to assure only one pump running atime
{
Blynk.virtualWrite(V10, 0); // to assure the pin is not active while other pump is runing
Blynk.virtualWrite(V12, 0); // to assure the pin is not active while other pump is runing
pumpRunning = true;
digitalWrite(pumpPin[1], HIGH);
Serial.print("pump2 is priming");
}
else{
digitalWrite(pumpPin[1], LOW);
Serial.print("pump2 is stoped priming");
pumpRunning = false;
}
if(priming_pump_3==1 && pumpRunning==false)
{
Blynk.virtualWrite(V10, 0);
Blynk.virtualWrite(V11, 0);
pumpRunning = true;
digitalWrite(pumpPin[2], HIGH);
Serial.print("pump3 is priming");
}
else{
digitalWrite(pumpPin[2], LOW);
Serial.print("pump3 is stoped priming");
pumpRunning = false;
}
}
void setup()
{
Serial.begin(115200);
pinMode(Erasing_button, INPUT);
pinMode(stirrer, OUTPUT);
for (uint8_t t = 4; t > 0; t--) {
Serial.println(t);
delay(1000);
}
// Press and hold the button to erase all the credentials
if (digitalRead(Erasing_button) == LOW)
{
Credentials.Erase_eeprom();
}
String auth_string = Credentials.EEPROM_Config();
auth_string.toCharArray(auth_token, 33);
if (Credentials.credentials_get())
{
Blynk.config(auth_token);
connected_to_internet = 1;
}
else
{
Credentials.setupAP(esp_ssid, esp_pass);
connected_to_internet = 0;
}
if (connected_to_internet)
{
ph.begin();
sensors.begin();
// timer.setInterval(1000L, myTimerEvent);
timer.setInterval(1200L, myTimerEvent2);
timer.setInterval(500L, Priming);
for (int p = 0; p<= 2; p++)
{
pinMode(pumpPin[p],OUTPUT);
}
Blynk.virtualWrite(V10,0);
Blynk.virtualWrite(V11,0);
Blynk.virtualWrite(V12,0);
Blynk.virtualWrite(V14,0);
}
}
void loop()
{
Credentials.server_loops();
if (connected_to_internet)
{
Blynk.run();
timer.run();
}
}