Here is the code for a Mung Bean grower that controls temperature, the watering cycle, and watering on time. I have a lot of notes, just to help me learn, as my son wrote 90% of the code.
I have one problem! The button hold down time is to long to get a read, It misses the read with a normal push time of the button.
I have removed operation of all delay(), however, millis() is used and the internal clock is used.
Can you see what is causing the hold up is in the button pushing?
// This sketch is for a Bean Sprout Grower. It controls temperature (using a DS18B20),
//how many hours between pump cycles, and how long the pump is on during each
// watering cycle. The "Set Temperature", "Set Water Cycle", and 'Set Shower Time" are
//sequenced through using the Select button, The UP/DOWN buttons are used to set the
// temperature', how long the 'Water Cycle' is (in Hours) , and the length of the
//'water shower' (in 5 second increments).
//
// Libraries used
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
//Sets digital pins for LCD
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int relayHeat = 12;
int relayPump = 11;
//x= analog read of pin A (buttons)
int x;
// Sets S at 0 for button sequencing
int S = 0;
// millis count
unsigned long currentTime;
//millis before adding
unsigned long previousTime;
int temperature;
int tempC;
const int oneWireBus = 3;
OneWire oneWire(oneWireBus);
DallasTemperature sensors (&oneWire);
// Sets intitial thermostat temperature
int setPoint = 80;
int H2OcycleVis = 0;
unsigned long H2OcycleReal = 0; //Change this back to 0 for prime time.
int H2OtimeVis= 0;
int H2OtimeReal= 0;
// gets the temerature from Index in *F
int getTempFByIndex;
//this is the cycle time in ms. Change it to whatever time interval you want to test.
//3,600,000 = sequencing in 1HR intervals
const unsigned long H2Oc =20000;
// this is the 'Watering Time' ON in ms. In 5 Second Increments
const unsigned long H2Ot = 5000;
const int tempM = 0;
void setup() {
lcd.begin(16, 2);
pinMode(relayHeat, OUTPUT);
pinMode(relayPump, OUTPUT);
pinMode (3, INPUT);
Serial.begin(9600);
sensors.begin();
}
void loop() {
sensors.requestTemperatures();
temperature = sensors.getTempFByIndex(0);
currentTime = millis();
x = analogRead (0);
//Serial.print("F: ");
//.print(sensors.getTempFByIndex(0)); //change back to 0
//Serial.print("\t Temperature: ");
//Serial.print(temperature);
//Serial.print("\t C: ");
//Serial.println(sensors.getTempCByIndex(0)); //change back to 0
//Serial.print("\t H2O Cycle Real: ");
//Serial.print(H2OcycleReal);
//Serial.print("\t H2O Time Real: ");
//Serial.print(H2OtimeReal);
//Serial.print("\t currentTime: ");
//Serial.print(currentTime);
//Serial.print("\t previousTime:");
//Serial.println(previousTime);
//Serial.print("\t TempH:");
//Serial.print(tempH);
//Serial.print("\t Heat On:");
//Serial.println(12);
// This steps through the select button choices
if (S > 3){
S = 0; }
if (x > 600 && x < 800)
{ S++;
//delay(100);
}
// This lcd prints the temp when S=0
if (S == 0) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temperature ");
lcd.setCursor(6,1);
lcd.print(temperature);
lcd.setCursor(8,1);
lcd.print (" F");
//delay(100);
}
// This lcd prints 'Set Temperature' when S=1
//and increments DOWN button with (x > 100 && x < 200) or UP x > 200 && x < 400
if (S == 1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Temerature ");
lcd.setCursor(6,1);
lcd.print (setPoint);
lcd.setCursor(8,1);
lcd.print (" F ");
if (x > 100 && x < 200) {
setPoint++;
//delay(500);
}
if (x > 200 && x < 400) {
setPoint--;
//delay(500);
}
}
//This lcd prints 'Set Water Cycle' when S=2
//and increments UP Button with (x > 100 && x < 200)or UP with x > 200 && x < 400
if (S == 2) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Water Cycle ");
lcd.setCursor(5,1);
lcd.print (H2OcycleVis);
lcd.setCursor(8,1);
lcd.print ("hr(s)");
if (x > 100 && x < 200) {
H2OcycleVis++;
H2OcycleReal = H2OcycleReal + H2Oc;
//delay(100);
}
if (x > 200 && x < 400) {
H2OcycleVis--;
H2OcycleReal = H2OcycleReal - H2Oc;
//delay(100);
}
}
//This lcd prints 'Set Shower Time' if S=3
//and increments DOWN Button (x > 100 && x < 200) or UP with x > 200 && x < 400
if (S == 3) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Shower Time:");
lcd.setCursor(3,1);
lcd.print (H2OtimeVis);
lcd.setCursor(7,1);
lcd.print ("seconds");
if (x > 100 && x < 200) {
H2OtimeVis = H2OtimeVis + 5;
H2OtimeReal = H2OtimeReal + H2Ot;
//delay(100);
}
if (x > 200 && x < 400) {
H2OtimeVis = H2OtimeVis - 5;
H2OtimeReal = H2OtimeReal - H2Ot;
//delay(100);
}
}
// Internal Clock counting down Hourly cycle
if(currentTime - previousTime > H2OcycleReal) {
previousTime = currentTime;
}
// this resets currentTime when internal clock hits maximum count.
if (currentTime < 2000) {
previousTime = currentTime;
}
// If 'Set Water Cycle' time is reached turn on Pump
if (currentTime - previousTime < H2OtimeReal){
digitalWrite(11, HIGH);
}
else {
(digitalWrite(11, LOW));
}
// Turns heater off if temp reaches SetPoint,
if ( temperature >= (setPoint)){
digitalWrite(12, LOW);
}
if ( temperature <= (setPoint)){
digitalWrite(12, HIGH);
}
//delay(50);
}