-
Notifications
You must be signed in to change notification settings - Fork 0
/
All_another_food.ino
106 lines (86 loc) · 2.79 KB
/
All_another_food.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* All another food
* Copyright (C) 2016 Nicola Corna <nicola@corna.info>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <DallasTemperature.h>
const uint8_t one_wire_pin = A2;
const uint8_t tec_pin = 3;
const uint8_t potentiometer_pin = A0;
const uint8_t led_pin = LED_BUILTIN;
const uint8_t min_target_temp = 30; //°C * 10^(-1)
const uint8_t max_target_temp = 130; //°C * 10^(-1)
const uint8_t hysteresis = 16; //°C * 10^(-1)
const uint16_t loop_delay_ms = 2000;
OneWire oneWire(one_wire_pin);
DallasTemperature sensors(&oneWire);
void setup(void)
{
Serial.begin(115200);
Serial.println("All another food");
pinMode(tec_pin, OUTPUT);
pinMode(led_pin, OUTPUT);
pinMode(potentiometer_pin, INPUT);
digitalWrite(tec_pin, LOW);
digitalWrite(led_pin, LOW);
sensors.begin();
sensors.setResolution(12);
}
void loop(void)
{
int16_t cur_temperature;
int16_t target_temperature;
if (sensors.getDeviceCount() == 0) {
Serial.print("No device found!!! Always on!!");
cur_temperature = max_target_temp;
}
else {
sensors.requestTemperatures();
cur_temperature = round(sensors.getTempCByIndex(0) * 10);
Serial.print("Current temperature: ");
Serial.println(cur_temperature);
}
target_temperature = map(analogRead(potentiometer_pin), 0, 1023, max_target_temp, min_target_temp);
if (target_temperature <= max_target_temp - 3) {
Serial.print("Target temperature (plus/minus hysteresis): ");
Serial.print(target_temperature);
if (digitalRead(tec_pin) == HIGH)
target_temperature -= hysteresis / 2;
else
target_temperature += hysteresis / 2;
Serial.print(" (");
Serial.print(target_temperature);
Serial.println(")");
}
else {
Serial.print("Target temperature: ");
Serial.print(target_temperature);
Serial.println(" (= OFF mode)");
target_temperature = cur_temperature + hysteresis / 2 + 1;
}
if (cur_temperature >= target_temperature) {
Serial.println("Turning on");
digitalWrite(tec_pin, HIGH);
digitalWrite(led_pin, HIGH);
}
else {
Serial.println("Turning off");
digitalWrite(tec_pin, LOW);
digitalWrite(led_pin, LOW);
}
Serial.println();
delay(loop_delay_ms);
}