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