Hey there,
I have been working on making a controller for my hydroponic and have got the EC and pH working now, and even had it running on my LCD shield (Found here).
The problem im having is trying to get a menu to work with it, i found code for a menu Here. and i have been trying to get it to work with what i want it to do.
I have got it going but the problem is with this piece of code which 'pauses' the menu and makes the main screen showing the EC and pH update every 200 m/s
// Wait one fifth of a second to complete
while(millis() % 200 != 0);
any help to fix this problem, simple terms would be great to as Im still learning.
Full code is below.
#include <LiquidCrystal.h>
#include "LCDKeypad.h"
unsigned int setting = 0;
unsigned int days = 0;
unsigned int hours = 0;
unsigned int minutes = 0;
unsigned int seconds = 0;
#define HOMESCREEN 0
#define CALIBRATION 1
#define DOSING 2
#define SET_POINTS 3
#define BACKLIGHT 4
LCDKeypad lcd;
#define LCDAnalogPin A0 //LCD Shield input
#define CFAnalogPin A2 //CF meter Analog input
#define pHAnalogPin A1 //pH meter Analog input
#define Offset 0.00 //deviation compensate
#define ArrayLength 40 //times of collection
#define samplingInterval 20
#define printInterval 500
#define ArrayLength 40 //times of collection
int pHArray[ArrayLength]; //Store the average value of the sensor feedback
int pHArrayIndex = 0; //listing and control variables
double Calccf;
double Calcph;
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
}
void loop()
{
buttonListen();
}
double ReadPH()
{
static unsigned long samplingTime = millis();
static float pHValue, voltage;
if(millis()-samplingTime > samplingInterval)
{
pHArray[pHArrayIndex++] = analogRead(pHAnalogPin); //adds new value to the pH array
if(pHArrayIndex==ArrayLength)pHArrayIndex=0; //if this is the first value, index is 0.
voltage = avergearray(pHArray, ArrayLength)*5.0/1024;
pHValue = 3.5*voltage+Offset;
samplingTime=millis();
}
return pHValue;
}
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[i];
}
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[i]<min){
amount+=min; //arr<min
min=arr[i];
}else {
if(arr[i]>max){
amount+=max; //arr>max
max=arr[i];
}else{
amount+=arr[i]; //min<=arr<=max
}
}//if
}//for
avg = (double)amount/(number-2);
}//if
return avg;
}
double ReadCF()
{
static float CFValue;
CFValue = analogRead(CFAnalogPin)* 5.00 / 1024, 2;
return CFValue;
}
void buttonListen()
{
// Read the buttons five times in a second
for (int i = 0; i < 5; i++) {
// Read the buttons value
int button = lcd.button();
switch (button) {
// Right button was pushed
case KEYPAD_RIGHT:
setting++;
break;
// Left button was pushed
case KEYPAD_LEFT:
setting--;
break;
// Up button was pushed
case KEYPAD_UP:
switch (setting) {
case CALIBRATION:
days++;
break;
case DOSING:
hours++;
break;
case SET_POINTS:
minutes++;
break;
case BACKLIGHT:
seconds++;
}
break;
// Down button was pushed
case KEYPAD_DOWN:
switch (setting) {
case HOMESCREEN:
days--;
if (days == -1) days = 99;
break;
case CALIBRATION:
days--;
if (days == -1) days = 99;
break;
case DOSING:
hours--;
if (hours == -1) hours = 23;
break;
case SET_POINTS:
minutes--;
if (minutes == -1) minutes = 59;
break;
case BACKLIGHT:
seconds--;
if (seconds == -1) seconds = 59;
}
}
setting %= 5;
printSetting();
// Wait one fifth of a second to complete
while(millis() % 200 != 0);
}
}
void printSetting() {
lcd.setCursor(0,0);
switch (setting) {
case HOMESCREEN:
lcd.clear();
Homescreen();
break;
case CALIBRATION:
lcd.clear();
lcd.print(" CALIBRATION ");
break;
case DOSING:
lcd.clear();
lcd.print(" DOSING ");
break;
case SET_POINTS:
lcd.clear();
lcd.print(" SET POINTS ");
break;
case BACKLIGHT:
lcd.clear();
lcd.print(" BACKLIGHT ");
}
}
void Homescreen()
{
Calcph = ReadPH();
Calccf = ReadCF();
static unsigned long printTime = millis();
if(millis() - printTime > printInterval) //Every 800 milliseconds
{
lcd.print("pH value: ");
lcd.println(Calcph);
lcd.setCursor(0,1);
lcd.print("CF value: ");
lcd.println(Calccf);
printTime=millis();
}
}