Hello,
I have brought some changes to my previous code under the topic Slow response, how to improve.
In this one i did not use the delay function in reading the soil moisture. I have 3 of these function in the code. However, i made the probe active high on only one side and did not do the alternate current on the soil probe.
I did try to make it but could not as it would not work at all. Any suggestion please.
I notice on the LCD, things are quite flashy as if the values want to change but not changing, but itd updating quite nice, any approach please.
Thanks
Taz.
/*Declaration of I/Os */
const int Moisture_Input0 = 1;
const int Moisture_Input1 = 2;
const int Moisture_Input2 = 3;
const int SwapButton = 12;
const int dcSource = 13;
/*--------------------------------------------------------------*/
/*Library Code*/
#include <LiquidCrystal.h> // include the library code:
/*
LCD Connections:
rs (LCD pin 4) to Arduino pin 11
rw (LCD pin 5) to Arduino pin 10
enable (LCD pin 6) to Arduino pin 9
LCD pin 15 to Arduino pin 4
LCD pins d4, d5, d6, d7 to Arduino pins 8, 7, 6, 5
*/
/*--------------------------------------------------------------*/
LiquidCrystal lcd(11, 10, 9, 8, 7, 6, 5); // initialize the library with the numbers of the interface pins
/* Declaration of Variables*/
unsigned long Sprb_timer;
const unsigned long Cpt_Efcts = 1000;
int Moist_Val_0;
int Moist_Val_1;
int Moist_Val_2;
double Percentage_S_Moist_0;
double Percentage_S_Moist_1;
double Percentage_S_Moist_2;
int val;
int val2;
int buttonState;
int lcdMode = 0;
/*--------------------------------------------------------------*/
void setup (void) {
Serial.begin (9600);
pinMode(dcSource,OUTPUT);
pinMode(SwapButton,INPUT); // Define Digital Pin as Input
buttonState = digitalRead(SwapButton);
Sprb_timer = millis (); // Returns the milliseconds ellapsed since Prgm started
lcd.begin(16,2); // rows, columns. use 16,2 for a 16x2 LCD, etc.
lcd.clear(); // start with a blank screen
lcd.setCursor(0,0); // set cursor to column 0, row 0
}
void Sprb_i_strt (){
digitalWrite (dcSource,HIGH);
// Remember when Fwd direction of current has been set.
Sprb_timer = millis ();
}
int S_Moisture_Rd_0(){
int i;
int Moist_Read_0;
int Moist_Vals_0[4];
int Av_Moist_0 = 0;
//Variable to hold value from Moisture Probe
for (i=0;i<4;i++){
Moist_Vals_0[i] = analogRead(Moisture_Input0);
// Take reading from Soil Moisture Probe.
Av_Moist_0 = Av_Moist_0 + Moist_Vals_0[i];
}
Moist_Read_0 = (Av_Moist_0/4);
return Moist_Read_0;
}
int S_Moisture_Rd_1(){
int i;
int Moist_Read_1;
int Moist_Vals_1[4];
int Av_Moist_1 = 0;
//Variable to hold value from Moisture Probe
for (i=0;i<4;i++){
Moist_Vals_1[i] = analogRead(Moisture_Input1);
// Take reading from Soil Moisture Probe.
Av_Moist_1 = Av_Moist_1 + Moist_Vals_1[i];
}
Moist_Read_1 = (Av_Moist_1/4);
return Moist_Read_1;
}
int S_Moisture_Rd_2(){
int i;
int Moist_Read_2;
int Moist_Vals_2[4];
int Av_Moist_2 = 0;
//Variable to hold value from Moisture Probe
for (i=0;i<4;i++){
Moist_Vals_2[i] = analogRead(Moisture_Input2);
// Take reading from Soil Moisture Probe.
Av_Moist_2 = Av_Moist_2 + Moist_Vals_2[i];
}
Moist_Read_2 = (Av_Moist_2/4);
return Moist_Read_2;
}
void LCDprintMenu (void) {
lcd.clear();
lcd.print("Tazlim -- GrnHse");
lcd.setCursor(0,1);
lcd.print(" EngineerinG");
}
void LCDprintSoilMoist(void) {
lcd.clear();
lcd.print("SoilMoisture");
lcd.setCursor(0,1);
lcd.print("Percent: ");
lcd.print(Percentage_S_Moist_0,DEC);
lcd.print("%");
}
void LCDprintSoilMoist1(void) {
lcd.clear();
lcd.print("SoilMoisture");
lcd.setCursor(0,1);
lcd.print("Percent: ");
lcd.print(Percentage_S_Moist_1,DEC);
lcd.print("%");
}
void LCDprintSoilMoist2(void) {
lcd.clear();
lcd.print("SoilMoisture");
lcd.setCursor(0,1);
lcd.print("Percent: ");
lcd.print(Percentage_S_Moist_2,DEC);
lcd.print("%");
}
void SwapMenu (void){
// Push Button connected to PIN 4 by Pull up resistor Circuit for swapping Menu on LCD
val = digitalRead(SwapButton); // Read Input value and store in val.
delay(10);
val2 = digitalRead(SwapButton); // Read Input again to check for bounces
if (val == val2){
if(val != buttonState){
if (val == LOW){
lcdMode++;
if (lcdMode == 4)
{lcdMode = 0;}
}
buttonState = val; // Save the new state in the Variable for monitoring further button press (Continuous program loop).
}
if (lcdMode == 0)
{LCDprintMenu();} // Display Intro Display on the LCD.
if (lcdMode == 1)
{LCDprintSoilMoist();} // Display SoilMoist Reading in %
if (lcdMode == 2)
{LCDprintSoilMoist1();}
if (lcdMode == 3)
{LCDprintSoilMoist2();}
}
}
void loop (void){
// SoilMoisture sequence
Sprb_i_strt ();
if( (millis() - Sprb_timer) >= Cpt_Efcts )
{
S_Moisture_Rd_0();
S_Moisture_Rd_1();
S_Moisture_Rd_2();
}
Moist_Val_0 = S_Moisture_Rd_0();
Moist_Val_1 = S_Moisture_Rd_1();
Moist_Val_2 = S_Moisture_Rd_2();
// Stop current through Probe
digitalWrite(dcSource,LOW);
Percentage_S_Moist_0 =((Moist_Val_0/1000.00)*100.00);
Percentage_S_Moist_1 =((Moist_Val_1/1000.00)*100.00);
Percentage_S_Moist_2 =((Moist_Val_2/1000.00)*100.00);
// End of Soil Moisture Sequence
SwapMenu ();
/*
Serial.print("Moist0 = ");
Serial.println (Moist_Val_0);
Serial.print("Moist1 = ");
Serial.println (Moist_Val_1);
Serial.print("Moist2 = ");
Serial.println (Moist_Val_2);
Serial.print("% Moist0");
Serial.println (Percentage_S_Moist_0);
Serial.print("% Moist1");
Serial.println (Percentage_S_Moist_1);
Serial.print("% Moist2");
Serial.println (Percentage_S_Moist_2);
delay(1000);
*/
}