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)
24 boolean ajar_last_state = false;
26 #define MANUAL_OPEN_PIN 12 // keys for manual open and close
27 #define MANUAL_CLOSE_PIN 13 //
28 #define DEBOUNCE_DELAY 6250 // * 16us = 100ms
29 #define DEBOUNCE_IDLE 0 // currently no debouncing
30 #define DEBOUNCE_OPEN 1 // debouncing open key
31 #define DEBOUNCE_CLOSE 2 // debouncing close key
32 #define DEBOUNCE_FINISHED 4 // debouncing finished
36 #define IDLE 0 // close and open may be called
37 #define OPENING 1 // opening, only 's' command is allowed
38 #define CLOSING 2 // closing, onyl 's' command is allowed
39 #define WAIT 3 // wait some time after open or close and hold last step
40 #define ERROR 4 // an error occured
44 #define CMD_TOGGLE 't'
45 #define CMD_STATUS 's'
48 #define STEPPER_OFF 0x30
49 byte current_state = IDLE; // current state of internal state machine
50 byte next_step = 0; // step counter 0 .. 3
51 #define MOVING_TIMEOUT 1600 // *2 ms, in case limit switches don't work stop and report an error
52 int timeout_cnt = 0; // counts up to MOVING_TIMEOUT
54 //********************************************************************//
58 pinMode(LIMIT_OPENED_PIN, INPUT); // set pin to input
59 digitalWrite(LIMIT_OPENED_PIN, HIGH); // turn on pullup resistors
61 pinMode(LIMIT_CLOSED_PIN, INPUT); // set pin to input
62 digitalWrite(LIMIT_CLOSED_PIN, HIGH); // turn on pullup resistors
67 if(digitalRead(LIMIT_OPENED_PIN))
75 if(digitalRead(LIMIT_CLOSED_PIN))
85 pinMode(AJAR_PIN, INPUT); // set pin to input
86 digitalWrite(AJAR_PIN, HIGH); // turn on pullup resistors
87 ajar_last_state = digitalRead(AJAR_PIN);
90 boolean get_ajar_status() // shut = true, ajar = false
92 if(digitalRead(AJAR_PIN))
102 pinMode(MANUAL_OPEN_PIN, INPUT); // set pin to input
103 digitalWrite(MANUAL_OPEN_PIN, HIGH); // turn on pullup resistors
105 pinMode(MANUAL_CLOSE_PIN, INPUT); // set pin to input
106 digitalWrite(MANUAL_CLOSE_PIN, HIGH); // turn on pullup resistors
108 debounce_state = DEBOUNCE_IDLE;
109 debounce_cnt = DEBOUNCE_DELAY;
112 boolean manual_open_pressed()
114 if(digitalRead(MANUAL_OPEN_PIN))
120 boolean manual_close_pressed()
122 if(digitalRead(MANUAL_CLOSE_PIN))
128 void start_debounce_timer() // this breaks millis() function, but who cares
130 debounce_cnt = DEBOUNCE_DELAY;
132 TCCR0A = 0; // no prescaler, WGM = 0 (normal)
134 OCR0A = 255; // 1+255 = 256 -> 16us @ 16 MHz
135 //OCR0A = 255; // 1+255 = 256 -> 12.8us @ 20 MHz
136 TCNT0 = 0; // reseting timer
137 TIMSK0 = 1<<OCF0A; // enable Interrupt
141 void stop_debounce_timer()
144 TCCR0B = 0; // no clock source
145 TIMSK0 = 0; // disable timer interrupt
148 ISR(TIMER0_COMPA_vect)
150 if(((debounce_state & DEBOUNCE_OPEN) && manual_open_pressed()) ||
151 ((debounce_state & DEBOUNCE_CLOSE) && manual_close_pressed())) {
156 debounce_state |= DEBOUNCE_FINISHED;
158 debounce_cnt = DEBOUNCE_DELAY;
161 boolean manual_open()
163 if(manual_open_pressed()) {
164 if(debounce_state & DEBOUNCE_CLOSE) {
165 stop_debounce_timer();
166 debounce_state = DEBOUNCE_IDLE;
170 if(debounce_state == DEBOUNCE_IDLE) {
171 debounce_state = DEBOUNCE_OPEN;
172 start_debounce_timer();
174 else if(debounce_state & DEBOUNCE_FINISHED) {
175 stop_debounce_timer();
176 debounce_state = DEBOUNCE_IDLE;
180 else if(debounce_state & DEBOUNCE_OPEN) {
181 stop_debounce_timer();
182 debounce_state = DEBOUNCE_IDLE;
188 boolean manual_close()
190 if(manual_close_pressed()) {
191 if(debounce_state & DEBOUNCE_OPEN) {
192 stop_debounce_timer();
193 debounce_state = DEBOUNCE_IDLE;
197 if(debounce_state == DEBOUNCE_IDLE) {
198 debounce_state = DEBOUNCE_CLOSE;
199 start_debounce_timer();
201 else if(debounce_state & DEBOUNCE_FINISHED) {
202 stop_debounce_timer();
203 debounce_state = DEBOUNCE_IDLE;
207 else if(debounce_state & DEBOUNCE_CLOSE) {
208 stop_debounce_timer();
209 debounce_state = DEBOUNCE_IDLE;
215 //********************************************************************//
226 DDRB = 0x0F; // set PortB 3:0 as output
230 byte step_table(byte step)
232 switch(step) { // 0011 xxxx, manual keys pull-ups stay active
248 digitalWrite(LEDS_GREEN_COMMON_PIN, HIGH);
249 digitalWrite(LEDS_RED_COMMON_PIN, HIGH);
255 pinMode(LEDS_GREEN_COMMON_PIN, OUTPUT);
256 pinMode(LEDS_RED_COMMON_PIN, OUTPUT);
260 byte led_table(byte led)
262 switch(led) { // xxxx xx00, leave RxD and TxD to 0
275 digitalWrite(LEDS_GREEN_COMMON_PIN, LOW);
276 digitalWrite(LEDS_RED_COMMON_PIN, HIGH);
281 digitalWrite(LEDS_GREEN_COMMON_PIN, HIGH);
282 digitalWrite(LEDS_RED_COMMON_PIN, LOW);
287 if(digitalRead(LEDS_GREEN_COMMON_PIN) == HIGH) {
288 digitalWrite(LEDS_GREEN_COMMON_PIN, LOW);
289 digitalWrite(LEDS_RED_COMMON_PIN, HIGH);
292 digitalWrite(LEDS_GREEN_COMMON_PIN, HIGH);
293 digitalWrite(LEDS_RED_COMMON_PIN, LOW);
299 void start_step_timer()
301 // timer 1: 2 ms, between stepper output state changes
302 TCCR1A = 0; // prescaler 1:256, WGM = 4 (CTC)
303 TCCR1B = 1<<WGM12 | 1<<CS12; //
304 OCR1A = 124; // (1+124)*256 = 32000 -> 2 ms @ 16 MHz
305 //OCR1A = 155; // (1+155)*256 = 40000 -> 2 ms @ 20 MHz
306 TCNT1 = 0; // reseting timer
307 TIMSK1 = 1<<OCIE1A; // enable Interrupt
310 void start_wait_timer()
312 // timer1: 250 ms, minimal delay between subsequent open/close
313 TCCR1A = 0; // prescaler 1:256, WGM = 0 (normal)
315 OCR1A = 15624; // (1+15624)*256 = 4000000 -> 250 ms @ 16 MHz
316 //OCR1A = 19530; // (1+19530)*256 = 5000000 -> 250 ms @ 20 MHz
317 TCNT1 = 0; // reseting timer
318 TIMSK1 = 1<<OCIE1A; // enable Interrupt
321 void start_error_timer()
323 // timer1: 500 ms, blinking leds with 1 Hz
324 TCCR1A = 0; // prescaler 1:256, WGM = 4 (CTC)
325 TCCR1B = 1<<WGM12 | 1<<CS12; //
326 OCR1A = 31249; // (1+31249)*256 = 8000000 -> 500 ms @ 16 MHz
327 //OCR1A = 39061; // (1+39061)*256 = 10000000 -> 500 ms @ 20 MHz
328 TCNT1 = 0; // reseting timer
329 TIMSK1 = 1<<OCIE1A; // enable Interrupt
332 void stop_timer() // stop the timer
335 TCCR1B = 0; // no clock source
336 TIMSK1 = 0; // disable timer interrupt
339 ISR(TIMER1_COMPA_vect)
341 // check if limit switch is active
342 if((current_state == OPENING && is_opened()) ||
343 (current_state == CLOSING && is_closed()))
347 if(current_state == OPENING)
352 current_state = WAIT;
357 if(current_state == OPENING || current_state == CLOSING) {
359 if(timeout_cnt >= MOVING_TIMEOUT) {
362 current_state = ERROR;
363 Serial.println("Error: open/close took too long!");
370 if(current_state == OPENING) { // next step (open)
371 PORTB = step_table(next_step);
376 else if(current_state == CLOSING) { // next step (close)
377 PORTB = step_table(next_step);
383 else if(current_state == WAIT) { // wait after last open/close finished -> idle
386 current_state = IDLE;
387 Serial.print("Status: ");
389 Serial.print("opened");
391 Serial.print("closed");
392 Serial.print(", idle");
393 if(get_ajar_status())
394 Serial.println(", shut");
396 Serial.println(", ajar");
399 else if(current_state == ERROR) {
403 else { // timer is useless stop it
409 if(led_delay_cnt >= LED_DELAY) {
412 PORTD = led_table(next_led);
414 if(current_state == OPENING) {
420 else if(current_state == CLOSING) {
428 //********************************************************************//
430 void reset_heartbeat()
432 digitalWrite(HEARTBEAT_PIN, HIGH);
438 digitalWrite(HEARTBEAT_PIN, LOW);
443 digitalWrite(HEARTBEAT_PIN, HIGH);
446 void init_heartbeat()
448 pinMode(HEARTBEAT_PIN, OUTPUT);
450 // timer 2: ~10 ms, timebase for heartbeat signal
451 TCCR2A = 1<<WGM21; // prescaler 1:1024, WGM = 2 (CTC)
452 TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20; //
453 OCR2A = 155; // (1+155)*1024 = 159744 -> ~10 ms @ 16 MHz
454 //OCR2A = 194; // (1+194)*1024 = 199680 -> ~10 ms @ 20 MHz
455 TCNT2 = 0; // reseting timer
456 TIMSK2 = 1<<OCIE2A; // enable Interrupt
460 // while running this gets called every ~10ms
461 ISR(TIMER2_COMPA_vect)
464 if(heartbeat_cnt == HEARTBEAT_DURATION)
466 else if(heartbeat_cnt >= HEARTBEAT_DELAY) {
472 //********************************************************************//
474 void reset_after_error()
481 current_state = IDLE;
485 current_state = CLOSING;
488 Serial.println("Ok, closing now");
496 current_state = OPENING;
505 current_state = CLOSING;
511 Serial.print("Status: ");
513 Serial.print("opened");
515 Serial.print("closed");
519 switch(current_state) {
520 case IDLE: Serial.print(", idle"); break;
521 case OPENING: Serial.print(", opening"); break;
522 case CLOSING: Serial.print(", closing"); break;
523 case WAIT: Serial.print(", waiting"); break;
524 default: Serial.print(", <undefined state>"); break;
526 if(get_ajar_status())
527 Serial.println(", shut");
529 Serial.println(", ajar");
544 current_state = IDLE;
546 // make sure door is locked after reset
551 current_state = CLOSING;
554 Serial.println("init complete");
559 if(Serial.available()) {
560 char command = Serial.read();
562 if(current_state == ERROR && command != CMD_RESET) {
563 Serial.println("Error: last open/close operation took too long!");
565 else if (command == CMD_RESET) {
568 else if (command == CMD_OPEN) {
569 if(current_state == IDLE) {
571 Serial.println("Already open");
574 Serial.println("Ok");
578 Serial.println("Error: Operation in progress");
580 else if (command == CMD_CLOSE) {
581 if(current_state == IDLE) {
583 Serial.println("Already closed");
586 Serial.println("Ok");
590 Serial.println("Error: Operation in progress");
592 else if (command == CMD_TOGGLE) {
593 if(current_state == IDLE) {
598 Serial.println("Ok");
601 Serial.println("Error: Operation in progress");
603 else if (command == CMD_STATUS)
606 Serial.println("Error: unknown command");
608 if(manual_open() && !is_opened() && (current_state == IDLE || current_state == ERROR)) {
609 Serial.println("open forced manually");
612 if(manual_close() && !is_closed() && (current_state == IDLE || current_state == ERROR)) {
613 Serial.println("close forced manually");
616 if(current_state == IDLE) {
626 boolean a = get_ajar_status();
627 if(a != ajar_last_state)