Showing PH meter values using lcd widget

Hi there
I have this code for a Ph sensor and I am having all kinds of trouble getting it to display on a blynk widget ive been trying to display it on the LCD widget, a guage, and a value display. sorry im very new to all this and having to teach myself every step of the way. any help would be appreciated.
thanks Matt

#include <LiquidCrystal.h>


LiquidCrystal lcd(4, 6, 10, 11, 12, 13);
float corrective = 20.73; // calculate and change this value to calibrate
float averageValue = 0.00; // transit area, used in the calculations
int tabval[50]; // memorization table of 10 consecutive readings.
int temp = 0; // transit area, used in the sorting loop of read values
byte index = 0; // index table scrolling values
byte index1 = 0; // index used in the sort loop of the read values
float sumValues = 0; // sum of the 6 intermediate values ​​detected on the analog pin 0
float PHaverage = 0; // PH calculated on the average of the voltages detected on pin 0
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
 lcd.begin(16, 2); //initialize the display - display initialize
 // flash the background three times - flashing 3 times
 for (int i = 0; i < 3; i++)
 {
  
 }
 
 lcd.setCursor(0, 0);
 lcd.print("Lets go"); // Lets go
 delay (3000);
 lcd.clear ();
 lcd.print ("System Ready"); // system ready
 Serial.println ("System Ready");
 delay (2000);
 lcd.clear ();
 {
  
 }
}

void loop() {
  // put your main code here, to run repeatedly:
  for (index = 0; index < 50; index++) //  stores 50 readings
 {
 tabval[index] = analogRead(0);
 delay(60);
 }
 //
//rather interesting routine sorts values in tabval array, putting them in order, from lowest to
//highest.
for (index = 0; index < 49; index++)
 {
for (index1 = index + 1; index1 < 50; index1++)
 {
 if (tabval[index] > tabval[index1])
 {
 temp = tabval[index];
 tabval[index] = tabval[index1];
 tabval[index1] = temp;
 }
 }
 }
 sumValues = 0; // resets the sum area of ​​the values ​​in advance
 for (int index = 10; index < 39; index++) // when calculating the average, consider only the values
 // intermediate, from the tenth to the thirty-ninth position in the tabval table.
 sumValues += tabval[index]; // average of the six intermediate values ​​stored in tabval
 averageValue = (float)sumValues * 5.0 / 1024 / 30; // average voltage on the analog pin 0
 PHaverage = -5.70 * averageValue + corrective; // PH calculation, from a formula found on the net
 Serial.print("PH detected = ");
 Serial.println(PHaverage);
 lcd.setCursor (0, 0);
 lcd.print ("PH detected:");
 lcd.setCursor (0,1);
 lcd.print (PHaverage);
 lcd.print (" ");
 delay(500);
{

  }
}

Have you posted the correct code? There is no Blynk code in the sketch you’ve posted.

Pete.

Sorry I left it out because I made a mess of it but this is the basic side of the code but selecting the right widget for displaying the value of the ph sensor is whats confusing me

#define BLYNK_PRINT Serial // Enables Serial Monitor

// Following includes are for Arduino Ethernet Shield (W5100)
// If you're using another shield, see Boards_* examples
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>

char auth[] = "NJQA6KGas6JzVK10vJX4NXLgvFOFtLHn";

#include <LiquidCrystal.h>


LiquidCrystal lcd(4, 6, 10, 11, 12, 13);
float corrective = 20.73; // calculate and change this value to calibrate
float averageValue = 0.00; // transit area, used in the calculations
int tabval[50]; // memorization table of 10 consecutive readings.
int temp = 0; // transit area, used in the sorting loop of read values
byte index = 0; // index table scrolling values
byte index1 = 0; // index used in the sort loop of the read values
float sumValues = 0; // sum of the 6 intermediate values ​​detected on the analog pin 0
float PHaverage = 0; // PH calculated on the average of the voltages detected on pin 0
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);

  Blynk.begin(auth);
  
 lcd.begin(16, 2); //initialize the display - display initialize
 // flash the background three times - flashing 3 times
 for (int i = 0; i < 3; i++)
 {
  
 }
 
 lcd.setCursor(0, 0);
 lcd.print("Lets go"); // Lets go
 delay (3000);
 lcd.clear ();
 lcd.print ("System Ready"); // system ready
 Serial.println ("System Ready");
 delay (2000);
 lcd.clear ();
 {
  
 }
}

void loop() {
  // put your main code here, to run repeatedly:
  for (index = 0; index < 50; index++) //  stores 50 readings
 {
 tabval[index] = analogRead(0);
 delay(60);
 }
 //
//rather interesting routine sorts values in tabval array, putting them in order, from lowest to
//highest.
for (index = 0; index < 49; index++)
 {
for (index1 = index + 1; index1 < 50; index1++)
 {
 if (tabval[index] > tabval[index1])
 {
 temp = tabval[index];
 tabval[index] = tabval[index1];
 tabval[index1] = temp;
 }
 }
 }
 sumValues = 0; // resets the sum area of ​​the values ​​in advance
 for (int index = 10; index < 39; index++) // when calculating the average, consider only the values
 // intermediate, from the tenth to the thirty-ninth position in the tabval table.
 sumValues += tabval[index]; // average of the six intermediate values ​​stored in tabval
 averageValue = (float)sumValues * 5.0 / 1024 / 30; // average voltage on the analog pin 0
 PHaverage = -5.70 * averageValue + corrective; // PH calculation, from a formula found on the net
 Serial.print("PH detected = ");
 Serial.println(PHaverage);
 lcd.setCursor (0, 0);
 lcd.print ("PH detected:");
 lcd.setCursor (0,1);
 lcd.print (PHaverage);
 lcd.print (" ");
 delay(500);

 Blynk.run();
{

  }
}

also the ph sensor reads perfectly on the serial monitor but transferring that data to blynk is where im struggling

Okay, first of all you need to read this and understand it fully:

I’d then suggest that you implement this approach in your non-blynk code and get it working correctly.
You should then add-in the Blynk code, with some amendments…

Your current Blynk code does not include any commands for sending your readings to Blynk. These are the Blynk equivalents of lcd.print and Serial.print. You wouldn’t expect to hook-up a physical LCD display to your board and have it magically show your PH data, and Blynk is the same.

There are several approaches that can be used when sending data to a Blynk LCD widget. One is to define an LCD widget object and print data to that. There is an example here:
https://examples.blynk.cc/?board=ESP8266&shield=ESP8266%20WiFi&example=Widgets%2FLCD%2FLCD_AdvancedMode

However, if you follow this approach exactly then you’ll hit a problem because your existing physical LCD is declared as an object called lcd. The example uses the same name for the Blynk LCD widget, so this would cause an error.
If you wanted to take this approach then I’d cal your Blynk LCD object something else (blynk_lcd maybe?).
You’d then double-up on your commands like this:

lcd.print (PHaverage);;
blynk_ lcd.print (x,y,PHaverage);

the x & y coordinates represent the row and line of the display,just like in the lcd.setCursor (0, 0); command. This approach requires the LCD widget to be set to Advanced mode.

As for getting the data to a gauge widget and/or a labelled value widget is concerned, you’d attach these to virtual pins and push the data to them usieng a Blynk.virtualWrite command.
If the widget in question was attached to pin V10 the you’d use:

Blynk.virtualWrite(V10,PHaverage);

The same approach can also be used with the LCD widget in Simple mode. With this approach you wouldn’t declare the blynk_ lcd object. Instead, Simple mode gives you two virtual pins, one for row 0 and the other for row 1.
With this approach you just use Blynk.virtualWrite commands to push the data to your your two widgets.
This approach is shown in this example:
https://examples.blynk.cc/?board=ESP8266&shield=ESP8266%20WiFi&example=Widgets%2FLCD%2FLCD_SimpleModePushing

Pete.

thanks heaps Pete
ill have a play around
if im to keep the void loop clean and use blynk.run does that mean ill be putting commands like mentioned into the void setup

blynk_lcd.print (x,y,PHaverage);

No, void setup runs just once, when the device boots up.

As shown in the examples in the “keep your void loop clean” document, you’d use a timer to call a function which contains the code you’d use to take a reading from your sensor and push it out to your physical LCD, serial monitor and Blynk.

Pete.

Hi there
Thanks for all your help with my project, I have finally figured out how to get the PH readings working on the LCD widget. now im trying to get another sensor to display its readings at the same time on the same LCD widget, but I get an error which only appears when I have the ph and tds sketches together.
the error is ‘getMedianNum’ was not declared in this scope. This doesnt occur when I run the tds sensor with the lcd widget on its own. could you please look at the sketch and give me some advice.


#define BLYNK_PRINT Serial


#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "NJQA6KGas6JzVK10vJX4NXLgvFOFtLHn";

WidgetLCD lcd(V1);
BlynkTimer timer;


// Start of TDS Section
#define TdsSensorPin A1
#define VREF 5.0 // analog reference voltage(Volt) of the ADC
#define SCOUNT 30 // sum of sample point
int analogBuffer[SCOUNT]; // store the analog value in the array, read from ADC
int analogBufferTemp[SCOUNT];
int analogBufferIndex = 0,copyIndex = 0;
float averageVoltage = 0,tdsValue = 0,temperature = 25;
// End TDS Section 

float corrective = 20.73; // calculate and change this value to calibrate
float averageValue = 0.00; // transit area, used in the calculations
int tabval[50]; // memorization table of 10 consecutive readings.
int temp = 0; // transit area, used in the sorting loop of read values
byte index = 0; // index table scrolling values
byte index1 = 0; // index used in the sort loop of the read values
float sumValues = 0; // sum of the 6 intermediate values ​​detected on the analog pin 0
float PHaverage = 0; // PH calculated on the average of the voltages detected on pin 0

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);

Blynk.begin(auth);
Blynk.virtualWrite(V1,PHaverage);
 for (int i = 0; i < 3; i++)

Blynk.virtualWrite(V1,tdsValue);

 // For TDS Setup

pinMode(TdsSensorPin,INPUT);
 // End TDS Setup
}





void loop() {
  // put your main code here, to run repeatedly:
 Blynk.run();
 timer.run();

  
  for (index = 0; index < 50; index++) //  stores 50 readings
 {
 tabval[index] = analogRead(0);
 delay(60);
 }
 //
//rather interesting routine sorts values in tabval array, putting them in order, from lowest to
//highest.
for (index = 0; index < 49; index++)
 {
for (index1 = index + 1; index1 < 50; index1++)
 {
 if (tabval[index] > tabval[index1])
 {
 temp = tabval[index];
 tabval[index] = tabval[index1];
 tabval[index1] = temp;
 }
 }
 }
 sumValues = 0; // resets the sum area of ​​the values ​​in advance
 for (int index = 10; index < 39; index++) // when calculating the average, consider only the values
 // intermediate, from the tenth to the thirty-ninth position in the tabval table.
 sumValues += tabval[index]; // average of the six intermediate values ​​stored in tabval
 averageValue = (float)sumValues * 5.0 / 1024 / 30; // average voltage on the analog pin 0
 PHaverage = -5.70 * averageValue + corrective; // PH calculation, from a formula found on the net
 Serial.print("PH detected = ");
 Serial.println(PHaverage);

 lcd.print(0,0,PHaverage);
 lcd.print (6,0,"Ph");

 // Start TDS Loop
static unsigned long analogSampleTimepoint = millis();
if(millis()-analogSampleTimepoint > 40U) //every 40 milliseconds,read the analog value from the ADC
{
analogSampleTimepoint = millis();
analogBuffer[analogBufferIndex] = analogRead(TdsSensorPin); //read the analog value and store into the buffer
analogBufferIndex++;
if(analogBufferIndex == SCOUNT)
analogBufferIndex = 0;
}
static unsigned long printTimepoint = millis();
if(millis()-printTimepoint > 800U)
{
printTimepoint = millis();
for(copyIndex=0;copyIndex<SCOUNT;copyIndex++)
analogBufferTemp[copyIndex]= analogBuffer[copyIndex];

averageVoltage = getMedianNum(analogBufferTemp,SCOUNT) * (float)VREF/ 1024.0; // read the analog value more stable by the median filtering algorithm, and convert to voltage value
float compensationCoefficient=1.0+0.02*(temperature-25.0); //temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.02*(fTP-25.0));
float compensationVolatge=averageVoltage/compensationCoefficient; //temperature compensation
tdsValue=(133.42*compensationVolatge*compensationVolatge*compensationVolatge - 255.86*compensationVolatge*compensationVolatge + 857.39*compensationVolatge)*0.5; //convert voltage value to tds value
//Serial.print("voltage:");
//Serial.print(averageVoltage,2);
//Serial.print("V ");
Serial.print("TDS Value:");
Serial.print(tdsValue,0);
Serial.println("ppm");
}
}
int getMedianNum(int bArray[], int iFilterLen)

int bTab[iFilterLen];
for (byte i = 0; i<iFilterLen; i++)
bTab[i] = bArray[i];
int i, j, bTemp;
for (j = 0; j < iFilterLen - 1; j++)
{
for (i = 0; i < iFilterLen - j - 1; i++)
{
if (bTab[i] > bTab[i + 1])
{
bTemp = bTab[i];
bTab[i] = bTab[i + 1];
bTab[i + 1] = bTemp;
}
}
}
if ((iFilterLen & 1) > 0)
bTemp = bTab[(iFilterLen - 1) / 2];
else
bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
return bTemp;

lcd.print(0,1,tdsValue);
lcd.print(9,1,"ppm");
// End TDS Loop
{

  }
}

this is the line where its telling me not declared in the scope

averageVoltage = getMedianNum(analogBufferTemp,SCOUNT) * (float)VREF/ 1024.0; // read the analog value more stable by the median filtering algorithm, and convert to voltage value 

Hi there
ive played around with it and sorted the problem if you could look at the sketch and tell me if it could be better structured that would be very handy.
thanks Matt


#define BLYNK_PRINT Serial


#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "NJQA6KGas6JzVK10vJX4NXLgvFOFtLHn";

WidgetLCD lcd(V1);
BlynkTimer timer;


// Start of TDS Section
#define TdsSensorPin A1
#define VREF 5.0 // analog reference voltage(Volt) of the ADC
#define SCOUNT 30 // sum of sample point
int analogBuffer[SCOUNT]; // store the analog value in the array, read from ADC
int analogBufferTemp[SCOUNT];
int analogBufferIndex = 0,copyIndex = 0;
float averageVoltage = 0,tdsValue = 0,temperature = 25;
// End TDS Section 

float corrective = 20.73; // calculate and change this value to calibrate
float averageValue = 0.00; // transit area, used in the calculations
int tabval[50]; // memorization table of 10 consecutive readings.
int temp = 0; // transit area, used in the sorting loop of read values
byte index = 0; // index table scrolling values
byte index1 = 0; // index used in the sort loop of the read values
float sumValues = 0; // sum of the 6 intermediate values ​​detected on the analog pin 0
float PHaverage = 0; // PH calculated on the average of the voltages detected on pin 0

void setup()
{
  // For TDS Setup


Serial.begin(9600);

Blynk.begin(auth);
Blynk.virtualWrite(V1,tdsValue);
Blynk.virtualWrite(V1,PHaverage);


pinMode(TdsSensorPin,INPUT);
 // End TDS Setup

  // flash the background three times - flashing 3 times
 for (int i = 0; i < 3; i++);
}


void loop()
{
  // Start TDS Loop

 Blynk.run();
 timer.run();

for (index = 0; index < 50; index++) //  stores 50 readings
 {
 tabval[index] = analogRead(0);
 delay(60);
 }
 //
//rather interesting routine sorts values in tabval array, putting them in order, from lowest to
//highest.
for (index = 0; index < 49; index++)
 {
for (index1 = index + 1; index1 < 50; index1++)
 {
 if (tabval[index] > tabval[index1])
 {
 temp = tabval[index];
 tabval[index] = tabval[index1];
 tabval[index1] = temp;
 }
 }
 }
 sumValues = 0; // resets the sum area of ​​the values ​​in advance
 for (int index = 10; index < 39; index++) // when calculating the average, consider only the values
 // intermediate, from the tenth to the thirty-ninth position in the tabval table.
 sumValues += tabval[index]; // average of the six intermediate values ​​stored in tabval
 averageValue = (float)sumValues * 5.0 / 1024 / 30; // average voltage on the analog pin 0
 PHaverage = -5.70 * averageValue + corrective; // PH calculation, from a formula found on the net
 Serial.print("PH detected = ");
 Serial.println(PHaverage);

lcd.print(0,1,PHaverage);
lcd.print(6,1,"Ph");

  
static unsigned long analogSampleTimepoint = millis();
if(millis()-analogSampleTimepoint > 40U) //every 40 milliseconds,read the analog value from the ADC
{
analogSampleTimepoint = millis();
analogBuffer[analogBufferIndex] = analogRead(TdsSensorPin); //read the analog value and store into the buffer
analogBufferIndex++;
if(analogBufferIndex == SCOUNT)
analogBufferIndex = 0;
}
static unsigned long printTimepoint = millis();
if(millis()-printTimepoint > 800U)
{
printTimepoint = millis();
for(copyIndex=0;copyIndex<SCOUNT;copyIndex++)
analogBufferTemp[copyIndex]= analogBuffer[copyIndex];
averageVoltage = getMedianNum(analogBufferTemp,SCOUNT) * (float)VREF/ 1024.0; // read the analog value more stable by the median filtering algorithm, and convert to voltage value
float compensationCoefficient=1.0+0.02*(temperature-25.0); //temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.02*(fTP-25.0));
float compensationVolatge=averageVoltage/compensationCoefficient; //temperature compensation
tdsValue=(133.42*compensationVolatge*compensationVolatge*compensationVolatge - 255.86*compensationVolatge*compensationVolatge + 857.39*compensationVolatge)*0.5; //convert voltage value to tds value
//Serial.print("voltage:");
//Serial.print(averageVoltage,2);
//Serial.print("V ");
Serial.print("TDS Value:");
Serial.print(tdsValue,0);
Serial.println("ppm");
lcd.print(0,0,tdsValue);
lcd.print(9,0,"ppm");
}
}
int getMedianNum(int bArray[], int iFilterLen)
{
int bTab[iFilterLen];
for (byte i = 0; i<iFilterLen; i++)
bTab[i] = bArray[i];
int i, j, bTemp;
for (j = 0; j < iFilterLen - 1; j++)
{
for (i = 0; i < iFilterLen - j - 1; i++)
{
if (bTab[i] > bTab[i + 1])
{
bTemp = bTab[i];
bTab[i] = bTab[i + 1];
bTab[i + 1] = bTemp;
}
}
}
if ((iFilterLen & 1) > 0)
bTemp = bTab[(iFilterLen - 1) / 2];
else
bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
return bTemp;

 

// End TDS Loop
}

Go back and re-read the “keep your void loop clean” document I linked to earlier, then implement it.

The way you lay-out your code also changes the readability dramatically.
Sensible use of indents, and always putting opening curly brackets on a seperate line, and putting several blank lines between functions will help enormously.

Pete.