#include <LiquidCrystal.h> #define sensor A0 bool LCDControl = 0, LowUmid = 0, HighUmid = 0; byte UmidityPercent = 0, moisture = 0, PreviousValue = 0; int ValUmidade = 0, AnalogValue = 0; const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7; LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup()
{
Serial.begin(9600);
lcd.begin(16,2);
pinMode(sensor, INPUT);
PreviousValue = analogRead(sensor);
}With the variables created and the commands in the void setup function, we will explain all logical programming in the loop function.
//Le o valor do pino A0 do sensor
AnalogValue = analogRead(sensor);
//Mostra o valor da porta analogica no serial monitor
Serial.print("Analog Port: ");
Serial.println(AnalogValue);
UmidityPercent = map(AnalogValue, 0, 1023, 0, 100);
moisture = 100 - UmidityPercent;In the loop function, the analog value was read and the value was mapped in a range of 0 and 100. This value represents a percentage of moisture of soil. When the moisture is high world, the value approaches 0 and if the moisture is low the value approaches 100.moisture = 100 - UmidityPercent;After to read the moisture value is need to present in the Display LCD. The next step is to verify if moisture value is different from its value plus 1 or its value minus 1, according to the condition below.
if( (moisture > (PreviousValue)+1) || (moisture < (PreviousValue)- 1))This condition is used to prevent the system to present the same value several times in the Display LCD. But, when the condition is true, the system will present the value in the LCD and will verify if the value is more or equal than 60% ou less than 60%.
if( (moisture > (PreviousValue)+1) || (moisture < (PreviousValue)- 1))
{
lcd.setCursor(1,0);
lcd.print("Moisture: ");
lcd.print(" ");
lcd.setCursor(11,0);
lcd.print(moisture);
lcd.print("%");
if(moisture < 60 && LowUmid == 0)
{
lcd.setCursor(1,1);
lcd.print(" ");
lcd.setCursor(1,1);
lcd.print("Low Moisture");
LowUmid = 1;
HighUmid = 0;
}
if(moisture >= 60 && HighUmid == 0)
{
lcd.setCursor(2,1);
lcd.print(" ");
lcd.setCursor(1,1);
lcd.print("High Moisture");
HighUmid = 1;
LowUmid = 0;
}
PreviousValue = moisture;
}Finally, the system will be store the value of moisture variable in the PreviousValue variable to actualize its value. Each time of a new value is presented in the display the variable PreviousValue is actualized to be used in others cycles of processing of the code.
Therefore, this is a simple system used to monitoring the moisture of plants in our residances and inform at users about the soil moisture level.#include <LiquidCrystal.h>
#define sensor A0
bool LCDControl = 0, LowUmid = 0, HighUmid = 0;
byte UmidityPercent = 0, moisture = 0, PreviousValue = 0;
int ValUmidade = 0, AnalogValue = 0;
const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup()
{
Serial.begin(9600);
lcd.begin(16,2);
pinMode(sensor, INPUT);
PreviousValue = analogRead(sensor);
}
void loop()
{
//Le o valor do pino A0 do sensor
AnalogValue = analogRead(sensor);
//Mostra o valor da porta analogica no serial monitor
Serial.print("Analog Port: ");
Serial.println(AnalogValue);
UmidityPercent = map(AnalogValue, 0, 1023, 0, 100);
moisture = 100 - UmidityPercent;
if( (moisture > (PreviousValue)+1) || (moisture < (PreviousValue)- 1))
{
lcd.setCursor(1,0);
lcd.print("Moisture: ");
lcd.print(" ");
lcd.setCursor(11,0);
lcd.print(moisture);
lcd.print("%");
if(moisture < 60 && LowUmid == 0)
{
lcd.setCursor(1,1);
lcd.print(" ");
lcd.setCursor(1,1);
lcd.print("Low Moisture");
LowUmid = 1;
HighUmid = 0;
}
if(moisture >= 60 && HighUmid == 0)
{
lcd.setCursor(2,1);
lcd.print(" ");
lcd.setCursor(1,1);
lcd.print("High Moisture");
HighUmid = 1;
LowUmid = 0;
}
PreviousValue = moisture;
}
}