1 |
// Lamps controller |
1 |
// Lamps controller |
- |
|
2 |
#define VERSION "$Revision: 3651 $" |
- |
|
3 |
|
2 |
|
4 |
|
3 |
#include <OneWire.h> |
5 |
#include <OneWire.h> |
- |
|
6 |
#include <EEPROM.h> |
- |
|
7 |
|
4 |
|
8 |
|
5 |
#define LAG 400 // dellay in ms between lamp relay switching |
9 |
#define LAG 400 // dellay in ms between lamp relay switching |
6 |
|
10 |
|
7 |
// Triacs |
11 |
// Triacs |
8 |
int t1 = 2; // PD2 - trafo for gas lamps |
12 |
int t1 = 2; // PD2 - trafo for gas lamps |
9 |
int t2 = 3; // PD3 - relay for switching between lamps |
13 |
int t2 = 3; // PD3 - relay for switching between lamps |
10 |
int t3 = 4; // PD4 - halogen lamp |
14 |
int t3 = 4; // PD4 - halogen lamp |
11 |
int t4 = 5; // PD5 - focuser |
15 |
int t4 = 5; // PD5 - focuser |
12 |
int t5 = 6; // PD6 |
16 |
int t5 = 6; // PD6 - COLORES |
13 |
int t6 = 7; // PD7 |
17 |
int t6 = 7; // PD7 |
14 |
int t7 = 8; // PB0 |
18 |
int t7 = 8; // PB0 |
15 |
int t8 = 9; // PB1 |
19 |
int t8 = 9; // PB1 |
16 |
|
20 |
|
17 |
// DS18S20 Temperature chip |
21 |
// DS18S20 Temperature chip |
18 |
OneWire ds(12); // PB4 1-Wire pin (needs pull-up to Vcc) |
22 |
OneWire ds(12); // PB4 1-Wire pin (needs pull-up to Vcc) |
19 |
byte addr[8]; // Thermometer address |
23 |
byte addr[8]; // Thermometer address |
20 |
|
24 |
|
21 |
int n; // Counter |
25 |
int n; // Counter |
22 |
char state; // State of Gas Lamps |
26 |
char state; // State of Gas Lamps |
23 |
|
27 |
|
24 |
char deleni16[16]={'0','1','1','2','3','3','4','4','5','6','6','7','7','8','9','9'}; |
28 |
char deleni16[16]={'0','1','1','2','3','3','4','4','5','6','6','7','7','8','9','9'}; |
25 |
|
29 |
|
- |
|
30 |
void info () // Print an information string |
- |
|
31 |
{ |
- |
|
32 |
Serial.print("Lamps Controller "); |
- |
|
33 |
Serial.println(VERSION); |
- |
|
34 |
Serial.println("Commands: abcdefghABCDEFGHiS"); |
- |
|
35 |
} |
- |
|
36 |
|
26 |
int temperature () |
37 |
int temperature () // Read temperature from Dallas |
27 |
{ |
38 |
{ |
28 |
int i, temp; |
39 |
int i, temp; |
29 |
byte data[12]; |
40 |
byte data[12]; |
30 |
|
41 |
|
31 |
if (OneWire::crc8 (addr, 7) != addr[7]) |
42 |
if (OneWire::crc8 (addr, 7) != addr[7]) |
32 |
{ |
43 |
{ |
33 |
Serial.print("CRC is not valid!\n"); |
44 |
Serial.print("CRC is not valid!\n"); |
34 |
return 0; |
45 |
return 0; |
35 |
} |
46 |
} |
36 |
|
47 |
|
37 |
ds.reset(); |
48 |
ds.reset(); |
38 |
ds.select(addr); |
49 |
ds.select(addr); |
39 |
ds.write(0x44, 1); // start conversion, with parasite power on at the end |
50 |
ds.write(0x44, 1); // start conversion, with parasite power on at the end |
40 |
|
51 |
|
41 |
delay(800); // maybe 750ms is enough, maybe not |
52 |
delay(800); // maybe 750ms is enough, maybe not |
42 |
|
53 |
|
43 |
ds.reset(); |
54 |
ds.reset(); |
44 |
ds.select(addr); |
55 |
ds.select(addr); |
45 |
ds.write(0xBE); // Read Scratchpad |
56 |
ds.write(0xBE); // Read Scratchpad |
46 |
|
57 |
|
47 |
for ( i = 0; i < 9; i++) // we need 9 bytes |
58 |
for ( i = 0; i < 9; i++) // we need 9 bytes |
48 |
{ |
59 |
{ |
49 |
data[i] = ds.read(); |
60 |
data[i] = ds.read(); |
50 |
} |
61 |
} |
51 |
|
62 |
|
52 |
temp = (data[1] << 8) + data[0]; //take the two bytes from the response relating to temperature |
63 |
temp = (data[1] << 8) + data[0]; //take the two bytes from the response relating to temperature |
53 |
// temp = temp >> 4; //divide by 16 to get pure celcius readout |
64 |
// temp = temp >> 4; //divide by 16 to get pure celcius readout |
54 |
|
65 |
|
55 |
return temp; |
66 |
return temp; |
56 |
} |
67 |
} |
57 |
|
68 |
|
- |
|
69 |
void pstatus() // Print status to serial line |
- |
|
70 |
{ |
- |
|
71 |
int t; // Temperature |
- |
|
72 |
|
- |
|
73 |
t=temperature(); // Read temperature |
- |
|
74 |
Serial.print (t >> 4); |
- |
|
75 |
Serial.print ("."); |
- |
|
76 |
Serial.print (deleni16[t & 0xf]); |
- |
|
77 |
Serial.print (' '); |
- |
|
78 |
for (n=1;n<=8;n++) |
- |
|
79 |
{ |
- |
|
80 |
if(digitalRead(n+1)) |
- |
|
81 |
{ |
- |
|
82 |
Serial.print('t'); |
- |
|
83 |
} |
- |
|
84 |
else |
- |
|
85 |
{ |
- |
|
86 |
Serial.print('T'); |
- |
|
87 |
} |
- |
|
88 |
Serial.print(n, DEC); |
- |
|
89 |
Serial.print(' '); |
- |
|
90 |
} |
- |
|
91 |
Serial.println(); |
- |
|
92 |
} |
- |
|
93 |
|
- |
|
94 |
|
58 |
// the setup routine runs once when you press reset: |
95 |
// the setup routine runs once when you press reset: |
59 |
void setup() |
96 |
void setup() |
60 |
{ |
97 |
{ |
61 |
// initialize the digital pin as an output and switch off |
98 |
// initialize the digital pin as an output and switch off |
62 |
digitalWrite(t1, HIGH); |
99 |
digitalWrite(t1, HIGH); |
63 |
digitalWrite(t2, HIGH); |
100 |
digitalWrite(t2, HIGH); |
64 |
digitalWrite(t3, HIGH); |
101 |
digitalWrite(t3, HIGH); |
65 |
digitalWrite(t4, HIGH); |
102 |
digitalWrite(t4, HIGH); |
66 |
digitalWrite(t5, HIGH); |
103 |
digitalWrite(t5, HIGH); |
67 |
digitalWrite(t6, HIGH); |
104 |
digitalWrite(t6, HIGH); |
68 |
digitalWrite(t7, HIGH); |
105 |
digitalWrite(t7, HIGH); |
69 |
digitalWrite(t8, HIGH); |
106 |
digitalWrite(t8, HIGH); |
70 |
pinMode(t1, OUTPUT); |
107 |
pinMode(t1, OUTPUT); |
71 |
pinMode(t2, OUTPUT); |
108 |
pinMode(t2, OUTPUT); |
72 |
pinMode(t3, OUTPUT); |
109 |
pinMode(t3, OUTPUT); |
73 |
pinMode(t4, OUTPUT); |
110 |
pinMode(t4, OUTPUT); |
74 |
pinMode(t5, OUTPUT); |
111 |
pinMode(t5, OUTPUT); |
75 |
pinMode(t6, OUTPUT); |
112 |
pinMode(t6, OUTPUT); |
76 |
pinMode(t7, OUTPUT); |
113 |
pinMode(t7, OUTPUT); |
77 |
pinMode(t8, OUTPUT); |
114 |
pinMode(t8, OUTPUT); |
78 |
state = 'a'; |
115 |
state = 'a'; |
79 |
|
116 |
|
80 |
// initialize the serial port |
117 |
// initialize the serial port |
81 |
Serial.begin(9600); |
118 |
Serial.begin(9600); |
82 |
Serial.println(); |
119 |
Serial.println(); |
83 |
Serial.println("Cvak."); |
120 |
Serial.println("Cvak."); |
84 |
|
121 |
|
85 |
// OneWire init |
122 |
// OneWire init |
86 |
ds.reset_search(); |
123 |
ds.reset_search(); |
87 |
if (!ds.search(addr)) // search for next thermometer |
124 |
if (!ds.search(addr)) // search for next thermometer |
88 |
{ |
125 |
{ |
89 |
Serial.println("Thermometer error."); |
126 |
Serial.println("Thermometer error."); |
90 |
ds.reset_search(); |
127 |
ds.reset_search(); |
91 |
delay(250); |
128 |
delay(250); |
92 |
return; |
129 |
return; |
93 |
} |
130 |
} |
94 |
|
131 |
|
- |
|
132 |
for (n=1;n<=8;n++) |
- |
|
133 |
{ |
- |
|
134 |
digitalWrite(n+1,EEPROM.read(n)); // Read saved states from EEPROM |
- |
|
135 |
} |
- |
|
136 |
|
95 |
Serial.println("Hmmm...."); |
137 |
Serial.println("Hmmm...."); |
- |
|
138 |
info(); |
- |
|
139 |
pstatus(); |
96 |
} |
140 |
} |
97 |
|
141 |
|
98 |
// the loop routine runs over and over again forever: |
142 |
// the loop routine runs over and over again forever: |
99 |
void loop() |
143 |
void loop() |
100 |
{ |
144 |
{ |
101 |
byte inByte; // Character from serial line |
145 |
byte inByte; // Character from serial line |
102 |
int t; // Temperature |
- |
|
103 |
|
146 |
|
104 |
if (Serial.available() > 0) // wait for a char |
147 |
if (Serial.available() > 0) // wait for a char |
105 |
{ |
148 |
{ |
106 |
// get incoming byte: |
149 |
// get incoming byte: |
107 |
inByte = Serial.read(); |
150 |
inByte = Serial.read(); |
108 |
|
151 |
|
109 |
switch (inByte) |
152 |
switch (inByte) |
110 |
{ |
153 |
{ |
111 |
|
154 |
|
112 |
case 'A': // Gas Lamp 1 ON |
155 |
case 'A': // Gas Lamp 1 ON |
113 |
if (state != 'A') |
156 |
if (state != 'A') |
114 |
{ |
157 |
{ |
115 |
digitalWrite(t1, HIGH); |
158 |
digitalWrite(t1, HIGH); |
116 |
delay(LAG); |
159 |
delay(LAG); |
117 |
digitalWrite(t2, LOW); |
160 |
digitalWrite(t2, LOW); |
118 |
delay(LAG); |
161 |
delay(LAG); |
119 |
digitalWrite(t1, LOW); |
162 |
digitalWrite(t1, LOW); |
120 |
state = 'A'; |
163 |
state = 'A'; |
121 |
} |
164 |
} |
122 |
break; |
165 |
break; |
123 |
|
166 |
|
124 |
case 'B': // Gas Lamp 2 ON |
167 |
case 'B': // Gas Lamp 2 ON |
125 |
if (state != 'B') |
168 |
if (state != 'B') |
126 |
{ |
169 |
{ |
127 |
digitalWrite(t1, HIGH); |
170 |
digitalWrite(t1, HIGH); |
128 |
delay(LAG); |
171 |
delay(LAG); |
129 |
digitalWrite(t2, HIGH); |
172 |
digitalWrite(t2, HIGH); |
130 |
delay(LAG); |
173 |
delay(LAG); |
131 |
digitalWrite(t1, LOW); |
174 |
digitalWrite(t1, LOW); |
132 |
state = 'B'; |
175 |
state = 'B'; |
133 |
} |
176 |
} |
134 |
break; |
177 |
break; |
135 |
|
178 |
|
136 |
case 'a': // Gas Lamp 1 OFF |
179 |
case 'a': // Gas Lamp 1 OFF |
137 |
digitalWrite(t1, HIGH); |
180 |
digitalWrite(t1, HIGH); |
138 |
delay(LAG); |
181 |
delay(LAG); |
139 |
digitalWrite(t2, HIGH); |
182 |
digitalWrite(t2, HIGH); |
140 |
state = 'a'; |
183 |
state = 'a'; |
141 |
break; |
184 |
break; |
142 |
|
185 |
|
143 |
case 'b': // Gas Lamp 2 OFF |
186 |
case 'b': // Gas Lamp 2 OFF |
144 |
digitalWrite(t1, HIGH); |
187 |
digitalWrite(t1, HIGH); |
145 |
delay(LAG); |
188 |
delay(LAG); |
146 |
digitalWrite(t2, HIGH); |
189 |
digitalWrite(t2, HIGH); |
147 |
state = 'b'; |
190 |
state = 'b'; |
148 |
break; |
191 |
break; |
- |
|
192 |
|
- |
|
193 |
case 'i': // Print Info |
- |
|
194 |
info(); |
- |
|
195 |
break; |
- |
|
196 |
|
- |
|
197 |
case 'S': // Save states to EEPROM |
- |
|
198 |
for (n=1;n<=8;n++) |
- |
|
199 |
{ |
- |
|
200 |
EEPROM.write(n,digitalRead(n+1)); |
- |
|
201 |
} |
- |
|
202 |
break; |
149 |
} |
203 |
} |
150 |
|
204 |
|
151 |
if ( (inByte >= 'c') and (inByte <= 'h')) // Switch OFF other triacs |
205 |
if ( (inByte >= 'c') and (inByte <= 'h')) // Switch OFF other triacs |
152 |
{ |
206 |
{ |
153 |
digitalWrite(inByte-'a'+2,HIGH); |
207 |
digitalWrite(inByte-'a'+2,HIGH); |
154 |
} |
208 |
} |
155 |
|
209 |
|
156 |
if ( (inByte >= 'C') and (inByte <= 'H')) // Switch ON other triacs |
210 |
if ( (inByte >= 'C') and (inByte <= 'H')) // Switch ON other triacs |
157 |
{ |
211 |
{ |
158 |
digitalWrite(inByte-'A'+2, LOW); |
212 |
digitalWrite(inByte-'A'+2, LOW); |
159 |
} |
213 |
} |
160 |
|
214 |
|
161 |
// Send status to serial line |
215 |
pstatus(); // Print states |
162 |
t=temperature(); // Read temperature |
- |
|
163 |
Serial.print (t >> 4); |
- |
|
164 |
Serial.print ("."); |
- |
|
165 |
Serial.print (deleni16[t & 0xf]); |
- |
|
166 |
Serial.print (' '); |
- |
|
167 |
for (n=1;n<=8;n++) |
- |
|
168 |
{ |
- |
|
169 |
if(digitalRead(n+1)) |
- |
|
170 |
{ |
- |
|
171 |
Serial.print('t'); |
- |
|
172 |
} |
- |
|
173 |
else |
- |
|
174 |
{ |
- |
|
175 |
Serial.print('T'); |
- |
|
176 |
} |
- |
|
177 |
Serial.print(n, DEC); |
- |
|
178 |
Serial.print(' '); |
- |
|
179 |
} |
- |
|
180 |
Serial.println(); |
- |
|
181 |
|
- |
|
182 |
} |
216 |
} |
183 |
} |
217 |
} |