#include <Keypad.h>
#include <LiquidCrystal.h> //includes libraries for hardware
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2 //datacable for temp.sensor on digital pin 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire); //send oneWire-reference to Dallas Temperature
float tempInC; //decimal to us for printing temp i deg C
char key1; //characters for use in keypad-set-desired-temp
char key2;
char key3;
char digit1FromFunction;
char digit2FromFunction;
char digit3FromFunction;
int twoDigits;
int tempLimit;
int greenLed = 1;
int redLed = 3;
int whiteLed = 13;
int coolRelay = 4;
int heatRelay = 5;
byte Lae[8] = {B00000,B00000,B11010,B00101,B01111,B10100,B11111,B00000}; //æ
byte Loe[8] = {B00000,B00001,B01110,B10101,B10101,B01110,B10000,B00000}; //ø
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);//lcd(13, 14, 15, 16, 17, 18); //digital pins for LCD
char key; //1 digit from keypad
const byte ROWS = 4; //Keypad has 4 rows
const byte COLS = 3; //keypad has 3 columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'}, //defining keypad structure
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = { 9, 8, 7, 6 }; // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10 }; // Connect keypad COL0, COL1 and COL2 to these Arduino pins.
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); //constructing keypad
void setup() {
//Serial.begin(9600); //cannot be used witch digital pin 0 and 1 on digitalRead
sensors.begin();
lcd.begin(16,2);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(whiteLed, OUTPUT);
pinMode(coolRelay, OUTPUT);
pinMode(heatRelay, OUTPUT);
lcd.createChar (1, Lae); //æ
lcd.createChar(2, Loe); //ø
lcd.clear();
unsigned long now = millis();
while(millis()-now < 4000){ //blinky "loading"
digitalWrite(redLed, HIGH);
delay(90);
digitalWrite(redLed, LOW);
digitalWrite(greenLed, HIGH);
delay(90);
digitalWrite(greenLed, LOW);
digitalWrite(whiteLed, HIGH);
delay(90);
digitalWrite(whiteLed, LOW);}
sensors.requestTemperatures();
lcd.print("Temperatur: ");
lcd.print(sensors.getTempCByIndex(0));
delay(5000);
lcd.clear();
//Serial.println("Velg ønsket temperatur");
lcd.print("Velg ");
lcd.write(2);
lcd.print("nsket");
lcd.setCursor(0,1);
lcd.print("temperatur.");
getTwoDigitNumber();
tempLimit = twoDigits;
lcd.clear();
lcd.print("Du har valgt:");
lcd.setCursor(0,1);
lcd.print(tempLimit);
lcd.print(" grader C");
//Serial.print("Du har valgt ");
//Serial.print(tempLimit);
//Serial.println(" grader");
delay(5000);
lcd.clear();
lcd.print("Temp-styring");
lcd.setCursor(0,1);
lcd.print(tempLimit);
lcd.print(" grader kj");
lcd.write(2);
lcd.print("rer");
//Serial.print("Temperaturstyring ");
//Serial.print(tempLimit);
//Serial.println(" grader starter.");
delay(5000);
}
void loop() {
getTempPrintLcd();
if (tempInC < (tempLimit-0.3)){ //dette fungerer nå, men om while inkluderes, gjøres ingen av operasjonene under.
//while(tempInC < tempLimit);{
digitalWrite(coolRelay, LOW);
digitalWrite(heatRelay, HIGH);
digitalWrite(redLed, HIGH);
digitalWrite(whiteLed, LOW);
digitalWrite(greenLed, LOW);
// getTempPrintLcd();
//}
}
else if (tempInC > (tempLimit+0.3)){
//while(tempInC > tempLimit){
digitalWrite(heatRelay, LOW);
digitalWrite(coolRelay, HIGH);
digitalWrite(whiteLed, HIGH);
digitalWrite(redLed, LOW);
digitalWrite(greenLed, LOW);
// getTempPrintLcd();
//}
}
else {
digitalWrite(heatRelay, LOW);
digitalWrite(coolRelay, LOW);
digitalWrite(greenLed, HIGH);
digitalWrite(whiteLed, LOW);
digitalWrite(redLed, LOW);
}
}
void getTempPrintLcd() { //function to read and print temp from sensor
sensors.requestTemperatures();
tempInC = sensors.getTempCByIndex(0);
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.print(tempInC);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("Idealtemp: ");
lcd.print(tempLimit);
lcd.print(" ");
//Serial.print("Temperatur: ");
//Serial.print(tempInC);
//Serial.print(". Ideell temperatur: ");
//Serial.println(tempLimit);
}
void keypadInput1() { //Function to get first digit of two digit number.
while(key1==NO_KEY){
key1 = kpd.getKey();
}
digit1FromFunction = (key1 - 48);
}
void keypadInput2() { //Function to get second digit of two digit number
while(key2==NO_KEY){
key2 = kpd.getKey();
}
digit2FromFunction = (key2 - 48);
}
void getTwoDigitNumber(){ //Function to get a to digit number called twoDigits
keypadInput1();
keypadInput2();
twoDigits = ((digit1FromFunction*10) + (digit2FromFunction));
}