Hey guys, so I've written a self calibrating program for a capacitive soil moisture sensor that begins when a button is pressed.
Everything works fine, the program calculates the highest and lowest values once the readings are done in a specific period of time.
In essence I run 2 separate for loops that run for 10 seconds and 30 seconds depending on the calibration being done (air value vs wet value).
Everything works for the first time I run the program. However, once I try to run the calibration program again, it seems that the for loop conditions are immediately met, and the program skips to the next part.
I believe the problem lies in the local valiables I created for the for loops (to count the time the for loops run for) not deleting.
Any insight into this would be super helpful.
#include <LiquidCrystal.h>
#include <JELdimmer2.h>
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
//Configuraciones del sensor de humedad
int ValorAire = 600; //Este es el valor del sensor cuando está totalmente seco y al aire.
int ValorAgua = 250; //Este es el valor del sensor cuando está sumergido en agua.
int tiempoLecturaAgua = 30000; //Este es el tiempo que el programa de calibración usa para leer el sensor sumergido en agua.
int tiempoLecturaAire = 10000; //Este es el tiempo que el programa de calibración usa para leer el sensor sumergido en agua.
int HumedadTierra = 0;
byte HumedadTierraPorciento = 0;
byte BotonCalibracion = 8;
void setup() {
Serial.begin(9600); // Abrir puerto serial.
lcd.begin(16, 2);
pinMode(BotonCalibracion, INPUT_PULLUP);
}
void loop() {
Inicio:
HumedadTierra = analogRead(A0);
HumedadTierraPorciento = map(HumedadTierra, ValorAire, ValorAgua, 0, 100);
Serial.println(HumedadTierra);
Serial.print(HumedadTierraPorciento);
Serial.println("%");
lcd.setCursor(0, 0);
lcd.print("Humedad");
lcd.setCursor(0, 1);
lcd.print(HumedadTierraPorciento);
lcd.print(" % ");
delay(1000);
if (digitalRead(BotonCalibracion) == LOW){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Calibremos");
lcd.setCursor(0, 1);
lcd.print("el sensor");
delay(2000);
lcd.clear();
while (digitalRead(BotonCalibracion) == HIGH){
lcd.setCursor(0, 0);
lcd.print("Seque sensor");
lcd.setCursor(0, 1);
lcd.print("completamente");
if (digitalRead(BotonCalibracion) == LOW){
lcd.clear();
for (int tiempoInicioAire = millis(); (millis()-tiempoInicioAire) < tiempoLecturaAire; ){
ValorAire = max(analogRead(0), ValorAire);
lcd.setCursor(0, 0);
lcd.print("Calibrando");
lcd.setCursor(0, 1);
lcd.print("para aire ");
Serial.println(ValorAire);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Valor Aire");
lcd.setCursor(0, 1);
lcd.print("establecido: ");
lcd.print(ValorAire);
Serial.println(ValorAire);
delay(5000);
lcd.clear();
while (digitalRead(BotonCalibracion) == HIGH){
lcd.setCursor(0, 0);
lcd.print("Ahora sumerja");
lcd.setCursor(0, 1);
lcd.print("en agua");
if (digitalRead(BotonCalibracion) == LOW){
lcd.clear();
for (int tiempoInicioAgua = millis(); (millis()-tiempoInicioAgua) < tiempoLecturaAgua; ){
ValorAgua = min(analogRead(0), ValorAgua);
lcd.setCursor(0, 0);
lcd.print("Calibrando");
lcd.setCursor(0, 1);
lcd.print("para agua");
Serial.println(ValorAgua);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Valor Agua");
lcd.setCursor(0, 1);
lcd.print("establecido: ");
lcd.print(ValorAgua);
Serial.println(ValorAgua);
delay(5000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Calibracion");
lcd.setCursor(0, 1);
lcd.print("exitosa");
Serial.println(ValorAgua);
delay(5000);
lcd.clear();
return;
}
}
}
}
}
}