2 #include <avr/interrupt.h>
5 #include <DallasTemperature.h>
7 //********************************************************************//
9 #define DATA_OUT_PIN 13
10 #define IR_MOVEMENT_PIN 9
11 #define ONE_WIRE_PIN 8
12 #define PANIC_BUTTON_PIN 7
13 #define IR_SAMPLE_DURATION 20000
14 #define IR_TRESHOLD 13000
15 #define PB_SAMPLE_DURATION 5000
16 #define PB_TRESHOLD 5000
18 OneWire onewire(ONE_WIRE_PIN);
19 DallasTemperature dallas_sensors(&onewire);
20 DeviceAddress onShieldTemp = { 0x10, 0xE7, 0x77, 0xD3, 0x01, 0x08, 0x00, 0x3F };
27 // offset is number of alphas (0.08ms)
29 const rf_bit_t zero_bit[] = { { 4, 1 },
35 const rf_bit_t one_bit[] = { { 12, 1 },
41 const rf_bit_t float_bit[] = { { 4, 1 },
47 const rf_bit_t sync_bit[] = { { 4, 1 },
51 typedef enum { ZERO = 0, ONE , FLOAT , SYNC } adbit_t;
52 typedef byte ad_bit_t;
54 typedef ad_bit_t word_t[WORD_LEN];
56 const rf_bit_t* bit_defs[] = { zero_bit, one_bit, float_bit, sync_bit };
62 const ad_bit_t* current_word;
63 byte volatile frame_finished = 1;
87 const word_t words[] = {
88 { ZERO, ZERO, FLOAT, FLOAT, ZERO, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // A1_ON
89 { ZERO, ZERO, FLOAT, FLOAT, ZERO, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, SYNC }, // A1_OFF
90 { ZERO, ZERO, FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // A2_ON
91 { ZERO, ZERO, FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, SYNC }, // A2_OFF
93 { FLOAT, ZERO, FLOAT, FLOAT, ZERO, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // B1_ON
94 { FLOAT, ZERO, FLOAT, FLOAT, ZERO, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, SYNC }, // B1_OFF
95 { FLOAT, ZERO, FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // B2_ON
96 { FLOAT, ZERO, FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, SYNC }, // B2_OFF
98 { ZERO, FLOAT, FLOAT, FLOAT, ZERO, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // C1_ON
99 { ZERO, FLOAT, FLOAT, FLOAT, ZERO, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, SYNC }, // C1_OFF
100 { ZERO, FLOAT, FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // C2_ON
101 { ZERO, FLOAT, FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, SYNC }, // C2_OFF
103 { FLOAT, FLOAT, FLOAT, FLOAT, ZERO, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // D1_ON
104 { FLOAT, FLOAT, FLOAT, FLOAT, ZERO, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, SYNC }, // D1_OFF
105 { FLOAT, FLOAT, FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // D2_ON
106 { FLOAT, FLOAT, FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, SYNC } // D2_OFF
110 //********************************************************************//
115 TCCR1A = 0; // prescaler 1:8, WGM = 4 (CTC)
116 TCCR1B = 1<<WGM12 | 1<<CS11; //
117 OCR1A = 159; // (1+159)*8 = 1280 -> 0.08ms @ 16 MHz -> 1*alpha
118 // OCR1A = 207; // (1+207)*8 = 1664 -> 0.104ms @ 16 MHz -> 1*alpha
119 TCNT1 = 0; // reseting timer
120 TIMSK1 = 1<<OCIE1A; // enable Interrupt
123 void stop_timer() // stop the timer
126 TCCR1B = 0; // no clock source
127 TIMSK1 = 0; // disable timer interrupt
130 void init_word(const word_t w)
137 if(bit_defs[current_word[bit_cnt]][chunk_cnt].state)
138 digitalWrite(DATA_OUT_PIN, HIGH);
140 digitalWrite(DATA_OUT_PIN, LOW);
145 ISR(TIMER1_COMPA_vect)
148 if(alpha_cnt < bit_defs[current_word[bit_cnt]][chunk_cnt].offset)
152 if(bit_defs[current_word[bit_cnt]][chunk_cnt].offset != 0) {
153 if(bit_defs[current_word[bit_cnt]][chunk_cnt].state)
154 digitalWrite(DATA_OUT_PIN, HIGH);
156 digitalWrite(DATA_OUT_PIN, LOW);
161 if(bit_cnt < WORD_LEN) {
164 if(bit_defs[current_word[bit_cnt]][chunk_cnt].state)
165 digitalWrite(DATA_OUT_PIN, HIGH);
167 digitalWrite(DATA_OUT_PIN, LOW);
171 digitalWrite(DATA_OUT_PIN, LOW);
174 if(word_cnt < FRAME_LEN)
175 init_word(current_word);
183 void send_frame(const word_t w)
194 //********************************************************************//
196 void printTemperature(DeviceAddress deviceAddress)
198 dallas_sensors.requestTemperatures();
199 float tempC = dallas_sensors.getTempC(deviceAddress);
200 Serial.print("Temp C: ");
201 Serial.println(tempC);
202 //Serial.print(" Temp F: ");
203 //Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
206 //********************************************************************//
210 pinMode(DATA_OUT_PIN, OUTPUT);
211 digitalWrite(DATA_OUT_PIN, LOW);
212 pinMode(IR_MOVEMENT_PIN, INPUT); // set pin to input
213 digitalWrite(IR_MOVEMENT_PIN, LOW); // turn off pullup resistors
214 pinMode(PANIC_BUTTON_PIN, INPUT); // set pin to input
215 digitalWrite(PANIC_BUTTON_PIN, HIGH); // turn on pullup resistors
218 onewire.reset_search();
219 dallas_sensors.begin();
220 //in case we change temp sensor:
221 if (!dallas_sensors.getAddress(onShieldTemp, 0))
222 Serial.println("Unable to find address for Device 0");
223 dallas_sensors.setResolution(onShieldTemp, 9);
228 unsigned int ir_time=IR_SAMPLE_DURATION;
229 unsigned int ir_count=0;
230 unsigned int pb_time=PB_SAMPLE_DURATION;
231 unsigned int pb_count=0;
237 ir_count += (digitalRead(IR_MOVEMENT_PIN) == HIGH);
238 pb_count += (digitalRead(PANIC_BUTTON_PIN) == LOW);
242 if (ir_count >= IR_TRESHOLD)
243 Serial.println("movement");
244 ir_time=IR_SAMPLE_DURATION;
250 if (pb_count >= PB_TRESHOLD)
251 Serial.println("PanicButton");
252 pb_time=PB_SAMPLE_DURATION;
256 if(Serial.available()) {
257 char command = Serial.read();
260 send_frame(words[A1_ON]);
261 else if(command == 'a')
262 send_frame(words[A1_OFF]);
263 else if(command == 'w')
264 send_frame(words[A2_ON]);
265 else if(command == 's')
266 send_frame(words[A2_OFF]);
268 else if(command == 'e')
269 send_frame(words[B1_ON]);
270 else if(command == 'd')
271 send_frame(words[B1_OFF]);
272 else if(command == 'r')
273 send_frame(words[B2_ON]);
274 else if(command == 'f')
275 send_frame(words[B2_OFF]);
277 else if(command == 't')
278 send_frame(words[C1_ON]);
279 else if(command == 'g')
280 send_frame(words[C1_OFF]);
281 else if(command == 'z')
282 send_frame(words[C2_ON]);
283 else if(command == 'h')
284 send_frame(words[C2_OFF]);
286 else if(command == 'u')
287 send_frame(words[D1_ON]);
288 else if(command == 'j')
289 send_frame(words[D1_OFF]);
290 else if(command == 'i')
291 send_frame(words[D2_ON]);
292 else if(command == 'k')
293 send_frame(words[D2_OFF]);
294 else if(command == 'T')
295 printTemperature(onShieldTemp);