ArduinoGeneral

Help Noob Over a Hump Please

userHead Account cancelled 2020-05-30 09:50:59 1099 Views1 Replies
Just learning all this. I got the lcd and the PH sensor working(serial Monitor) but I cant understand how to display analog A0 info to the LCD. I went back to the start. both programs run. Feel like a rock
Explain to a 60 year old learning new stuff to stay mentally there.


//# Product: analog pH meter
// # SKU : SEN0161

#include <dht.h>
#include <LiquidCrystal.h>
#define SensorPin A0 //pH meter Analog output to Arduino Analog Input 0
#define Offset 0.00 //deviation compensate
#define LED 13 // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#define samplingInterval 20
#define printInterval 800
#define ArrayLenth 40 //times of collection
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int pHArray[ArrayLenth]; //Store the average value of the sensor feedback
int pHArrayIndex = 0;
void setup(void)
{

lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
pinMode(LED, OUTPUT);
Serial.begin(9600);
Serial.println("pH meter experiment!"); //Test the serial monitor
}
void loop(void)
{
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis() / 1000);
static unsigned long samplingTime = millis();
static unsigned long printTime = millis();
static float pHValue, voltage;
if (millis() - samplingTime > samplingInterval)
{
pHArray[pHArrayIndex++] = analogRead(SensorPin);
if (pHArrayIndex == ArrayLenth)pHArrayIndex = 0;
voltage = avergearray(pHArray, ArrayLenth) * 5.0 / 1024;
pHValue = 3.5 * voltage + Offset;
samplingTime = millis();
}
if (millis() - printTime > printInterval) //Every 800 milliseconds, print a numerical, convert the state of the LED indicator
{
Serial.print("Voltage:");
Serial.print(voltage, 2);
Serial.print(" pH value: ");
Serial.println(pHValue, 2);
digitalWrite(LED, digitalRead(LED) ^ 1);
printTime = millis();
}
}
double avergearray(int* arr, int number) {
int i;
int max, min;
double avg;
long amount = 0;
if (number <= 0) {
Serial.println("Error number for the array to avraging!/n");
return 0;
}
if (number < 5) { //less than 5, calculated directly statistics
for (i = 0; i < number; i++) {
amount += arr;
}
avg = amount / number;
return avg;
} else {
if (arr[0] < arr[1]) {
min = arr[0]; max = arr[1];
}
else {
min = arr[1]; max = arr[0];
}
for (i = 2; i < number; i++) {
if (arr < min) {
amount += min; //arr<min
min = arr;
} else {
if (arr > max) {
amount += max; //arr>max
max = arr;
} else {
amount += arr; //min<=arr<=max
}
}//if
}//for
avg = (double)amount / (number - 2);
}//if
return avg;
}