-
Notifications
You must be signed in to change notification settings - Fork 0
/
materino.ino
79 lines (64 loc) · 2.02 KB
/
materino.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
/**
*
* Materino Project. Hosted on https://github.com/bianucci/materino
*
*
*
*/
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
// relay which turns on/off heater is plugged into pin 12 on the arduino
#define RELAY 12
// for testing purposes the desired water temperature is 30 degrees
#define DESIRED_TEMP 30
// led connected to pin 13
#define LED 13
// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// global heating state. i know this is shitty, I will improve asap.
boolean heating = false;
void setup(void)
{
Serial.begin(9600);
sensors.begin();
pinMode(RELAY, OUTPUT);
digitalWrite(RELAY, HIGH); // turn off the heater
pinMode(LED, OUTPUT);
}
// counters are used to make sure the measured value was at least measured twice
// just to avoid any inconsistencies at measuring
int tempLowCounter = 0;
int tempHighCounter = 0;
void loop(void)
{
// read temperature
sensors.requestTemperatures();
int temp = sensors.getTempCByIndex(0);
Serial.print(temp);
Serial.print("\n");
// TODO replace global heating state by reading current state of pin
if(temp < DESIRED_TEMP){
tempLowCounter++;
if( heating == false && tempLowCounter > 2) {
Serial.print("TempTooLow! Turn ON heater.\n");
digitalWrite(RELAY, LOW); // turn on the heater
digitalWrite(LED, HIGH); // turn on the heater
heating = true;
tempLowCounter = 0;
}
} else if (temp >= DESIRED_TEMP) {
tempHighCounter++;
if( heating == true && tempHighCounter > 2) {
Serial.print("TempTooHIGH! Turn OFF heater.\n");
digitalWrite(RELAY, HIGH); // turn off the heater
digitalWrite(LED, LOW); // turn on the heater
heating = false;
tempHighCounter = 0;
}
}
}