projects
/
svn42.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
ajar-state: boolean vs. byte
[svn42.git]
/
firmware
/
tuer.pde
diff --git
a/firmware/tuer.pde
b/firmware/tuer.pde
index
c0c4bf1
..
3813e00
100644
(file)
--- a/
firmware/tuer.pde
+++ b/
firmware/tuer.pde
@@
-20,9
+20,14
@@
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 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 SHUT 0
+#define AJAR 1
+byte ajar_last_state = SHUT;
+
#define MANUAL_OPEN_PIN 12 // keys for manual open and close
#define MANUAL_CLOSE_PIN 13 //
#define MANUAL_OPEN_PIN 12 // keys for manual open and close
#define MANUAL_CLOSE_PIN 13 //
-#define DEBOUNCE_DELAY 625 // * 16us = 10ms
+#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_IDLE 0 // currently no debouncing
#define DEBOUNCE_OPEN 1 // debouncing open key
#define DEBOUNCE_CLOSE 2 // debouncing close key
@@
-38,6
+43,7
@@
int debounce_cnt = 0;
#define CMD_OPEN 'o'
#define CMD_CLOSE 'c'
#define CMD_OPEN 'o'
#define CMD_CLOSE 'c'
+#define CMD_TOGGLE 't'
#define CMD_STATUS 's'
#define CMD_RESET 'r'
#define CMD_STATUS 's'
#define CMD_RESET 'r'
@@
-76,6
+82,23
@@
boolean is_closed()
//**********//
//**********//
+byte get_ajar_status()
+{
+ if(digitalRead(AJAR_PIN) == LOW)
+ return SHUT;
+
+ return AJAR;
+}
+
+void init_ajar()
+{
+ pinMode(AJAR_PIN, INPUT); // set pin to input
+ digitalWrite(AJAR_PIN, HIGH); // turn on pullup resistors
+ ajar_last_state = get_ajar_status();
+}
+
+//**********//
+
void init_manual()
{
pinMode(MANUAL_OPEN_PIN, INPUT); // set pin to input
void init_manual()
{
pinMode(MANUAL_OPEN_PIN, INPUT); // set pin to input
@@
-109,8
+132,9
@@
void start_debounce_timer() // this breaks millis() function, but who cares
debounce_cnt = DEBOUNCE_DELAY;
TCCR0A = 0; // no prescaler, WGM = 0 (normal)
debounce_cnt = DEBOUNCE_DELAY;
TCCR0A = 0; // no prescaler, WGM = 0 (normal)
- TCCR0B = 1<<WGM01; //
+ TCCR0B = 1<<CS00; //
OCR0A = 255; // 1+255 = 256 -> 16us @ 16 MHz
OCR0A = 255; // 1+255 = 256 -> 16us @ 16 MHz
+ //OCR0A = 255; // 1+255 = 256 -> 12.8us @ 20 MHz
TCNT0 = 0; // reseting timer
TIMSK0 = 1<<OCF0A; // enable Interrupt
TCNT0 = 0; // reseting timer
TIMSK0 = 1<<OCF0A; // enable Interrupt
@@
-125,8
+149,8
@@
void stop_debounce_timer()
ISR(TIMER0_COMPA_vect)
{
ISR(TIMER0_COMPA_vect)
{
- if((debounce_state & DEBOUNCE_OPEN && manual_open_pressed()) ||
- (debounce_state & DEBOUNCE_CLOSE && manual_close_pressed())) {
+ if(((debounce_state & DEBOUNCE_OPEN) && manual_open_pressed()) ||
+ ((debounce_state & DEBOUNCE_CLOSE) && manual_close_pressed())) {
if(debounce_cnt) {
debounce_cnt--;
return;
if(debounce_cnt) {
debounce_cnt--;
return;
@@
-155,7
+179,7
@@
boolean manual_open()
return true;
}
}
return true;
}
}
- else {
+ else if(debounce_state & DEBOUNCE_OPEN) {
stop_debounce_timer();
debounce_state = DEBOUNCE_IDLE;
}
stop_debounce_timer();
debounce_state = DEBOUNCE_IDLE;
}
@@
-182,7
+206,7
@@
boolean manual_close()
return true;
}
}
return true;
}
}
- else {
+ else if(debounce_state & DEBOUNCE_CLOSE) {
stop_debounce_timer();
debounce_state = DEBOUNCE_IDLE;
}
stop_debounce_timer();
debounce_state = DEBOUNCE_IDLE;
}
@@
-280,6
+304,7
@@
void start_step_timer()
TCCR1A = 0; // prescaler 1:256, WGM = 4 (CTC)
TCCR1B = 1<<WGM12 | 1<<CS12; //
OCR1A = 124; // (1+124)*256 = 32000 -> 2 ms @ 16 MHz
TCCR1A = 0; // prescaler 1:256, WGM = 4 (CTC)
TCCR1B = 1<<WGM12 | 1<<CS12; //
OCR1A = 124; // (1+124)*256 = 32000 -> 2 ms @ 16 MHz
+ //OCR1A = 155; // (1+155)*256 = 40000 -> 2 ms @ 20 MHz
TCNT1 = 0; // reseting timer
TIMSK1 = 1<<OCIE1A; // enable Interrupt
}
TCNT1 = 0; // reseting timer
TIMSK1 = 1<<OCIE1A; // enable Interrupt
}
@@
-290,6
+315,7
@@
void start_wait_timer()
TCCR1A = 0; // prescaler 1:256, WGM = 0 (normal)
TCCR1B = 1<<CS12; //
OCR1A = 15624; // (1+15624)*256 = 4000000 -> 250 ms @ 16 MHz
TCCR1A = 0; // prescaler 1:256, WGM = 0 (normal)
TCCR1B = 1<<CS12; //
OCR1A = 15624; // (1+15624)*256 = 4000000 -> 250 ms @ 16 MHz
+ //OCR1A = 19530; // (1+19530)*256 = 5000000 -> 250 ms @ 20 MHz
TCNT1 = 0; // reseting timer
TIMSK1 = 1<<OCIE1A; // enable Interrupt
}
TCNT1 = 0; // reseting timer
TIMSK1 = 1<<OCIE1A; // enable Interrupt
}
@@
-300,6
+326,7
@@
void start_error_timer()
TCCR1A = 0; // prescaler 1:256, WGM = 4 (CTC)
TCCR1B = 1<<WGM12 | 1<<CS12; //
OCR1A = 31249; // (1+31249)*256 = 8000000 -> 500 ms @ 16 MHz
TCCR1A = 0; // prescaler 1:256, WGM = 4 (CTC)
TCCR1B = 1<<WGM12 | 1<<CS12; //
OCR1A = 31249; // (1+31249)*256 = 8000000 -> 500 ms @ 16 MHz
+ //OCR1A = 39061; // (1+39061)*256 = 10000000 -> 500 ms @ 20 MHz
TCNT1 = 0; // reseting timer
TIMSK1 = 1<<OCIE1A; // enable Interrupt
}
TCNT1 = 0; // reseting timer
TIMSK1 = 1<<OCIE1A; // enable Interrupt
}
@@
-359,6
+386,16
@@
ISR(TIMER1_COMPA_vect)
stop_timer();
reset_stepper();
current_state = IDLE;
stop_timer();
reset_stepper();
current_state = IDLE;
+ Serial.print("Status: ");
+ if(is_opened())
+ Serial.print("opened");
+ else if(is_closed())
+ Serial.print("closed");
+ Serial.print(", idle");
+ if(get_ajar_status() == SHUT)
+ Serial.println(", shut");
+ else
+ Serial.println(", ajar");
return;
}
else if(current_state == ERROR) {
return;
}
else if(current_state == ERROR) {
@@
-416,6
+453,7
@@
void init_heartbeat()
TCCR2A = 1<<WGM21; // prescaler 1:1024, WGM = 2 (CTC)
TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20; //
OCR2A = 155; // (1+155)*1024 = 159744 -> ~10 ms @ 16 MHz
TCCR2A = 1<<WGM21; // prescaler 1:1024, WGM = 2 (CTC)
TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20; //
OCR2A = 155; // (1+155)*1024 = 159744 -> ~10 ms @ 16 MHz
+ //OCR2A = 194; // (1+194)*1024 = 199680 -> ~10 ms @ 20 MHz
TCNT2 = 0; // reseting timer
TIMSK2 = 1<<OCIE2A; // enable Interrupt
heartbeat_on();
TCNT2 = 0; // reseting timer
TIMSK2 = 1<<OCIE2A; // enable Interrupt
heartbeat_on();
@@
-454,32
+492,20
@@
void reset_after_error()
void start_open()
{
void start_open()
{
- if(is_opened()) {
- Serial.println("Already open");
- return;
- }
-
reset_stepper();
reset_leds();
leds_green();
current_state = OPENING;
start_step_timer();
reset_stepper();
reset_leds();
leds_green();
current_state = OPENING;
start_step_timer();
- Serial.println("Ok");
}
void start_close()
{
}
void start_close()
{
- if(is_closed()) {
- Serial.println("Already closed");
- return;
- }
-
reset_stepper();
reset_leds();
leds_red();
current_state = CLOSING;
start_step_timer();
reset_stepper();
reset_leds();
leds_red();
current_state = CLOSING;
start_step_timer();
- Serial.println("Ok");
}
void print_status()
}
void print_status()
@@
-493,13
+519,16
@@
void print_status()
Serial.print("<->");
switch(current_state) {
Serial.print("<->");
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(", <undefined state>"); 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(", <undefined state>"); break;
}
}
-
+ if(get_ajar_status() == SHUT)
+ Serial.println(", shut");
+ else
+ Serial.println(", ajar");
}
//**********//
}
//**********//
@@
-507,6
+536,7
@@
void print_status()
void setup()
{
init_limits();
void setup()
{
init_limits();
+ init_ajar();
init_stepper();
init_leds();
init_heartbeat();
init_stepper();
init_leds();
init_heartbeat();
@@
-523,6
+553,7
@@
void setup()
current_state = CLOSING;
start_step_timer();
}
current_state = CLOSING;
start_step_timer();
}
+ Serial.println("init complete");
}
void loop()
}
void loop()
@@
-531,20
+562,43
@@
void loop()
char command = Serial.read();
if(current_state == ERROR && command != CMD_RESET) {
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) {
}
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) {
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");
}
else
Serial.println("Error: Operation in progress");
}
@@
-553,15
+607,15
@@
void loop()
else
Serial.println("Error: unknown command");
}
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();
}
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();
}
Serial.println("close forced manually");
start_close();
}
- if (current_state == IDLE) {
+ if(current_state == IDLE) {
if(is_opened()) {
leds_green();
PORTD = LEDS_ON;
if(is_opened()) {
leds_green();
PORTD = LEDS_ON;
@@
-571,4
+625,10
@@
void loop()
PORTD = LEDS_ON;
}
}
PORTD = LEDS_ON;
}
}
+ byte a = get_ajar_status();
+ if(a != ajar_last_state)
+ {
+ print_status();
+ ajar_last_state = a;
+ }
}
}