#include <Arduino.h>
#include "WiFi.h"
#include "ThingSpeak.h"
#include <Wire.h>
#include "DFRobot_LCD.h"
// pH SENSOR---------------------------------------/
#define PH_PIN 25
// DO SENSOR-----------------------------------------------------------------/
#define DO_PIN A1
#define VREF 5000 // VREF(mv)
#define ADC_RES 1024 //ADC Resolution/
#define TWO_POINT_CALIBRATION 1
#define READ_TEMP (25)
#define CAL1_V (1600) //mv CHANGE AFTER CALIBRATION
#define CAL1_T (25) //β
#define CAL2_V (1300) //mv CHANGE AFTER CALIBRATION
#define CAL2_T (15) //β
const uint16_t DO_Table[11] = {
8570, 8410, 8250, 8110, 7960, 7820, 7690, 7560, 7430, 7300, 7180};
uint8_t Temperaturet;
uint16_t ADC_Raw;
uint16_t ADC_Voltage;
uint16_t DO;
int16_t readDO(uint32_t voltage_mv, uint8_t temperature_c)
{
#if TWO_POINT_CALIBRATION == 0
uint16_t V_saturation = (uint32_t)CAL1_V + (uint32_t)35 * temperature_c - (uint32_t)CAL1_T * 35;
return (voltage_mv * DO_Table[temperature_c] / V_saturation);
#else
uint16_t V_saturation = (int16_t)((int8_t)temperature_c - CAL2_T) * ((uint16_t)CAL1_V - CAL2_V) / ((uint8_t)CAL1_T - CAL2_T) + CAL2_V;
return (voltage_mv * DO_Table[temperature_c] / V_saturation);
#endif
}
// pH SENSOR---------------------------------------------------------------/
DFRobot_LCD lcd(16, 2); //16 characters and 2 lines of show
float voltage,phvalue,temperature = 25;
float acidVoltage = (1990); //buffer solution at 4.o
float neutralVoltage = (1389); //buffer solution at 7.o
const int Sensor_MINvalue1 = 6.5; // +-0.5 from ph 4.0
const int Sensor_MAXvalue1 = 7.5;
// THINGSPEAK--------------------------------------------------------------------//
#define CHANNEL_ID 1967185
#define CHANNEL_API_KEY "A3Y9LSEOI6ETE19T"
WiFiClient client;
int counter =0;
#define WIFI_TIMEOUT_MS 20000
#define WIFI_NETWORK "ASUS nanosun-123"
#define WIFI_PASSWORD "201312279c"
void connectToWiFi(){
Serial.print("Connecting Wifi");
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_NETWORK, WIFI_PASSWORD);
unsigned long startAttemptTime = millis();
while (WiFi.status() != WL_CONNECTED &&
millis() - startAttemptTime < WIFI_TIMEOUT_MS){
Serial.print(".");
delay(100);
}
if(WiFi.status() != WL_CONNECTED){
Serial.println(" Failed ");
} else{
Serial.print("Connected:" );
Serial.println(WiFi.localIP());
}
}
void breath(unsigned char color){
for(int i=0; i<255; i++){
lcd.setPWM(color, i);
delay(5);
}
delay(500);
for(int i=254; i>=0; i--){
lcd.setPWM(color, i);
delay(1000);
}
delay (300) ;
}
//-------------------------------------------------------------//
void setup()
{
Serial.begin(11520);
// THINGSPEAK-------------------------------------------------//
connectToWiFi();
ThingSpeak.begin(client);
// pH Sensor---------------------------------------------- //
lcd.init();
lcd.setCursor(4,0);
lcd.print("Welcome");
delay(1000);
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Reading ");
lcd.setCursor(3,1);
lcd.print("pH & DO");
delay(1000);
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Please Wait");
delay(1000);
lcd.clear();
}
void loop()
{
//THINGSPEAK-------------------------------------------------------------//
counter++;
ThingSpeak.setField(1, phvalue);
ThingSpeak.setField(2, DO);
ThingSpeak.writeFields(CHANNEL_ID, CHANNEL_API_KEY);
delay(5000);
//pH SENSOR------------------------------------------------------------------//
static unsigned long timepoint = millis();
if(millis()-timepoint>1000U){
timepoint = millis();
// timepoint = read temperature
voltage = analogRead (PH_PIN)/4095.0*3300;
float slope = (7.0-4.0)/((neutralVoltage-1500)/30 - (acidVoltage-1500)/3.0);
float intercept = 7.0 - slope*(neutralVoltage-1500)/3.0;
phvalue = slope*(voltage-1500)/3.0 + intercept; // y=k*x + b [formula
if( phvalue < Sensor_MAXvalue1 && phvalue > Sensor_MINvalue1 ){
Serial.print("Voltage:");
Serial.print(voltage,1);
Serial.print("PH:");
Serial.println(phvalue,2);
//LCD setting 16x2
lcd.setCursor(0,0);
lcd.print("PH : ");
lcd.print(phvalue,2);
lcd.setCursor(0,1);
lcd.print("Volt : ");
lcd.print(voltage/1000,2);
}
else {
Serial.begin(11520);
lcd.init();
lcd.setCursor(0,0);
lcd.print("DOSING REQUIRED");
delay(3000);
lcd.clear();
}
}
// DO SENSOR-------------------------------------------------------------------------//
Temperaturet = (uint8_t)READ_TEMP;
ADC_Raw = analogRead(DO_PIN);
ADC_Voltage = uint32_t(VREF) * ADC_Raw / ADC_RES;
Serial.print("Temperaturet:\t" + String(Temperaturet) + "\t");
Serial.print("ADC RAW:\t" + String(ADC_Raw) + "\t");
Serial.print("ADC Voltage:\t" + String(ADC_Voltage) + "\t");
Serial.println("DO:\t" + String(readDO(ADC_Voltage, Temperaturet)) + "\t");
}
The code has no errors to m knowledge however upon trying out the code it is skipping my if statement
guix
December 7, 2022, 6:59am
2
Welcome
If you try to store a float in an int, it will be truncated, so 6.5 become 6 and 7.5 become 7
So do you think this condition can ever be true : if( phvalue < 7 && phvalue > 6 )
?
Edit: sorry I see phValue
is a float, so yes this condition can be true
Try print phValue
outside the if, to see if your calculation gives the expected result
Which arduino do you use ?
it is not skipping, it just happens that your condition doesn't get true.
Print all used variables of your condition before the if and check what value isn't like you would have expected.
PS: format your sketch in the IDE with CTRL-T - makes reading of your code easier
PSS: read about the Arduino F-Makro for your prints (Serial, LCD) of fixtexts.
esp 32 firebeetle
it works but when i put it in this way it doesn't
if( phvalue < Sensor_MAXvalue1 && phvalue > Sensor_MINvalue1 ){
Serial.print("Voltage:");
Serial.print(voltage,1);
Serial.print("PH:");
Serial.println(phvalue,2);
//LCD setting 16x2
lcd.setCursor(0,0);
lcd.print("PH : ");
lcd.print(phvalue,2);
lcd.setCursor(0,1);
lcd.print("Volt : ");
lcd.print(voltage/1000,2);
}
the value when check ouside the if statement is correct
but the moment I introduce the if and else statement it would skip the If statement
show your updated code.
show the output on Serial, showing exactly that situation where the values "before" lead to something magic (spoiler: there is no magic in IT).
Describe each value.
The more you describe the easier you will find your error on your own.
an if thingy test.
Serial.print( "phvalue " );
Serial.print ( phvalue );
Serial.print ( " Sensor_MAXvalue1 " );
Serial.print( Sensor_MAXvalue1 );
Serial. print ( " Sensor_MINvalue1 " );
Serial.print ( Sensor_MINvalue1 );
Serial.println();
if( phvalue < Sensor_MAXvalue1 && phvalue > Sensor_MINvalue1 ){
Serial.print("Voltage:");
Serial.print(voltage,1);
Serial.print("PH:");
Serial.println(phvalue,2);
//LCD setting 16x2
lcd.setCursor(0,0);
lcd.print("PH : ");
lcd.print(phvalue,2);
lcd.setCursor(0,1);
lcd.print("Volt : ");
lcd.print(voltage/1000,2);
}
Your postings do not show you are looking at the values of the if statement, thought I'd add the code in for you.
Now could you post the print out of the if thing test?
1 Like
I may have missed something whilst following through your code but my maths says phvalue will never be between 6 and 7 and so your if statement will never be true.
analogRead gives a value between 0 and 1023. So once you've applied your formulas I think the min and max resulting values for phvalue are 10.39 and 15.33
alto777
December 7, 2022, 11:27am
11
To generalize on @guix and basically everyone's advice, Serial .print and/or Serial .println can be scattered about your code liberally to
check the values of key variables
demonstrate that flow has reached key points in your algorithm
Crude but tots effective and used even by the heaviest of the heavies.
And you can relax and be confident that it's you, not some previously unknown flaw or defect in the tools. 99.999 percent, it's not A or B or C or⦠on any list of what might be you problem.
Why is my code doing X ? Because that's what you wrote, even if it doesn't do what you had mind.
Again, true for eveyone.
a7
They are using an ESP32 processor. Does that default to 12-bit (4095) analogRead() like the ESP8266?
Note, Serial.begin(11520) in both setup() and loop(). Is your Serial Monitor set to 11520, and if so, why such a low baud rate?
alto777
December 7, 2022, 8:49pm
16
ToddL1962:
Dumb questions:
No such thing. Dumb code, on the other handβ¦
@touch1337 try
if( phvalue < Sensor_MAXvalue1 && phvalue > Sensor_MINvalue1 ){
Serial.print("IF: ");
Serial.print(" PH:");
Serial.println(phvalue,2);
}
else {
Serial.print("ELSE: ");
Serial.print(" PH:");
Serial.println(phvalue,2);
}
to separate out the issue @ToddL1962 raises.
When that works, worry about the LCD. Don't repeat these newly spotted mistakes.
a7
after checking you are right, my values are at the range of 11pH When it is suppose to be 6.5-7.5 how do I change it ?
alto777
December 8, 2022, 2:11am
18
aniqchino:
how do I change it ?
How should we know? Presumably you are measuring some things and calculating some things and testing some value(s) produced.
Has nothing to do with code, rather with your area of expertise.
a7
how did u calculate the ph for yourself? the first time I used the code it showed pH values that I wanted and I got the code from a tutorial at a website
the first time I did the coding it was like this
#include "DFRobot_LCD.h"
#include <Arduino.h>
#include <Wire.h>
// pH SENSOR
DFRobot_LCD lcd(16, 2); //16 characters and 2 lines of show
#define PH_PIN 25
float voltage,phvalue,temperature = 25;
float acidVoltage = 2023; //buffer solution at 4.o
float neutralVoltage = 1500; //buffer solution at 7.o
void setup()
{
Serial.begin(11520);
// LCD Begin //
lcd.init();
lcd.setCursor(4,0);
lcd.print("Welcome");
delay(1000);
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Reading ");
lcd.setCursor(3,1);
lcd.print("pH & DO");
delay(1000);
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Please Wait");
delay(1000);
lcd.clear();
}
void loop()
{
//pH SENSOR
static unsigned long timepoint = millis();
if(millis()-timepoint>1000U){
timepoint = millis();
// timepoint = read temperature
voltage = analogRead (PH_PIN)/4095.0*3300;
float slope = (7.0-4.0)/((neutralVoltage-1500)/30 - (acidVoltage-1500)/3.0);
float intercept = 7.0 - slope*(neutralVoltage-1500)/3.0;
phvalue = slope*(voltage-1500)/3.0 + intercept; // y=k*x + b [formula]
// PH SENSOR
Serial.print("Voltage:");
Serial.print(voltage,1);
Serial.print("PH:");
Serial.println(phvalue,2);
//LCD setting 16x2
lcd.setCursor(7, 0);
lcd.print(" ");
lcd.setCursor(7, 1);
lcd.print(" ");
lcd.setCursor(0,0);
lcd.print("PH : ");
lcd.print(phvalue,2);
lcd.setCursor(0,1);
lcd.print("Volt : ");
lcd.print(voltage/1000,2);
}
}
is it better for me to have a higher baud rate and which one most of the code I have is through tutorials and I only did minor changes