-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOneWireConnector.ino
138 lines (122 loc) · 2.28 KB
/
OneWireConnector.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <OneWire.h>
#include <DallasTemperature.h>
#include "SSR.h"
#define ONE_WIRE_BUS A0
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
uint8_t ** addresses;
uint8_t nAddresses;
DeviceAddress target;
float temperature = -1000000.00;
void setupSensors(void)
{
nAddresses = sensors.getDeviceCount();
addresses = (uint8_t**)malloc(sizeof(uint8_t *) * nAddresses);
for(uint8_t i = 0; i < nAddresses; i++)
{
addresses[i] = (uint8_t*)malloc(sizeof(DeviceAddress));
sensors.getAddress(addresses[i], i);
}
sensors.setResolution(9);
}
void setup(void)
{
SSRInit();
// Start up the library
sensors.begin();
setupSensors();
// start serial port
Serial.begin(9600);
}
union{
byte asBytes[4];
float asFloat;
} FloatBytes;
void setTemperature()
{
while(Serial.available()<4) delay(50);
for(int8_t i = 0; i < 4; ++i)
{
FloatBytes.asBytes[i] = Serial.read();
}
temperature = FloatBytes.asFloat;
Serial.println(temperature);
}
void setTarget()
{
while(Serial.available()<8) delay(50);
for(int8_t i = 0; i < 8; ++i)
{
target[i] = Serial.read();
}
for(uint8_t j = 0; j<8; ++j)
{
Serial.print("0x");
Serial.print(target[j], HEX);
Serial.print(" ");
}
Serial.println();
}
void printTemps()
{
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println(nAddresses);
for(uint8_t i = 0; i < nAddresses; i++)
{
for(uint8_t j = 0; j<8; j++)
{
Serial.print("0x");
Serial.print(addresses[i][j], HEX);
Serial.print(" ");
}
Serial.println();
Serial.println(sensors.getTempC(addresses[i]));
}
Serial.flush();
}
void cycle()
{
if(sensors.validAddress(target))
{
float reading = sensors.getTempC(target);
if(reading < 0.0f)
{
return;
}
float diff = temperature - reading;
if(diff > 0.0f)
{
//SSROn();
SSRPhase(diff);
} else {
SSROff();
}
} else {
SSROff();
}
}
void loop(void)
{
switch(Serial.read())
{
case 1:
printTemps();
break;
case 2:
setTemperature();
break;
case 3:
setTarget();
break;
case 4:
PumpOn();
break;
case 5:
PumpOff();
break;
case -1:
delay(500);
break;
}
cycle();
}