Hi everybody.
This is my issue. I was using a guide for making an aruino that checks temperture and humidity (http://www.instructables.com/id/Arduino-Automatic-Temperature-Humidity-Controller-/?ALLSTEPS)
Now, I have added a ground moisture sensor that works perfectly fine, the only problem is that i dont have enough space in my (robot lcd keypad shield) screen for all the values.
What I tried to do was making (at first an IF loop but then) a WHILE loop that will continue monitoring and showing some of the data -> and when a key id pressed (up and down for this case) the program will go to another While loop, showing the rest of the information.
I have to admit the i got a bit confused, but dont blame me, just a newbie
The issue with my program, is that the program have to run in a loop until a key is pressed, and not wait for the user to press the key (becouse then the monitorind, and the real time info will get stuck)
This is where i got so far (not working):
#include <LiquidCrystal.h>
#include <DFR_Key.h>
#include "DHT.h"
#define temHighTrigger 29 //Setting the trigger value for the temperture, once the temperture lower than this trigger value, the heater band will start heating
#define humLowTrigger 45 //Setting the trigger value for the humidity, once the humidity lower than this value, start humidification
#define DHTPIN 15 // what pin we're connected to
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
const int relay1 = 18; // the number of the relay 1 pin
const int relay2 = 19; // the number of the relay 2 pin
const int DFRKEY = 0;
const int CYTRON = 1;
DHT dht(DHTPIN, DHTTYPE);
//Pin assignments for DFRobot LCD Keypad Shield
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
//---------------------------------------------
DFR_Key keypad;
// define some values used by the panel and buttons
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
int sensorPin = A2; // select the input pin for the potentiometer
int sensorValue = 0; // variable to store the value coming from the moist sensor
int read_LCD_buttons(){ // read the buttons
adc_key_in = analogRead(0); // read the value from the sensor
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
// We make this the 1st option for speed reasons since it will be the most likely result
if (adc_key_in > 1000) return btnNONE;
// For V1.1 us this threshold
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 250) return btnUP;
if (adc_key_in < 450) return btnDOWN;
if (adc_key_in < 650) return btnLEFT;
if (adc_key_in < 850) return btnSELECT;
// For V1.0 comment the other threshold and use the one below:
/*
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
*/
return btnNONE; // when all others fail, return this.
}
void setup()
{
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("initializing...");
lcd.setCursor(0, 1);
delay(1000);
Serial.begin(9600);
dht.begin();
delay(1000);
lcd.clear();
/*
OPTIONAL
keypad.setRate(x);
Sets the sample rate at once every x milliseconds.
Default: 10ms
*/
keypad.setRate(10);
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
}
void loop()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
float sensorValue = analogRead(sensorPin);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
while (read_LCD_buttons() > 1000) {};
while (read_LCD_buttons() < 250) {
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h)) {
Serial.println("Failed to read from DHT");
} else {
lcd.setCursor(0, 0);
lcd.print("Hum: ");
lcd.print(h);
lcd.print(" %");
lcd.setCursor(0, 1);
lcd.print("Tem: ");
lcd.print(t);
lcd.print(" *C");
}
if(h < humLowTrigger) //if the humidity lower than the trigger value
digitalWrite(relay1, LOW); //start humidification
else
digitalWrite(relay1, HIGH);
if(t < temHighTrigger)
digitalWrite(relay2, HIGH);//if temperture higher start cooling
else
digitalWrite(relay2, LOW);
}
while (read_LCD_buttons() > 250 and read_LCD_buttons() < 450) {
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h)) {
Serial.println("Failed to read from DHT");
} else {
lcd.setCursor(0, 1);
lcd.print("Moist: ");
lcd.print(sensorValue);
lcd.print(" %");
}
if(h < humLowTrigger) //if the humidity lower than the trigger value
digitalWrite(relay1, LOW); //start humidification
else
digitalWrite(relay1, HIGH);
if(t < temHighTrigger)
digitalWrite(relay2, HIGH);//if temperture higher start cooling
else
digitalWrite(relay2, LOW);
}
}