TomGeorge:
Please post your entire code.
I thought it would be better to post only the relevant parts. But of course I can do it:
#include <LiquidCrystal.h>
#include <EEPROM.h>
const int rs = 12, en = 11, d4 = 10, d5 = 9, d6 = 8, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const int ButtGoDown = 6; //button pins
const int ButtGoUp = 5;
const int ButtChTemp = 4;
const int ButtChTime = 3;
const int ButtStart = 2;
int cal_Tempadr=0, cal_Timeadr = 2, save_Temp=0, save_Time = 0;
int GoDown = 0; //variables for reading the buttons (button states)
int GoUp = 0;
int ChTemp = 0;
int ChTime = 0;
int Start = 0;
int temp_nom = 130;
int save_temp = 0;
int cal_Temp=0, cal_Time=0;
int ThermistorPin = 2;
int Vo, temp_act;
float R1 = 20000;
float logR2, R2;
float c1 = 0.001026189, c2 = 0.000223186, c3 = 1.20877360e-7; //NTC Type 203
unsigned long timer = 30UL;
unsigned long timerM, time_start, time_rem, time_end; //will be manipulated with millis(), therefore unsigned long
void setup() {
/* definition of buttons */
pinMode(ButtGoDown, INPUT); // decrement value
pinMode(ButtGoUp, INPUT); // increment value
pinMode(ButtChTemp, INPUT); // change nominal temperature
pinMode(ButtChTime, INPUT); // change timer
pinMode(ButtStart, INPUT); // start timer
time_rem = constrain(time_rem, 0, 300);
temp_nom=EEPROMReadInt(cal_Tempadr); // initial values from eeprom
timer=EEPROMReadInt(cal_Timeadr);
lcd.begin(20,4);
lcd.setCursor(0,0);
lcd.print("Solltemp. (\337C):"); //nominal temperature
lcd.setCursor(0,1);
lcd.print("Isttemp. (\337C):"); //measured temperature
lcd.setCursor(0,2);
lcd.print("Sollzeit (min):"); // timer
lcd.setCursor(0,3);
lcd.print("Restzeit (min):"); // remaining time
Serial.begin(9600);
}
void loop() {
ChTemp = digitalRead(ButtChTemp);
ChTime = digitalRead(ButtChTime);
/* setting nominal temperature */
temp_nom = constrain(temp_nom, 20, 150);
lcd.setCursor(16,0);
if (temp_nom < 100){lcd.print(" ");lcd.print(temp_nom);}
else {lcd.print(temp_nom);}
if (ChTemp == HIGH) {
GoUp = digitalRead(ButtGoUp);
if (GoUp == HIGH) {++temp_nom;}
GoDown = digitalRead(ButtGoDown);
if (GoDown == HIGH) {--temp_nom;}
save_Temp = 1;
}
if(save_Temp==1) { // save nominal temperature to eeprom
EEPROMUpdateIntTemp(cal_Tempadr, temp_nom);
save_Temp=0; // temp_nom is saved
}
delay(150); // delay to get in-/decrements of 1 while pressing button up/down
/*actual temperature*/
Vo = analogRead(ThermistorPin);
R2 = R1 * (1023.0 / (float)Vo - 1.0);
logR2 = log(R2);
temp_act = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2)); //c1: a0, c2: a1, c3: a3
//(cf. https://de.wikipedia.org/wiki/Steinhart-Hart-Gleichung)
temp_act = (temp_act - 273.15);
lcd.setCursor(16,1);
if (temp_act < 100) {lcd.print(" "); lcd.print(temp_act);}
else {lcd.print(temp_act);}
/* setting timer */
timer = constrain(timer, 1, 300);
lcd.setCursor(16,2);
if (timer < 10){lcd.print(" ");lcd.print(timer);}
else if (timer < 100){lcd.print(" ");lcd.print(timer);}
else {lcd.print(timer);}
if (ChTime == HIGH) {
GoUp = digitalRead(ButtGoUp);
if (GoUp == HIGH) {++timer;}
GoDown = digitalRead(ButtGoDown);
if (GoDown == HIGH) {--timer;}
save_Time=1;
}
if (save_Time==1){
EEPROMUpdateIntTime(cal_Timeadr, timer);
save_Time=0;
// cal_Time=!cal_Time;
}
/* running timer */
Start = digitalRead(ButtStart);
timerM = timer*60000; // ms to min
if (Start == HIGH){
time_start = millis();
Start = LOW;
}
time_end = time_start + timerM;
time_rem = (time_end - millis())/60000;
// time_rem = constrain(time_rem, 1, 200);
lcd.setCursor(16,3);
if (time_rem < 10){lcd.print(" "); lcd.print(time_rem + 1);}
else if (timer < 100){lcd.print(" "); lcd.print(time_rem + 1);}
else {lcd.print(time_rem - 1);}
if (time_end < millis()) {
Serial.println("EEENNNDDDEEE");
}
Serial.print("timerM: ");Serial.print(timerM);Serial.print(" time_rem: ");Serial.print(time_rem + 1); Serial.print(" time_start: "); Serial.print(time_start); Serial.print(" time_end: "); Serial.print(time_end); Serial.print(" millis: "); Serial.println(millis());
}
/* update Temp to eeprom */
void EEPROMUpdateIntTemp(int cal_Tempadr, int temp_nom)
{
byte lowByte = ((temp_nom >> 0) & 0xFF);
byte highByte = ((temp_nom >> 8) & 0xFF);
EEPROM.update(cal_Tempadr, lowByte);
EEPROM.update(cal_Tempadr + 1, highByte);
}
/* update timer to eeprom */
void EEPROMUpdateIntTime(int cal_Timeadr, long timer)
{
byte lowByte = ((timer >> 0) & 0xFF);
byte highByte = ((timer >> 8) & 0xFF);
EEPROM.update(cal_Timeadr, lowByte);
EEPROM.update(cal_Timeadr + 1, highByte);
}
/* read/write from/to eeprom */
int EEPROMReadInt(int adr) {
// Integer aus dem EEPROM lesen
byte low, high;
low=EEPROM.read(adr);
high=EEPROM.read(adr+1);
return (low + ((high << 8)&0xFF00));
}
/*
void eepromWriteInt(int adr, int val) {
// Integer in das EEPROM schreiben
byte low, high;
low=val&0xFF;
high=(val >> 8)&0xFF;
EEPROM.update(adr, low); // dauert 3,3ms
EEPROM.update(adr+1, high);
return;
}*/
Be aware that the code is not finished yet. I'm sure not everything is solved smoothly. Many parts could be better. But fine tunig comes afterwards. It shall control a heating unit for test tubes.