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