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 #define AJAR_LOW_PASS_TAU 200
86 byte ajar_low_pass_counter = 0;
87 byte ajar_low_pass_last_value = ajar_last_state;
88 byte get_ajar_status()
90 b = ( (digitalRead(AJAR_PIN) == LOW)? SHUT : AJAR );
91 ajar_low_pass_counter = ( (b == ajar_low_pass_last_value)? ajar_low_pass_counter + 1 : 0 );
92 ajar_low_pass_last_value = b;
93 if (ajar_low_pass_counter >= AJAR_LOW_PASS_TAU)
95 ajar_low_pass_counter = 0;
99 return ajar_last_state;
104 pinMode(AJAR_PIN, INPUT); // set pin to input
105 digitalWrite(AJAR_PIN, HIGH); // turn on pullup resistors
106 ajar_last_state = get_ajar_status();
113 pinMode(MANUAL_OPEN_PIN, INPUT); // set pin to input
114 digitalWrite(MANUAL_OPEN_PIN, HIGH); // turn on pullup resistors
116 pinMode(MANUAL_CLOSE_PIN, INPUT); // set pin to input
117 digitalWrite(MANUAL_CLOSE_PIN, HIGH); // turn on pullup resistors
119 debounce_state = DEBOUNCE_IDLE;
120 debounce_cnt = DEBOUNCE_DELAY;
123 boolean manual_open_pressed()
125 if(digitalRead(MANUAL_OPEN_PIN))
131 boolean manual_close_pressed()
133 if(digitalRead(MANUAL_CLOSE_PIN))
139 void start_debounce_timer() // this breaks millis() function, but who cares
141 debounce_cnt = DEBOUNCE_DELAY;
143 TCCR0A = 0; // no prescaler, WGM = 0 (normal)
145 OCR0A = 255; // 1+255 = 256 -> 16us @ 16 MHz
146 //OCR0A = 255; // 1+255 = 256 -> 12.8us @ 20 MHz
147 TCNT0 = 0; // reseting timer
148 TIMSK0 = 1<<OCF0A; // enable Interrupt
152 void stop_debounce_timer()
155 TCCR0B = 0; // no clock source
156 TIMSK0 = 0; // disable timer interrupt
159 ISR(TIMER0_COMPA_vect)
161 if(((debounce_state & DEBOUNCE_OPEN) && manual_open_pressed()) ||
162 ((debounce_state & DEBOUNCE_CLOSE) && manual_close_pressed())) {
167 debounce_state |= DEBOUNCE_FINISHED;
169 debounce_cnt = DEBOUNCE_DELAY;
172 boolean manual_open()
174 if(manual_open_pressed()) {
175 if(debounce_state & DEBOUNCE_CLOSE) {
176 stop_debounce_timer();
177 debounce_state = DEBOUNCE_IDLE;
181 if(debounce_state == DEBOUNCE_IDLE) {
182 debounce_state = DEBOUNCE_OPEN;
183 start_debounce_timer();
185 else if(debounce_state & DEBOUNCE_FINISHED) {
186 stop_debounce_timer();
187 debounce_state = DEBOUNCE_IDLE;
191 else if(debounce_state & DEBOUNCE_OPEN) {
192 stop_debounce_timer();
193 debounce_state = DEBOUNCE_IDLE;
199 boolean manual_close()
201 if(manual_close_pressed()) {
202 if(debounce_state & DEBOUNCE_OPEN) {
203 stop_debounce_timer();
204 debounce_state = DEBOUNCE_IDLE;
208 if(debounce_state == DEBOUNCE_IDLE) {
209 debounce_state = DEBOUNCE_CLOSE;
210 start_debounce_timer();
212 else if(debounce_state & DEBOUNCE_FINISHED) {
213 stop_debounce_timer();
214 debounce_state = DEBOUNCE_IDLE;
218 else if(debounce_state & DEBOUNCE_CLOSE) {
219 stop_debounce_timer();
220 debounce_state = DEBOUNCE_IDLE;
226 //********************************************************************//
237 DDRB = 0x0F; // set PortB 3:0 as output
241 byte step_table(byte step)
243 switch(step) { // 0011 xxxx, manual keys pull-ups stay active
259 digitalWrite(LEDS_GREEN_COMMON_PIN, HIGH);
260 digitalWrite(LEDS_RED_COMMON_PIN, HIGH);
266 pinMode(LEDS_GREEN_COMMON_PIN, OUTPUT);
267 pinMode(LEDS_RED_COMMON_PIN, OUTPUT);
271 byte led_table(byte led)
273 switch(led) { // xxxx xx00, leave RxD and TxD to 0
286 digitalWrite(LEDS_GREEN_COMMON_PIN, LOW);
287 digitalWrite(LEDS_RED_COMMON_PIN, HIGH);
292 digitalWrite(LEDS_GREEN_COMMON_PIN, HIGH);
293 digitalWrite(LEDS_RED_COMMON_PIN, LOW);
298 if(digitalRead(LEDS_GREEN_COMMON_PIN) == HIGH) {
299 digitalWrite(LEDS_GREEN_COMMON_PIN, LOW);
300 digitalWrite(LEDS_RED_COMMON_PIN, HIGH);
303 digitalWrite(LEDS_GREEN_COMMON_PIN, HIGH);
304 digitalWrite(LEDS_RED_COMMON_PIN, LOW);
310 void start_step_timer()
312 // timer 1: 2 ms, between stepper output state changes
313 TCCR1A = 0; // prescaler 1:256, WGM = 4 (CTC)
314 TCCR1B = 1<<WGM12 | 1<<CS12; //
315 OCR1A = 124; // (1+124)*256 = 32000 -> 2 ms @ 16 MHz
316 //OCR1A = 155; // (1+155)*256 = 40000 -> 2 ms @ 20 MHz
317 TCNT1 = 0; // reseting timer
318 TIMSK1 = 1<<OCIE1A; // enable Interrupt
321 void start_wait_timer()
323 // timer1: 250 ms, minimal delay between subsequent open/close
324 TCCR1A = 0; // prescaler 1:256, WGM = 0 (normal)
326 OCR1A = 15624; // (1+15624)*256 = 4000000 -> 250 ms @ 16 MHz
327 //OCR1A = 19530; // (1+19530)*256 = 5000000 -> 250 ms @ 20 MHz
328 TCNT1 = 0; // reseting timer
329 TIMSK1 = 1<<OCIE1A; // enable Interrupt
332 void start_error_timer()
334 // timer1: 500 ms, blinking leds with 1 Hz
335 TCCR1A = 0; // prescaler 1:256, WGM = 4 (CTC)
336 TCCR1B = 1<<WGM12 | 1<<CS12; //
337 OCR1A = 31249; // (1+31249)*256 = 8000000 -> 500 ms @ 16 MHz
338 //OCR1A = 39061; // (1+39061)*256 = 10000000 -> 500 ms @ 20 MHz
339 TCNT1 = 0; // reseting timer
340 TIMSK1 = 1<<OCIE1A; // enable Interrupt
343 void stop_timer() // stop the timer
346 TCCR1B = 0; // no clock source
347 TIMSK1 = 0; // disable timer interrupt
350 ISR(TIMER1_COMPA_vect)
352 // check if limit switch is active
353 if((current_state == OPENING && is_opened()) ||
354 (current_state == CLOSING && is_closed()))
358 if(current_state == OPENING)
363 current_state = WAIT;
368 if(current_state == OPENING || current_state == CLOSING) {
370 if(timeout_cnt >= MOVING_TIMEOUT) {
373 current_state = ERROR;
374 Serial.println("Error: open/close took too long!");
381 if(current_state == OPENING) { // next step (open)
382 PORTB = step_table(next_step);
387 else if(current_state == CLOSING) { // next step (close)
388 PORTB = step_table(next_step);
394 else if(current_state == WAIT) { // wait after last open/close finished -> idle
397 current_state = IDLE;
398 Serial.print("Status: ");
400 Serial.print("opened");
402 Serial.print("closed");
403 Serial.print(", idle");
404 if(get_ajar_status() == SHUT)
405 Serial.println(", shut");
407 Serial.println(", ajar");
410 else if(current_state == ERROR) {
414 else { // timer is useless stop it
420 if(led_delay_cnt >= LED_DELAY) {
423 PORTD = led_table(next_led);
425 if(current_state == OPENING) {
431 else if(current_state == CLOSING) {
439 //********************************************************************//
441 void reset_heartbeat()
443 digitalWrite(HEARTBEAT_PIN, HIGH);
449 digitalWrite(HEARTBEAT_PIN, LOW);
454 digitalWrite(HEARTBEAT_PIN, HIGH);
457 void init_heartbeat()
459 pinMode(HEARTBEAT_PIN, OUTPUT);
461 // timer 2: ~10 ms, timebase for heartbeat signal
462 TCCR2A = 1<<WGM21; // prescaler 1:1024, WGM = 2 (CTC)
463 TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20; //
464 OCR2A = 155; // (1+155)*1024 = 159744 -> ~10 ms @ 16 MHz
465 //OCR2A = 194; // (1+194)*1024 = 199680 -> ~10 ms @ 20 MHz
466 TCNT2 = 0; // reseting timer
467 TIMSK2 = 1<<OCIE2A; // enable Interrupt
471 // while running this gets called every ~10ms
472 ISR(TIMER2_COMPA_vect)
475 if(heartbeat_cnt == HEARTBEAT_DURATION)
477 else if(heartbeat_cnt >= HEARTBEAT_DELAY) {
483 //********************************************************************//
485 void reset_after_error()
492 current_state = IDLE;
496 current_state = CLOSING;
499 Serial.println("Ok, closing now");
507 current_state = OPENING;
516 current_state = CLOSING;
522 Serial.print("Status: ");
524 Serial.print("opened");
526 Serial.print("closed");
530 switch(current_state) {
531 case IDLE: Serial.print(", idle"); break;
532 case OPENING: Serial.print(", opening"); break;
533 case CLOSING: Serial.print(", closing"); break;
534 case WAIT: Serial.print(", waiting"); break;
535 default: Serial.print(", <undefined state>"); break;
537 if(get_ajar_status() == SHUT)
538 Serial.println(", shut");
540 Serial.println(", ajar");
555 current_state = IDLE;
557 // make sure door is locked after reset
562 current_state = CLOSING;
565 Serial.println("init complete");
570 if(Serial.available()) {
571 char command = Serial.read();
573 if(current_state == ERROR && command != CMD_RESET) {
574 Serial.println("Error: last open/close operation took too long!");
576 else if (command == CMD_RESET) {
579 else if (command == CMD_OPEN) {
580 if(current_state == IDLE) {
582 Serial.println("Already open");
585 Serial.println("Ok");
589 Serial.println("Error: Operation in progress");
591 else if (command == CMD_CLOSE) {
592 if(current_state == IDLE) {
594 Serial.println("Already closed");
597 Serial.println("Ok");
601 Serial.println("Error: Operation in progress");
603 else if (command == CMD_TOGGLE) {
604 if(current_state == IDLE) {
609 Serial.println("Ok");
612 Serial.println("Error: Operation in progress");
614 else if (command == CMD_STATUS)
617 Serial.println("Error: unknown command");
619 if(manual_open() && !is_opened() && (current_state == IDLE || current_state == ERROR)) {
620 Serial.println("open forced manually");
623 if(manual_close() && !is_closed() && (current_state == IDLE || current_state == ERROR)) {
624 Serial.println("close forced manually");
627 if(current_state == IDLE) {
637 byte a = get_ajar_status();
638 if(a != ajar_last_state)