#include <Arduino.h>
#include <TM1637Display.h>
#include <EEPROM.h>
#include <OneButton.h>
#include <SimpleTimer.h>
// the timer object
SimpleTimer timer;
// define the connections pins
#define CLK 9
#define DIO 10
#define TEMP_PIN A0
#define RED_PIN 8
#define GREEN_PIN 7
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
int temperature_hight = 26;
int temperature_low = 24;
unsigned int addr_eeprom_temperature_hight = 0;
unsigned int addr_eeprom_temperature_low = 3;
#define buttonPin_up A4
#define buttonPin_down A5
//const int buttonPin_up = 2;
//const int buttonPin_down = 3;
// The actions I ca do...
typedef enum {
ACTION_WORK,
ACTION_SETUP
} MyActions;
MyActions nextAction = ACTION_WORK; // no action when starting
// Setup a new OneButton on pin A1.
OneButton buttonUp(buttonPin_up);
OneButton buttonDown(buttonPin_down);
// create a display object of type TM1637Display
TM1637Display display = TM1637Display(CLK, DIO);
// degree celsius symbol
const uint8_t celsius[] = {
SEG_A | SEG_B | SEG_F | SEG_G, // Degree symbol
SEG_A | SEG_D | SEG_E | SEG_F // C
};
// set default temperature
int init_eeprom (int a, int t) {
int value = 0;
EEPROM.get(a, value);
//Serial.print(a);
//Serial.print("\t");
//Serial.println(value);
if (value < 10 || value > 99) {
EEPROM.put(a, t);
}
EEPROM.get(a, value);
return value; // return temperature
};
void set_temperature_eeprom (int a) {
int value;
int time = 150;
EEPROM.get(a, value);
while (time >= 0) {
if (analogRead(buttonPin_up) == 0) {
EEPROM.get(a, value);
value+=1;
EEPROM.put(a, value);
delay(300);
time = 150;
//Serial.print(a1);
//Serial.print("\t");
//Serial.println(value);
} else if (analogRead(buttonPin_down) == 0) {
EEPROM.get(a, value);
value -= 1;
EEPROM.put(a, value);
delay(300);
time = 150;
//Serial.print(a2);
//Serial.print("\t");
//Serial.println(value);
}
//Serial.print(time);
time--;
display.showNumberDec(value, false, 2, 0);
display.setSegments(celsius, 2, 2);
nextAction = ACTION_WORK;
}
//display.showNumberDec(value, false, 2, 0);
//display.setSegments(celsius, 2, 2);
// delay(750);
// display.clear();
}
void relay (int t) {
int value;
EEPROM.get(addr_eeprom_temperature_hight, value);
if (t < value + 1) {
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
}
else {
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
}
}
void doubleclick() {
if (nextAction == ACTION_WORK) {
nextAction = ACTION_SETUP;
} else if (nextAction == ACTION_SETUP) {
nextAction = ACTION_WORK;
}
} // doubleclick
void setup() {
display.clear();
display.setBrightness(7); // set the brightness to 7 (0:dimmest, 7:brightest)
init_eeprom (addr_eeprom_temperature_hight, temperature_hight);
init_eeprom (addr_eeprom_temperature_low, temperature_low);
// link the doubleclick function to be called on a doubleclick event.
//buttonUp.attachDoubleClick(doubleclick);
//buttonDown.attachDoubleClick(doubleclick);
buttonUp.attachLongPressStart(doubleclick);
buttonDown.attachLongPressStart(doubleclick);
buttonUp.setDebounceMs(80);
buttonDown.setDebounceMs(80);
pinMode(buttonPin_up, INPUT_PULLUP);
pinMode(buttonPin_down, INPUT_PULLUP);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
timer.setInterval(20*60000); // wait 20*60 seconds - 20 min
Serial.begin(9600);
}
void loop() {
//int temp_hight = init_eeprom (addr_eeprom_temperature_hight, temperature_hight);
//int temp_low = init_eeprom (addr_eeprom_temperature_low, temperature_low);
//Serial.print(digitalRead(buttonPin_down));
//Serial.print("\t");
//Serial.print(digitalRead(buttonPin_up));
//Serial.println();
//Serial.print(nextAction);
//Serial.println();
// keep watching the push button:
buttonUp.tick();
buttonDown.tick();
if (timer.isReady()) { // Check is ready a timer
Serial.println("20 min have passed");
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
delay(2000);
timer.reset();
}
if (nextAction == ACTION_WORK) {
// normal mode.
int temperature = analogRead(TEMP_PIN);
float temperature_celsius = 1 / (log(1 / (1023. / temperature - 1)) / BETA + 1.0 / 298.15) - 273.15;
relay(temperature_celsius);
// displayed temperature
display.showNumberDec(temperature_celsius, false, 2, 0);
display.setSegments(celsius, 2, 2);
delay(100);
// display.clear();
} else if (nextAction == ACTION_SETUP) {
// setup mode.
set_temperature_eeprom (addr_eeprom_temperature_hight);
//delay(100);
//display.clear();
}
}