I'm trying to use an LCD keypad shield (in combination with some sensors) to make a Bio-Dome. I want the temperature and soil moisture to show on the LCD when the buttons are not being pushed. When the buttons are pushed I want them to change the "Set Temperature" and then "Set Moisture". The code I have now does what i want but i am having trouble with the delays. As soon as i let go of the buttons it goes back to the main screen, but i want to delay it for a couple seconds. Can anyone help?
CODE:
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
byte degree[8] = {
B01100,
B10010,
B10010,
B01100,
B00000,
B00000,
B00000,
B00000
};
int x;
int y;
int SensorPin = A1;
int SensorValue;
int Moisture;
int SetMoisture;
int TemperPin = A5;
int TemperValue;
int Temp;
int SetTemp;
int ledPin1 = 13;
int ledPin2 = 12;
int ledPin3 = 3;
int ledPin4 = 2;
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 read_LCD_buttons(){
adc_key_in = analogRead(0);
if (adc_key_in > 1000) return btnNONE;
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;
}
void setup(){
pinMode(SensorPin, INPUT);
pinMode(TemperPin, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
lcd.begin(16, 2);
lcd.createChar(1, degree);
SensorValue = analogRead(SensorPin);
Moisture = (SensorValue/10);
TemperValue = analogRead(TemperPin);
Temp = (((TemperValue)));
}
void loop(){
//----------------------------------------------------------------
SetTemp = x;
SetMoisture = y;
lcd.createChar(1, degree);
lcd_key = read_LCD_buttons();
switch (lcd_key){
case btnRIGHT:{
y++;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Set Moisture: ");
lcd.print(y);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(" ");
delay(100);
break;
}
case btnLEFT:{
y--;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Set Moisture: ");
lcd.print(y);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(" ");
delay (100);
break;
}
case btnUP:{
x++;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Set Temp: ");
lcd.print(x);
lcd.write(1);
lcd.print("F ");
lcd.setCursor(0,1);
lcd.print(" ");
delay (100);
break;
}
case btnDOWN:{
x--;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Set Temp: ");
lcd.print(x);
lcd.write(1);
lcd.print("F ");
lcd.setCursor(0,1);
lcd.print(" ");
delay (100);
break;
}
case btnSELECT:{
break;
}
case btnNONE:{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.print(Temp);
lcd.write(1);
lcd.print("F ");
lcd.setCursor (0,1);
lcd.print("Moisture: ");
lcd.print(Moisture);
lcd.print(" ");
delay(100);
break;
}
}
//-----------------------------------------------------------
/* if ((Moisture)<= (SetMoisture-3)){
digitalWrite(ledPin1,HIGH);
delay (100);
}
else if ((Moisture)>= (SetMoisture-3)){
digitalWrite(ledPin1,LOW);
delay (100);
}
if ((Moisture)>= (SetMoisture+3)){
digitalWrite(ledPin2,HIGH);
delay (100);
}
else if ((Moisture)<= (SetMoisture+3)){
digitalWrite(ledPin2,LOW);
delay (100);
}
//---------------------------------------------------------------
if ((Temp)<= (SetTemp - 1)){
digitalWrite(ledPin3,HIGH);
delay (100);
}
else if ((Temp)>= (SetTemp - 1)){
digitalWrite(ledPin3,LOW);
delay (100);
}
if ((Temp)>= (SetTemp + 1)){
digitalWrite(ledPin4,HIGH);
delay (100);
}
else if ((Temp)<= (SetTemp + 1)){
digitalWrite(ledPin4,LOW);
delay (100);
} */
//---------------------------------------------------------------
}
BioDome2.ino (4.93 KB)