2 #include <avr/interrupt.h>
4 //********************************************************************//
6 #define HEARTBEAT_PIN 15 // blinking led indicating that system is active
7 #define HEARTBEAT_DURATION 10 // *10 ms, duration of heartbeat pulse
8 #define HEARTBEAT_DELAY 200 // *10 ms, 1/heartbeat-frequency
14 #define LEDS_GREEN_COMMON_PIN 16
15 #define LEDS_RED_COMMON_PIN 17
16 #define LED_DELAY 50 // *2 ms, between led shifts
17 int led_delay_cnt = 0;
20 #define LIMIT_OPENED_PIN 18 // A4: limit switch for open
21 #define LIMIT_CLOSED_PIN 19 // A5: limit switch for close
23 #define AJAR_PIN 14 // input pin for reed relais (door ajar/shut)
26 byte ajar_last_state = SHUT;
28 #define MANUAL_OPEN_PIN 12 // keys for manual open and close
29 #define MANUAL_CLOSE_PIN 13 //
30 #define DEBOUNCE_DELAY 6250 // * 16us = 100ms
31 #define DEBOUNCE_IDLE 0 // currently no debouncing
32 #define DEBOUNCE_OPEN 1 // debouncing open key
33 #define DEBOUNCE_CLOSE 2 // debouncing close key
34 #define DEBOUNCE_FINISHED 4 // debouncing finished
38 #define IDLE 0 // close and open may be called
39 #define OPENING 1 // opening, only 's' command is allowed
40 #define CLOSING 2 // closing, onyl 's' command is allowed
41 #define WAIT 3 // wait some time after open or close and hold last step
42 #define ERROR 4 // an error occured
46 #define CMD_TOGGLE 't'
47 #define CMD_STATUS 's'
50 #define STEPPER_OFF 0x30
51 byte current_state = IDLE; // current state of internal state machine
52 byte next_step = 0; // step counter 0 .. 3
53 #define MOVING_TIMEOUT 1600 // *2 ms, in case limit switches don't work stop and report an error
54 int timeout_cnt = 0; // counts up to MOVING_TIMEOUT
56 //********************************************************************//
60 pinMode(LIMIT_OPENED_PIN, INPUT); // set pin to input
61 digitalWrite(LIMIT_OPENED_PIN, HIGH); // turn on pullup resistors
63 pinMode(LIMIT_CLOSED_PIN, INPUT); // set pin to input
64 digitalWrite(LIMIT_CLOSED_PIN, HIGH); // turn on pullup resistors
69 if(digitalRead(LIMIT_OPENED_PIN))
77 if(digitalRead(LIMIT_CLOSED_PIN))
85 byte get_ajar_status()
87 if(digitalRead(AJAR_PIN) == LOW)
95 pinMode(AJAR_PIN, INPUT); // set pin to input
96 digitalWrite(AJAR_PIN, HIGH); // turn on pullup resistors
97 ajar_last_state = get_ajar_status();
104 pinMode(MANUAL_OPEN_PIN, INPUT); // set pin to input
105 digitalWrite(MANUAL_OPEN_PIN, HIGH); // turn on pullup resistors
107 pinMode(MANUAL_CLOSE_PIN, INPUT); // set pin to input
108 digitalWrite(MANUAL_CLOSE_PIN, HIGH); // turn on pullup resistors
110 debounce_state = DEBOUNCE_IDLE;
111 debounce_cnt = DEBOUNCE_DELAY;
114 boolean manual_open_pressed()
116 if(digitalRead(MANUAL_OPEN_PIN))
122 boolean manual_close_pressed()
124 if(digitalRead(MANUAL_CLOSE_PIN))
130 void start_debounce_timer() // this breaks millis() function, but who cares
132 debounce_cnt = DEBOUNCE_DELAY;
134 TCCR0A = 0; // no prescaler, WGM = 0 (normal)
136 OCR0A = 255; // 1+255 = 256 -> 16us @ 16 MHz
137 //OCR0A = 255; // 1+255 = 256 -> 12.8us @ 20 MHz
138 TCNT0 = 0; // reseting timer
139 TIMSK0 = 1<<OCF0A; // enable Interrupt
143 void stop_debounce_timer()
146 TCCR0B = 0; // no clock source
147 TIMSK0 = 0; // disable timer interrupt
150 ISR(TIMER0_COMPA_vect)
152 if(((debounce_state & DEBOUNCE_OPEN) && manual_open_pressed()) ||
153 ((debounce_state & DEBOUNCE_CLOSE) && manual_close_pressed())) {
158 debounce_state |= DEBOUNCE_FINISHED;
160 debounce_cnt = DEBOUNCE_DELAY;
163 boolean manual_open()
165 if(manual_open_pressed()) {
166 if(debounce_state & DEBOUNCE_CLOSE) {
167 stop_debounce_timer();
168 debounce_state = DEBOUNCE_IDLE;
172 if(debounce_state == DEBOUNCE_IDLE) {
173 debounce_state = DEBOUNCE_OPEN;
174 start_debounce_timer();
176 else if(debounce_state & DEBOUNCE_FINISHED) {
177 stop_debounce_timer();
178 debounce_state = DEBOUNCE_IDLE;
182 else if(debounce_state & DEBOUNCE_OPEN) {
183 stop_debounce_timer();
184 debounce_state = DEBOUNCE_IDLE;
190 boolean manual_close()
192 if(manual_close_pressed()) {
193 if(debounce_state & DEBOUNCE_OPEN) {
194 stop_debounce_timer();
195 debounce_state = DEBOUNCE_IDLE;
199 if(debounce_state == DEBOUNCE_IDLE) {
200 debounce_state = DEBOUNCE_CLOSE;
201 start_debounce_timer();
203 else if(debounce_state & DEBOUNCE_FINISHED) {
204 stop_debounce_timer();
205 debounce_state = DEBOUNCE_IDLE;
209 else if(debounce_state & DEBOUNCE_CLOSE) {
210 stop_debounce_timer();
211 debounce_state = DEBOUNCE_IDLE;
217 //********************************************************************//
228 DDRB = 0x0F; // set PortB 3:0 as output
232 byte step_table(byte step)
234 switch(step) { // 0011 xxxx, manual keys pull-ups stay active
250 digitalWrite(LEDS_GREEN_COMMON_PIN, HIGH);
251 digitalWrite(LEDS_RED_COMMON_PIN, HIGH);
257 pinMode(LEDS_GREEN_COMMON_PIN, OUTPUT);
258 pinMode(LEDS_RED_COMMON_PIN, OUTPUT);
262 byte led_table(byte led)
264 switch(led) { // xxxx xx00, leave RxD and TxD to 0
277 digitalWrite(LEDS_GREEN_COMMON_PIN, LOW);
278 digitalWrite(LEDS_RED_COMMON_PIN, HIGH);
283 digitalWrite(LEDS_GREEN_COMMON_PIN, HIGH);
284 digitalWrite(LEDS_RED_COMMON_PIN, LOW);
289 if(digitalRead(LEDS_GREEN_COMMON_PIN) == HIGH) {
290 digitalWrite(LEDS_GREEN_COMMON_PIN, LOW);
291 digitalWrite(LEDS_RED_COMMON_PIN, HIGH);
294 digitalWrite(LEDS_GREEN_COMMON_PIN, HIGH);
295 digitalWrite(LEDS_RED_COMMON_PIN, LOW);
301 void start_step_timer()
303 // timer 1: 2 ms, between stepper output state changes
304 TCCR1A = 0; // prescaler 1:256, WGM = 4 (CTC)
305 TCCR1B = 1<<WGM12 | 1<<CS12; //
306 OCR1A = 124; // (1+124)*256 = 32000 -> 2 ms @ 16 MHz
307 //OCR1A = 155; // (1+155)*256 = 40000 -> 2 ms @ 20 MHz
308 TCNT1 = 0; // reseting timer
309 TIMSK1 = 1<<OCIE1A; // enable Interrupt
312 void start_wait_timer()
314 // timer1: 250 ms, minimal delay between subsequent open/close
315 TCCR1A = 0; // prescaler 1:256, WGM = 0 (normal)
317 OCR1A = 15624; // (1+15624)*256 = 4000000 -> 250 ms @ 16 MHz
318 //OCR1A = 19530; // (1+19530)*256 = 5000000 -> 250 ms @ 20 MHz
319 TCNT1 = 0; // reseting timer
320 TIMSK1 = 1<<OCIE1A; // enable Interrupt
323 void start_error_timer()
325 // timer1: 500 ms, blinking leds with 1 Hz
326 TCCR1A = 0; // prescaler 1:256, WGM = 4 (CTC)
327 TCCR1B = 1<<WGM12 | 1<<CS12; //
328 OCR1A = 31249; // (1+31249)*256 = 8000000 -> 500 ms @ 16 MHz
329 //OCR1A = 39061; // (1+39061)*256 = 10000000 -> 500 ms @ 20 MHz
330 TCNT1 = 0; // reseting timer
331 TIMSK1 = 1<<OCIE1A; // enable Interrupt
334 void stop_timer() // stop the timer
337 TCCR1B = 0; // no clock source
338 TIMSK1 = 0; // disable timer interrupt
341 ISR(TIMER1_COMPA_vect)
343 // check if limit switch is active
344 if((current_state == OPENING && is_opened()) ||
345 (current_state == CLOSING && is_closed()))
349 if(current_state == OPENING)
354 current_state = WAIT;
359 if(current_state == OPENING || current_state == CLOSING) {
361 if(timeout_cnt >= MOVING_TIMEOUT) {
364 current_state = ERROR;
365 Serial.println("Error: open/close took too long!");
372 if(current_state == OPENING) { // next step (open)
373 PORTB = step_table(next_step);
378 else if(current_state == CLOSING) { // next step (close)
379 PORTB = step_table(next_step);
385 else if(current_state == WAIT) { // wait after last open/close finished -> idle
388 current_state = IDLE;
389 Serial.print("Status: ");
391 Serial.print("opened");
393 Serial.print("closed");
394 Serial.print(", idle");
395 if(get_ajar_status() == SHUT)
396 Serial.println(", shut");
398 Serial.println(", ajar");
401 else if(current_state == ERROR) {
405 else { // timer is useless stop it
411 if(led_delay_cnt >= LED_DELAY) {
414 PORTD = led_table(next_led);
416 if(current_state == OPENING) {
422 else if(current_state == CLOSING) {
430 //********************************************************************//
432 void reset_heartbeat()
434 digitalWrite(HEARTBEAT_PIN, HIGH);
440 digitalWrite(HEARTBEAT_PIN, LOW);
445 digitalWrite(HEARTBEAT_PIN, HIGH);
448 void init_heartbeat()
450 pinMode(HEARTBEAT_PIN, OUTPUT);
452 // timer 2: ~10 ms, timebase for heartbeat signal
453 TCCR2A = 1<<WGM21; // prescaler 1:1024, WGM = 2 (CTC)
454 TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20; //
455 OCR2A = 155; // (1+155)*1024 = 159744 -> ~10 ms @ 16 MHz
456 //OCR2A = 194; // (1+194)*1024 = 199680 -> ~10 ms @ 20 MHz
457 TCNT2 = 0; // reseting timer
458 TIMSK2 = 1<<OCIE2A; // enable Interrupt
462 // while running this gets called every ~10ms
463 ISR(TIMER2_COMPA_vect)
466 if(heartbeat_cnt == HEARTBEAT_DURATION)
468 else if(heartbeat_cnt >= HEARTBEAT_DELAY) {
474 //********************************************************************//
476 void reset_after_error()
483 current_state = IDLE;
487 current_state = CLOSING;
490 Serial.println("Ok, closing now");
498 current_state = OPENING;
507 current_state = CLOSING;
513 Serial.print("Status: ");
515 Serial.print("opened");
517 Serial.print("closed");
521 switch(current_state) {
522 case IDLE: Serial.print(", idle"); break;
523 case OPENING: Serial.print(", opening"); break;
524 case CLOSING: Serial.print(", closing"); break;
525 case WAIT: Serial.print(", waiting"); break;
526 default: Serial.print(", <undefined state>"); break;
528 if(get_ajar_status() == SHUT)
529 Serial.println(", shut");
531 Serial.println(", ajar");
546 current_state = IDLE;
548 // make sure door is locked after reset
553 current_state = CLOSING;
556 Serial.println("init complete");
561 if(Serial.available()) {
562 char command = Serial.read();
564 if(current_state == ERROR && command != CMD_RESET) {
565 Serial.println("Error: last open/close operation took too long!");
567 else if (command == CMD_RESET) {
570 else if (command == CMD_OPEN) {
571 if(current_state == IDLE) {
573 Serial.println("Already open");
576 Serial.println("Ok");
580 Serial.println("Error: Operation in progress");
582 else if (command == CMD_CLOSE) {
583 if(current_state == IDLE) {
585 Serial.println("Already closed");
588 Serial.println("Ok");
592 Serial.println("Error: Operation in progress");
594 else if (command == CMD_TOGGLE) {
595 if(current_state == IDLE) {
600 Serial.println("Ok");
603 Serial.println("Error: Operation in progress");
605 else if (command == CMD_STATUS)
608 Serial.println("Error: unknown command");
610 if(manual_open() && !is_opened() && (current_state == IDLE || current_state == ERROR)) {
611 Serial.println("open forced manually");
614 if(manual_close() && !is_closed() && (current_state == IDLE || current_state == ERROR)) {
615 Serial.println("close forced manually");
618 if(current_state == IDLE) {
628 byte a = get_ajar_status();
629 if(a != ajar_last_state)