X-Git-Url: https://git.realraum.at/?a=blobdiff_plain;f=firmware%2Ftuer.pde;h=c4bfcb4ddf32924300bc43f9e7b116b7bda86daa;hb=4bb3eba50f61cc7f57d0ad279ab310ea5368a88d;hp=1808c883ee888c4ca7943b26e0c39d5d2ce54a27;hpb=2099456b39e30a6baa1e31cdd2be5c1f26b5faeb;p=svn42.git diff --git a/firmware/tuer.pde b/firmware/tuer.pde index 1808c88..c4bfcb4 100644 --- a/firmware/tuer.pde +++ b/firmware/tuer.pde @@ -20,8 +20,17 @@ byte next_led = 0; #define LIMIT_OPENED_PIN 18 // A4: limit switch for open #define LIMIT_CLOSED_PIN 19 // A5: limit switch for close +#define AJAR_PIN 14 // input pin for reed relais (door ajar/shut) + #define MANUAL_OPEN_PIN 12 // keys for manual open and close #define MANUAL_CLOSE_PIN 13 // +#define DEBOUNCE_DELAY 6250 // * 16us = 100ms +#define DEBOUNCE_IDLE 0 // currently no debouncing +#define DEBOUNCE_OPEN 1 // debouncing open key +#define DEBOUNCE_CLOSE 2 // debouncing close key +#define DEBOUNCE_FINISHED 4 // debouncing finished +byte debounce_state; +int debounce_cnt = 0; #define IDLE 0 // close and open may be called #define OPENING 1 // opening, only 's' command is allowed @@ -31,6 +40,7 @@ byte next_led = 0; #define CMD_OPEN 'o' #define CMD_CLOSE 'c' +#define CMD_TOGGLE 't' #define CMD_STATUS 's' #define CMD_RESET 'r' @@ -69,6 +79,22 @@ boolean is_closed() //**********// +void init_ajar() +{ + pinMode(AJAR_PIN, INPUT); // set pin to input + digitalWrite(AJAR_PIN, HIGH); // turn on pullup resistors +} + +boolean get_ajar_status() // shut = true, ajar = false +{ + if(digitalRead(AJAR_PIN)) + return false; + + return true; +} + +//**********// + void init_manual() { pinMode(MANUAL_OPEN_PIN, INPUT); // set pin to input @@ -76,9 +102,12 @@ void init_manual() pinMode(MANUAL_CLOSE_PIN, INPUT); // set pin to input digitalWrite(MANUAL_CLOSE_PIN, HIGH); // turn on pullup resistors + + debounce_state = DEBOUNCE_IDLE; + debounce_cnt = DEBOUNCE_DELAY; } -boolean manual_open() +boolean manual_open_pressed() { if(digitalRead(MANUAL_OPEN_PIN)) return false; @@ -86,7 +115,7 @@ boolean manual_open() return true; } -boolean manual_close() +boolean manual_close_pressed() { if(digitalRead(MANUAL_CLOSE_PIN)) return false; @@ -94,6 +123,93 @@ boolean manual_close() return true; } +void start_debounce_timer() // this breaks millis() function, but who cares +{ + debounce_cnt = DEBOUNCE_DELAY; + + TCCR0A = 0; // no prescaler, WGM = 0 (normal) + TCCR0B = 1< 16us @ 16 MHz + //OCR0A = 255; // 1+255 = 256 -> 12.8us @ 20 MHz + TCNT0 = 0; // reseting timer + TIMSK0 = 1< 2 ms @ 16 MHz + //OCR1A = 155; // (1+155)*256 = 40000 -> 2 ms @ 20 MHz TCNT1 = 0; // reseting timer TIMSK1 = 1< 250 ms @ 16 MHz + //OCR1A = 19530; // (1+19530)*256 = 5000000 -> 250 ms @ 20 MHz TCNT1 = 0; // reseting timer TIMSK1 = 1< 500 ms @ 16 MHz + //OCR1A = 39061; // (1+39061)*256 = 10000000 -> 500 ms @ 20 MHz TCNT1 = 0; // reseting timer TIMSK1 = 1< ~10 ms @ 16 MHz + //OCR2A = 194; // (1+194)*1024 = 199680 -> ~10 ms @ 20 MHz TCNT2 = 0; // reseting timer TIMSK2 = 1<"); switch(current_state) { - case IDLE: Serial.println(", idle"); break; - case OPENING: Serial.println(", opening"); break; - case CLOSING: Serial.println(", closing"); break; - case WAIT: Serial.println(", waiting"); break; - default: Serial.println(", "); break; + case IDLE: Serial.print(", idle"); break; + case OPENING: Serial.print(", opening"); break; + case CLOSING: Serial.print(", closing"); break; + case WAIT: Serial.print(", waiting"); break; + default: Serial.print(", "); break; } - + if(get_ajar_status()) + Serial.println(", shut"); + else + Serial.println(", ajar"); } //**********// @@ -411,6 +532,7 @@ void print_status() void setup() { init_limits(); + init_ajar(); init_stepper(); init_leds(); init_heartbeat(); @@ -435,20 +557,43 @@ void loop() char command = Serial.read(); if(current_state == ERROR && command != CMD_RESET) { - Serial.println("Error: last open/close operation took to long!"); + Serial.println("Error: last open/close operation took too long!"); } else if (command == CMD_RESET) { reset_after_error(); } else if (command == CMD_OPEN) { - if(current_state == IDLE) - start_open(); + if(current_state == IDLE) { + if(is_opened()) + Serial.println("Already open"); + else { + start_open(); + Serial.println("Ok"); + } + } else Serial.println("Error: Operation in progress"); } else if (command == CMD_CLOSE) { - if(current_state == IDLE) - start_close(); + if(current_state == IDLE) { + if(is_closed()) + Serial.println("Already closed"); + else { + start_close(); + Serial.println("Ok"); + } + } + else + Serial.println("Error: Operation in progress"); + } + else if (command == CMD_TOGGLE) { + if(current_state == IDLE) { + if(is_closed()) + start_open(); + else + start_close(); + Serial.println("Ok"); + } else Serial.println("Error: Operation in progress"); } @@ -457,11 +602,11 @@ void loop() else Serial.println("Error: unknown command"); } - if(manual_open() && !is_opened() && current_state == IDLE) { + if(manual_open() && !is_opened() && (current_state == IDLE || current_state == ERROR)) { Serial.println("open forced manually"); start_open(); } - if(manual_close() && !is_closed() && current_state == IDLE) { + if(manual_close() && !is_closed() && (current_state == IDLE || current_state == ERROR)) { Serial.println("close forced manually"); start_close(); }