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