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